text:
#pragma once
#ifndef THRILL_NET_FLOW_CONTROL_CHANNEL_HEADER
#define THRILL_NET_FLOW_CONTROL_CHANNEL_HEADER

#include <thrill/common/defines.hpp>
#include <thrill/common/functional.hpp>
#include <thrill/common/thread_barrier.hpp>
#include <thrill/net/collective.hpp>
#include <thrill/net/group.hpp>

#include <algorithm>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <string>
#include <vector>

namespace thrill {
namespace net {

//! \addtogroup net_layer
//! \{

/*!
 * Provides a blocking collection for communication.
 *
 * This wraps a raw net group, adds multi-worker/thread support, and should be
 * used for flow control with integral types.
 *
 * Important notice on threading: It is not allowed to call two different
 * methods of two different instances of FlowControlChannel simultaniously by
 * different threads, since the internal synchronization state (the barrier) is
 * shared globally.
 *
 * The implementations will be replaced by better/decentral versions.
 *
 * This class is probably the worst thing I've ever coded (ej).
 */
class FlowControlChannel
{
private:
    static constexpr bool self_verify = false;

    //! The group associated with this channel.
    Group& group_;

    //! The local host rank.
    size_t host_rank_;

    //! The count of all workers connected to this group.
    size_t num_hosts_;

    //! The id of the worker thread associated with this flow channel.
    size_t local_id_;

    //! The count of all workers connected to this group.
    size_t thread_count_;

    //! The shared barrier used to synchronize between worker threads on this node.
    common::ThreadBarrier& barrier_;

    //! Thread local data structure: aligned such that no cache line is
    //! shared. The actual vector is in the FlowControlChannelManager.
    struct LocalData
    {
        //! pointer to some thread-owned data type
        alignas(common::g_cache_line_size)
        std::atomic<void*> ptr { nullptr };

        //! atomic generation counter, compare this to generation_.
        std::atomic<size_t>     counter { 0 };

#if THRILL_HAVE_THREAD_SANITIZER
        // workarounds because ThreadSanitizer has false-positives work with
        // generation counters.

        //! mutex for locking condition variable
        std::mutex              mutex;

        //! condition variable for signaling incrementing of conunter.
        std::condition_variable cv;
#endif

        //! \name Generation Counting
        //! \{

        void WaitCounter(size_t this_step) {
#if THRILL_HAVE_THREAD_SANITIZER
            std::unique_lock<std::mutex> lock(mutex);
            cv.wait(lock, [&]() { return (counter != this_step); });
#else
            // busy wait on generation counter of predecessor
            while (counter.load(std::memory_order_relaxed) != this_step) { }
#endif
        }

        void                    IncCounter() {
            ++counter;
#if THRILL_HAVE_THREAD_SANITIZER
            std::unique_lock<std::mutex> lock(mutex);
            cv.notify_one();
#endif
        }

        //! \}
    };

    static_assert(sizeof(LocalData) % common::g_cache_line_size == 0,
                  "struct LocalData has incorrect size.");

    //! for access to struct LocalData
    friend class FlowControlChannelManager;

    //! The global shared local data memory location to work upon.
    LocalData* shmem_;

    //! Host-global shared generation counter
    std::atomic<size_t>& generation_;

    //! \name Pointer Casting
    //! \{

    template <typename T>
    void SetLocalShared(const T* value) {
        // We are only allowed to set our own memory location.
        size_t idx = local_id_;
        shmem_[idx].ptr.store(
            const_cast<void*>(reinterpret_cast<const void*>(value)),
            std::memory_order_release);
    }

    template <typename T>
    T * GetLocalShared(size_t idx) {
        assert(idx < thread_count_);
        return reinterpret_cast<T*>(
            shmem_[idx].ptr.load(std::memory_order_acquire));
    }

    template <typename T>
    T * GetLocalShared() {
        GetLocalShared<T>(local_id_);
    }

    //! \}

public:
    //! Creates a new instance of this class, wrapping a group.
    FlowControlChannel(Group& group,
                       size_t local_id, size_t thread_count,
                       common::ThreadBarrier& barrier,
                       LocalData* shmem,
                       std::atomic<size_t>& generation)
        : group_(group),
          host_rank_(group_.my_host_rank()), num_hosts_(group_.num_hosts()),
          local_id_(local_id),
          thread_count_(thread_count),
          barrier_(barrier), shmem_(shmem), generation_(generation) { }

    //! Return the associated net::Group. USE AT YOUR OWN RISK.
    Group& group() { return group_; }

    //! Return the worker's global rank
    size_t my_rank() const {
        return group_.my_host_rank() * thread_count_ + local_id_;
    }

    //! Return the total number of workers.
    size_t num_workers() const {
        return group_.num_hosts() * thread_count_;
    }

#ifdef SWIG
#define THRILL_ATTRIBUTE_WARN_UNUSED_RESULT
#endif

    /*!
     * Calculates the prefix sum over all workers, given a certain sum
     * operation.
     *
     * This method blocks until the sum is caluclated. Values are applied in
     * order, that means sum_op(sum_op(a, b), c) if a, b, c are the values of
     * workers 0, 1, 2.
     *
     * \param value The local value of this worker.
     * \param initial The initial element for the body defined by T and sum_op
     * \param sum_op The operation to use for
     * calculating the prefix sum. The default operation is a normal addition.
     * \param inclusive Whether the prefix sum is inclusive or exclusive.
     * \return The prefix sum for the position of this worker.
     */
    template <typename T, typename BinarySumOp = std::plus<T> >
    T THRILL_ATTRIBUTE_WARN_UNUSED_RESULT
    PrefixSum(const T& value, const T& initial = T(),
              const BinarySumOp& sum_op = BinarySumOp(),
              bool inclusive = true) {

        static constexpr bool debug = false;

        T local_value = value;

        SetLocalShared(&local_value);

        barrier_.Await();

        // Local Reduce
        if (local_id_ == 0) {

            // Global Prefix
            T** locals = reinterpret_cast<T**>(alloca(thread_count_ * sizeof(T*)));

            for (size_t i = 0; i < thread_count_; i++) {
                locals[i] = GetLocalShared<T>(i);
            }

            for (size_t i = 1; i < thread_count_; i++) {
                *(locals[i]) = sum_op(*(locals[i - 1]), *(locals[i]));
            }

            if (debug) {
                for (size_t i = 0; i < thread_count_; i++) {
                    // LOG << id_ << ", " << i << ", " << inclusive << ": me: " << *(locals[i]);
                }
            }

            T base_sum = *(locals[thread_count_ - 1]);
            group_.PrefixSum(base_sum, sum_op, false);

            if (host_rank_ == 0) {
                base_sum = initial;
            }

            // LOG << id_ << ", m, " << inclusive << ": base: " << base_sum;

            if (inclusive) {
                for (size_t i = 0; i < thread_count_; i++) {
                    *(locals[i]) = sum_op(base_sum, *(locals[i]));
                }
            }
            else {
                for (size_t i = thread_count_ - 1; i > 0; i--) {
                    *(locals[i]) = sum_op(base_sum, *(locals[i - 1]));
                }
                *(locals[0]) = base_sum;
            }

            if (debug) {
                for (size_t i = 0; i < thread_count_; i++) {
                    // LOG << id_ << ", " << i << ", " << inclusive << ": res: " << *(locals[i]);
                }
            }
        }

        barrier_.Await();

        return local_value;
    }

    /*!
     * Calculates the exclusive prefix sum over all workers, given a certain sum
     * operation.
     *
     * This method blocks until the sum is caluclated. Values are applied in
     * order, that means sum_op(sum_op(a, b), c) if a, b, c are the values of
     * workers 0, 1, 2.
     *
     * \param value The local value of this worker.
     * \param sum_op The operation to use for
     * \param initial The initial element of the body defined by T and SumOp
     * calculating the prefix sum. The default operation is a normal addition.
     * \return The prefix sum for the position of this worker.
     */
    template <typename T, typename BinarySumOp = std::plus<T> >
    T THRILL_ATTRIBUTE_WARN_UNUSED_RESULT
    ExPrefixSum(const T& value, const T& initial = T(),
                const BinarySumOp& sum_op = BinarySumOp()) {
        return PrefixSum(value, initial, sum_op, false);
    }

    /*!
     * Broadcasts a value of a serializable type T from the master (the worker
     * with id 0) to all other workers.
     *
     * This method is blocking on all workers except the master.
     *
     * \param value The value to broadcast. This value is ignored for each
     * worker except the master. We use this signature to keep the decision
     * wether a node is the master transparent.
     *
     * \param origin Worker number to broadcast value from.
     *
     * \return The value sent by the master.
     */
    template <typename T>
    T THRILL_ATTRIBUTE_WARN_UNUSED_RESULT
    Broadcast(const T& value, size_t origin = 0) {

        T res = value;

        // Select primary thread of each node to handle IO (assumes all hosts
        // has the same number of threads).
        size_t local_pe = origin % thread_count_;

        if (local_id_ == local_pe) {
            SetLocalShared(&res);

            group_.Broadcast(res, origin / thread_count_);
        }

        barrier_.Await();

        // other threads: read value from thread 0.
        if (local_id_ != local_pe) {
            res = *GetLocalShared<T>(local_pe);
        }

        barrier_.Await();

        return res;
    }

    /*!
     * Reduces a value of a serializable type T over all workers to the given
     * worker, provided a certain reduce function.
     *
     * This method is blocking. The reduce happens in order as with prefix
     * sum. The operation is assumed to be associative.
     *
     * \param value The value to use for the reduce operation.
     * \param root destination worker of the reduce
     * \param sum_op The operation to use for
     * calculating the reduced value. The default operation is a normal addition.
     * \return The result of the reduce operation.
     */
    template <typename T, typename BinarySumOp = std::plus<T> >
    T THRILL_ATTRIBUTE_WARN_UNUSED_RESULT
    Reduce(const T& value, size_t root = 0,
           const BinarySumOp& sum_op = BinarySumOp()) {
        assert(root < num_workers());

        T local = value;

        SetLocalShared(&local);
        barrier_.Await();

        if (local_id_ == 0) {

            // Local Reduce
            for (size_t i = 1; i < thread_count_; i++) {
                local = sum_op(local, *GetLocalShared<T>(i));
            }

            // Global reduce
            group_.Reduce(local, root / thread_count_, sum_op);

            // set the local value only at the root
            if (root / thread_count_ == group_.my_host_rank())
                *GetLocalShared<T>(root % thread_count_) = local;
        }

        barrier_.Await();

        return local;
    }

    /*!
     * Reduces a value of a serializable type T over all workers given a certain
     * reduce function.
     *
     * This method is blocking. The reduce happens in order as with prefix
     * sum. The operation is assumed to be associative.
     *
     * \param value The value to use for the reduce operation.
     * \param sum_op The operation to use for
     * calculating the reduced value. The default operation is a normal addition.
     * \return The result of the reduce operation.
     */
    template <typename T, typename BinarySumOp = std::plus<T> >
    T THRILL_ATTRIBUTE_WARN_UNUSED_RESULT
    AllReduce(const T& value, const BinarySumOp& sum_op = BinarySumOp()) {
        T local = value;

        SetLocalShared(&local);

        barrier_.Await();

        if (local_id_ == 0) {

            // Local Reduce
            for (size_t i = 1; i < thread_count_; i++) {
                local = sum_op(local, *GetLocalShared<T>(i));
            }

            // Global reduce
            group_.AllReduce(local, sum_op);

            // We have the choice: One more barrier so each slave can read from
            // master's shared memory, or p writes to write to each slaves
            // mem. I choose the latter since the cost of a barrier is very
            // high.
            for (size_t i = 1; i < thread_count_; i++) {
                *GetLocalShared<T>(i) = local;
            }
        }

        barrier_.Await();

        return local;
    }

    /*!
     * Collects up to k predecessors of type T from preceding PEs. k must be
     * equal on all PEs.
     *
     * Assume each worker has <= k items. Predecessor collects up to the k items
     * from preceding PEs. If the directly preceding PE has fewer than k items,
     * then it waits for its predecessor to deliver items, in the hope to get up
     * to k.
     *
     * This is used by the Window() transformation, but may in future also be
     * useful to get a single predecessor item in other distributed operations.
     */
    template <typename T>
    std::vector<T> Predecessor(size_t k, const std::vector<T>& my_values) {

        std::vector<T> res;

        // this vector must live beyond the ThreadBarrier.
        std::vector<T> send_values;

        // get generation counter
        size_t this_step = generation_.load(std::memory_order_acquire) + 1;

        if (my_values.size() >= k) {
            // if we already have k items, then "transmit" them to our successor
            if (local_id_ + 1 != thread_count_) {
                SetLocalShared(&my_values);
                // release memory inside vector
                std::atomic_thread_fence(std::memory_order_release);
                // increment generation counter to match this_step.
                shmem_[local_id_].IncCounter();
            }
            else if (host_rank_ + 1 != num_hosts_) {
                if (my_values.size() > k) {
                    std::vector<T> send_values_next(my_values.end() - k, my_values.end());
                    group_.SendTo(host_rank_ + 1, send_values_next);
                }
                else {
                    group_.SendTo(host_rank_ + 1, my_values);
                }
                // increment generation counter for synchronizing
                shmem_[local_id_].IncCounter();
            }
            else {
                // increment generation counter for synchronizing
                shmem_[local_id_].IncCounter();
            }

            // and wait for the predecessor to deliver its batch
            if (local_id_ != 0) {
                // wait on generation counter of predecessor
                shmem_[local_id_ - 1].WaitCounter(this_step);

                // acquire memory inside vector
                std::atomic_thread_fence(std::memory_order_acquire);

                std::vector<T>* pre =
                    GetLocalShared<std::vector<T> >(local_id_ - 1);

                // copy over only k elements (there may be more or less)
                res = std::vector<T>(
                    pre->size() <= k ? pre->begin() : pre->end() - k, pre->end());
            }
            else if (host_rank_ != 0) {
                group_.ReceiveFrom(host_rank_ - 1, &res);
            }
        }
        else {
            // we don't have k items, wait for our predecessor to send some.
            if (local_id_ != 0) {
                // wait on generation counter of predecessor
                shmem_[local_id_ - 1].WaitCounter(this_step);

                // acquire memory inside vector
                std::atomic_thread_fence(std::memory_order_acquire);

                std::vector<T>* pre =
                    GetLocalShared<std::vector<T> >(local_id_ - 1);

                // copy over only k elements (there may be more)
                res = std::vector<T>(
                    pre->size() <= k ? pre->begin() : pre->end() - k, pre->end());
            }
            else if (host_rank_ != 0) {
                group_.ReceiveFrom(host_rank_ - 1, &res);
            }

            // prepend values we got from our predecessor with local ones, such
            // that they will fill up send_values together with all local items
            size_t fill_size = k - my_values.size();
            send_values.reserve(std::min(k, fill_size + res.size()));
            send_values.insert(
                send_values.end(),
                // copy last fill_size items from res. don't do end - fill_size,
                // because that may result in unsigned wrap-around.
                res.size() <= fill_size ? res.begin() : res.end() - fill_size,
                res.end());
            send_values.insert(send_values.end(),
                               my_values.begin(), my_values.end());
            assert(send_values.size() <= k);

            // now we have k items or at many as we can get, hence, "transmit"
            // them to our successor
            if (local_id_ + 1 != thread_count_) {
                SetLocalShared(&send_values);
                // release memory inside vector
                std::atomic_thread_fence(std::memory_order_release);
                // increment generation counter to match this_step.
                shmem_[local_id_].IncCounter();
            }
            else if (host_rank_ + 1 != num_hosts_) {
                group_.SendTo(host_rank_ + 1, send_values);
                // increment generation counter for synchronizing
                shmem_[local_id_].IncCounter();
            }
            else {
                // increment generation counter for synchronizing
                shmem_[local_id_].IncCounter();
            }
        }

        // await until all threads have retrieved their value.
        barrier_.Await([this]() { generation_++; });

        return res;
    }

    //! A trivial global barrier.
    void Barrier() {
        size_t i = 0;
        i = AllReduce(i);
    }

    //! A trivial local thread barrier
    void LocalBarrier() {
        barrier_.Await();
    }
};

//! \}

} // namespace net
} // namespace thrill

#endif // !THRILL_NET_FLOW_CONTROL_CHANNEL_HEADER

/******************************************************************************/


text:
<?php

/**
 * @file
 * Definition of ViewsUiNodeViewsWizard.
 */

/**
 * Tests creating node views with the wizard.
 */
class ViewsUiNodeViewsWizard extends ViewsUiBaseViewsWizard {

  protected function row_style_options($type) {
    $options = array();
    $options['teasers'] = t('teasers');
    $options['full_posts'] = t('full posts');
    $options['titles'] = t('titles');
    $options['titles_linked'] = t('titles (linked)');
    $options['fields'] = t('fields');
    return $options;
  }

  protected function build_form_style(&$form, &$form_state, $type) {
    parent::build_form_style($form, $form_state, $type);
    $style_form =& $form['displays'][$type]['options']['style'];
    // Some style plugins don't support row plugins so stop here if that's the
    // case.
    if (!isset($style_form['row_plugin']['#default_value'])) {
      return;
    }
    $row_plugin = $style_form['row_plugin']['#default_value'];
    switch ($row_plugin) {
      case 'full_posts':
      case 'teasers':
        $style_form['row_options']['links'] = array(
          '#type' => 'select',
          '#title_display' => 'invisible',
          '#title' => t('Should links be displayed below each node'),
          '#options' => array(
            1 => t('with links (allow users to add comments, etc.)'),
            0 => t('without links'),
          ),
          '#default_value' => 1,
        );
        $style_form['row_options']['comments'] = array(
          '#type' => 'select',
          '#title_display' => 'invisible',
          '#title' => t('Should comments be displayed below each node'),
          '#options' => array(
            1 => t('with comments'),
            0 => t('without comments'),
          ),
          '#default_value' => 0,
        );
        break;
    }
  }

  /**
   * @override
   */
  protected function default_display_options($form, $form_state) {
    $display_options = parent::default_display_options($form, $form_state);

    // Add permission-based access control.
    $display_options['access']['type'] = 'perm';
    $display_options['access']['perm'] = 'access content';

    // Remove the default fields, since we are customizing them here.
    unset($display_options['fields']);

    // Add the title field, so that the display has content if the user switches
    // to a row style that uses fields.
    /* Field: Content: Title */
    $display_options['fields']['title']['id'] = 'title';
    $display_options['fields']['title']['table'] = 'node';
    $display_options['fields']['title']['field'] = 'title';
    $display_options['fields']['title']['label'] = '';
    $display_options['fields']['title']['alter']['alter_text'] = 0;
    $display_options['fields']['title']['alter']['make_link'] = 0;
    $display_options['fields']['title']['alter']['absolute'] = 0;
    $display_options['fields']['title']['alter']['trim'] = 0;
    $display_options['fields']['title']['alter']['word_boundary'] = 0;
    $display_options['fields']['title']['alter']['ellipsis'] = 0;
    $display_options['fields']['title']['alter']['strip_tags'] = 0;
    $display_options['fields']['title']['alter']['html'] = 0;
    $display_options['fields']['title']['hide_empty'] = 0;
    $display_options['fields']['title']['empty_zero'] = 0;
    $display_options['fields']['title']['link_to_node'] = 1;

    return $display_options;
  }

  protected function page_display_options($form, $form_state) {
    $display_options = parent::page_display_options($form, $form_state);
    $row_plugin = isset($form_state['values']['page']['style']['row_plugin']) ? $form_state['values']['page']['style']['row_plugin'] : NULL;
    $row_options = isset($form_state['values']['page']['style']['row_options']) ? $form_state['values']['page']['style']['row_options'] : array();
    $this->display_options_row($display_options, $row_plugin, $row_options);
    return $display_options;
  }

  protected function block_display_options($form, $form_state) {
    $display_options = parent::block_display_options($form, $form_state);
    $row_plugin = isset($form_state['values']['block']['style']['row_plugin']) ? $form_state['values']['block']['style']['row_plugin'] : NULL;
    $row_options = isset($form_state['values']['block']['style']['row_options']) ? $form_state['values']['block']['style']['row_options'] : array();
    $this->display_options_row($display_options, $row_plugin, $row_options);
    return $display_options;
  }

  /**
   * Set the row style and row style plugins to the display_options.
   */
  protected  function display_options_row(&$display_options, $row_plugin, $row_options) {
    switch ($row_plugin) {
      case 'full_posts':
        $display_options['row_plugin'] = 'node';
        $display_options['row_options']['build_mode'] = 'full';
        $display_options['row_options']['links'] = !empty($row_options['links']);
        $display_options['row_options']['comments'] = !empty($row_options['comments']);
        break;
      case 'teasers':
        $display_options['row_plugin'] = 'node';
        $display_options['row_options']['build_mode'] = 'teaser';
        $display_options['row_options']['links'] = !empty($row_options['links']);
        $display_options['row_options']['comments'] = !empty($row_options['comments']);
        break;
      case 'titles_linked':
        $display_options['row_plugin'] = 'fields';
        $display_options['field']['title']['link_to_node'] = 1;
        break;
      case 'titles':
        $display_options['row_plugin'] = 'fields';
        $display_options['field']['title']['link_to_node'] = 0;
        break;
    }
  }
}

redpajama_set_name: RedPajamaGithub


text:
/**
 *
 * @author Daniel Garnacho y Diego Castaño
 */

package clasificadores;

import datos.Datos;
import datos.Elemento;
import datos.TiposDeAtributos;
import java.util.ArrayList;
import java.util.HashMap;

public class ClasificadorNaiveBayes extends Clasificador{
    // El primer Array nos dice la columna
    // El primer HashMap nos dice el valor de la columna
    // El segundo hashmap cuenta por cada valor de la columna cuantas incidencias
    // Tiene la clase, para hacer p(D|H)
    ArrayList<HashMap<Elemento, HashMap<Elemento, Integer>>> incidencias;

    // Cuenta las incidencias totales de cada clase
    HashMap<Elemento, Integer> incidenciaClaseTotal;

    // Filas totales de train
    int filasTrain = 0;

    // Medias y varianzas de los atributos continuos dadas las clases
    // Primer HashMap = Clase
    // Primer ArrayList = Columna
    HashMap<Elemento, ArrayList<Double>> medias;
    HashMap<Elemento, ArrayList<Double>> varianzas;

    private HashMap<Elemento, Integer> generarTablaCeros (Datos d) {
        ArrayList<Elemento> clases = d.getClases();
        HashMap<Elemento, Integer> tabla = new HashMap<>();
        for (Elemento clase : clases) {
            tabla.put(clase, 0);
        }
        return tabla;
    }

    private double calcularMedia(ArrayList<Elemento> elementos) {
        double media = 0.0;
        for (Elemento e : elementos) {
            media += e.getValorContinuo();
        }
        media = media / (double) elementos.size();
        return media;
    }

    private double calcularVarianza(ArrayList<Elemento> elementos) {
        double varianza = 0.0;
        double media = calcularMedia(elementos);

        for (Elemento e : elementos) {
            varianza += Math.pow(e.getValorContinuo() - media, 2);
        }

        varianza = varianza / (double) elementos.size();
        return varianza;
    }

    @Override
    public void entrenamiento(Datos datosTrain) {
        this.incidencias = new ArrayList<>();
        this.incidenciaClaseTotal = this.generarTablaCeros(datosTrain);
        this.medias = new HashMap<>();
        this.varianzas = new HashMap<>();

        // Inicializa medias y varianzas por clase
        for (Elemento clase : datosTrain.getClases()) {
            ArrayList<Double> columnas = new ArrayList<>();
            ArrayList<Double> columnas2 = new ArrayList<>();
            for (int i = 0; i < datosTrain.getTamColumn(); i++) {
                columnas.add(0.0);
                columnas2.add(0.0);
            }
            this.medias.put(clase, columnas);
            this.varianzas.put(clase, columnas2);
        }

        // Calcula media y varianza de las columnas continuas por cada clase

        // Para cada clase
        for (Elemento clase: datosTrain.getClases()) {

            // Para cada columna
            int tamcolumn = datosTrain.getTamColumn() - 1;
            for (int i = 0; i < tamcolumn; i++) {

                ArrayList<Elemento> elementosContinuos = new ArrayList<>();

                // Recorrer las filas
                for (Elemento fila[] : datosTrain.getDatos()) {

                    // Si la fila es de la clase y la columa continua
                    if (fila[fila.length - 1].equals(clase) &&
                            fila[i].getTipo().equals(TiposDeAtributos.Continuo)) {

                        elementosContinuos.add(fila[i]);
                    }
                }

                // Calcular media de elementosContinuos
                double media = this.calcularMedia(elementosContinuos);
                // Calcular varianza de elementosContinuos
                double varianza = this.calcularVarianza(elementosContinuos);

                // Guardas valores

                this.medias.get(clase).set(i, media);
                this.varianzas.get(clase).set(i, varianza);

            }
        }

        // Crea un hashmap de hashmaps para cada columna
        for(Elemento e: datosTrain.getDatos()[0]){
            HashMap<Elemento, HashMap<Elemento, Integer>> incidenciaColumna = new HashMap<>();
            this.incidencias.add(incidenciaColumna);
        }

        for(Elemento fila[] : datosTrain.getDatos()){
            this.filasTrain++;
            Elemento clase = fila[fila.length-1];
            for(int i = 0; i < (fila.length - 1); i++){
                HashMap<Elemento, HashMap<Elemento, Integer>> incidenciaColumna  = this.incidencias.get(i);
                // Si el valor de la columna ya está en el hashmap
                if(incidenciaColumna.containsKey(fila[i])){
                    //se ha encontrado el elemento
                    HashMap<Elemento, Integer> incidenciaClase = incidenciaColumna.get(fila[i]);
                    if(incidenciaClase.containsKey(clase)){
                        Integer contador = incidenciaClase.get(clase);
                        contador++;
                        incidenciaClase.put(clase, contador);
                    }

                }else{
                    //no se encontraba el elemento en la base de datos
                    HashMap<Elemento, Integer> incidenciaClase = this.generarTablaCeros(datosTrain);
                    Integer contador = incidenciaClase.get(clase);
                    contador++;
                    incidenciaClase.put(clase, contador);
                    incidenciaColumna.put(fila[i], incidenciaClase);

                }
            }
            //sumar las incidencias de cada clase
            Integer nIncidencias = this.incidenciaClaseTotal.get(clase);
            nIncidencias++;
            this.incidenciaClaseTotal.put(clase, nIncidencias);

        }

    }

    @Override
    public ArrayList<Elemento> clasifica(Datos datosTest) {
        ArrayList<Elemento> clasificado = new ArrayList<>();

        for(Elemento fila[] : datosTest.getDatos()){
            Elemento mejorClase = null;
            double mejorProb = -1;
            for(Elemento claseTest : datosTest.getClases()){

                // Simulacion de la clase, decimos, si fuera esta clase, que prob da

                double prob = -1;
                for(int i = 0; i < (fila.length - 1); i++){
                    /*
                        prob de el dato dada la hipotesis
                        MULi (p(Di|H))
                    */
                    double probAux;

                    if (fila[i].getTipo().equals(TiposDeAtributos.Continuo)) {
                        /* Si el dato es continuo */
                        // Sacar la media y varianza del atributo dada la clase claseTest0;
                        double media = this.medias.get(claseTest).get(i);
                        double varianza = this.varianzas.get(claseTest).get(i);
                        // Calcular P(D|H) según func. de dist. de una normal
                        double elevado = (fila[i].getValorContinuo() - media);
                        elevado = - (Math.pow(elevado, 2.0) / (double)(2.0 * varianza));
                        elevado = Math.pow(Math.E, elevado);
                        probAux = elevado / (double) Math.sqrt(2.0 * Math.PI * varianza);
                        if(i == 0){
                            prob = probAux;
                        }else{
                            prob = prob*probAux;
                        }

                    } else {
                        /* Si el dato es nominal */
                        try{
                            probAux = this.incidencias.get(i).get(fila[i]).get(claseTest);
                        }catch(Exception e){
                            probAux = 0;
                        }
                        probAux = probAux/this.incidenciaClaseTotal.get(claseTest);
                        if(i == 0){
                            prob = probAux;
                        }else{
                            prob = prob*probAux;
                        }
                    }

                }
                /*
                    prob de la hipotesis
                */
                double probAux = this.incidenciaClaseTotal.get(claseTest);
                probAux = probAux/this.filasTrain;
                prob = prob*probAux;
                if(prob > mejorProb){
                    mejorProb = prob;
                    mejorClase = claseTest;
                }
            }
            /*fin del iterador de las clases*/
            clasificado.add(mejorClase);
        }
        /*fin del iterador de las filas*/
        return clasificado;
    }

}


text:
use std::io::{Read, Write, Cursor};
use std::net::{TcpStream, Shutdown};
use std::thread;
use crate::{Error, Result};
use crate::protocol::{self, Message};

pub struct Proxy {
    c2s_thread: thread::JoinHandle<Result<()>>,
    s2c_thread: thread::JoinHandle<Result<()>>,
}

impl Proxy {
    pub fn from_tcp_streams(mut server_stream: TcpStream, mut client_stream: TcpStream) ->
            Result<Proxy> {
        let server_version = protocol::Version::read_from(&mut server_stream)?;
        debug!("c<-s {:?}", server_version);
        protocol::Version::write_to(&server_version, &mut client_stream)?;

        let client_version = protocol::Version::read_from(&mut client_stream)?;
        debug!("c->s {:?}", client_version);
        protocol::Version::write_to(&client_version, &mut server_stream)?;

        fn security_type_supported(security_type: &protocol::SecurityType) -> bool {
            match security_type {
                &protocol::SecurityType::None => true,
                security_type => {
                    warn!("security type {:?} is not supported", security_type);
                    false
                }
            }
        }

        let security_types = match client_version {
            protocol::Version::Rfb33 => {
                let mut security_type = protocol::SecurityType::read_from(&mut server_stream)?;
                debug!("!<-s SecurityType::{:?}", security_type);

                // Filter out security types we can't handle
                if !security_type_supported(&security_type) {
                    security_type = protocol::SecurityType::Invalid
                }

                debug!("c<-! SecurityType::{:?}", security_type);
                protocol::SecurityType::write_to(&security_type, &mut client_stream)?;

                if security_type == protocol::SecurityType::Invalid {
                    vec![]
                } else {
                    vec![security_type]
                }
            },
            _ => {
                let mut security_types =
                    protocol::SecurityTypes::read_from(&mut server_stream)?;
                debug!("!<-s {:?}", security_types);

                // Filter out security types we can't handle
                security_types.0.retain(security_type_supported);

                debug!("c<-! {:?}", security_types);
                protocol::SecurityTypes::write_to(&security_types, &mut client_stream)?;

                security_types.0
            }
        };

        if security_types.is_empty() {
            let reason = String::read_from(&mut server_stream)?;
            debug!("c<-s {:?}", reason);
            String::write_to(&reason, &mut client_stream)?;

            return Err(Error::Server(reason))
        }

        let used_security_type = match client_version {
            protocol::Version::Rfb33 => security_types[0],
            _ => {
                let used_security_type =
                    protocol::SecurityType::read_from(&mut client_stream)?;
                debug!("c->s SecurityType::{:?}", used_security_type);
                protocol::SecurityType::write_to(&used_security_type, &mut server_stream)?;

                used_security_type
            }
        };

        let mut skip_security_result = false;
        match &(used_security_type, client_version) {
            &(protocol::SecurityType::None, protocol::Version::Rfb33) |
            &(protocol::SecurityType::None, protocol::Version::Rfb37) =>
                skip_security_result = true,
            _ => ()
        }

        if !skip_security_result {
            let security_result = protocol::SecurityResult::read_from(&mut server_stream)?;
            debug!("c<-s SecurityResult::{:?}", security_result);
            protocol::SecurityResult::write_to(&security_result, &mut client_stream)?;

            if security_result == protocol::SecurityResult::Failed {
                match client_version {
                    protocol::Version::Rfb33 | protocol::Version::Rfb37 =>
                        return Err(Error::AuthenticationFailure(String::from(""))),
                    protocol::Version::Rfb38 => {
                        let reason = String::read_from(&mut server_stream)?;
                        debug!("c<-s {:?}", reason);
                        String::write_to(&reason, &mut client_stream)?;
                        return Err(Error::AuthenticationFailure(reason))
                    }
                }
            }
        }

        let client_init = protocol::ClientInit::read_from(&mut client_stream)?;
        debug!("c->s {:?}", client_init);
        protocol::ClientInit::write_to(&client_init, &mut server_stream)?;

        let server_init = protocol::ServerInit::read_from(&mut server_stream)?;
        debug!("c<-s {:?}", server_init);
        protocol::ServerInit::write_to(&server_init, &mut client_stream)?;

        let (mut c2s_server_stream, mut c2s_client_stream) =
            (server_stream.try_clone().unwrap(), client_stream.try_clone().unwrap());
        let (mut s2c_server_stream, mut s2c_client_stream) =
            (server_stream.try_clone().unwrap(), client_stream.try_clone().unwrap());

        fn forward_c2s(server_stream: &mut TcpStream, client_stream: &mut TcpStream) ->
                Result<()> {
            fn encoding_supported(encoding: &protocol::Encoding) -> bool {
                match encoding {
                    &protocol::Encoding::Raw |
                    &protocol::Encoding::CopyRect |
                    &protocol::Encoding::Zrle |
                    &protocol::Encoding::Cursor |
                    &protocol::Encoding::DesktopSize => true,
                    encoding => {
                        warn!("encoding {:?} is not supported", encoding);
                        false
                    }
                }
            }

            loop {
                let mut message = protocol::C2S::read_from(client_stream)?;
                match message {
                    protocol::C2S::SetEncodings(ref mut encodings) => {
                        debug!("c->! SetEncodings({:?})", encodings);

                        // Filter out encodings we can't handle
                        encodings.retain(encoding_supported);

                        debug!("!->s SetEncodings({:?})", encodings);
                    },
                    protocol::C2S::SetPixelFormat(_) => {
                        // There is an inherent race condition in the VNC protocol (I think)
                        // between SetPixelFormat and FramebufferUpdate and I've no idea
                        // how to handle it properly, so defer for now.
                        panic!("proxying SetPixelFormat is not implemented!")
                    },
                    ref message => debug!("c->s {:?}", message)
                }
                protocol::C2S::write_to(&message, server_stream)?
            }
        }

        fn forward_s2c(server_stream: &mut TcpStream, client_stream: &mut TcpStream,
                       format: protocol::PixelFormat) ->
                Result<()> {

            loop {
                let mut buffer_stream = Cursor::new(Vec::new());

                let message = protocol::S2C::read_from(server_stream)?;
                debug!("c<-s {:?}", message);
                protocol::S2C::write_to(&message, &mut buffer_stream)?;

                match message {
                    protocol::S2C::FramebufferUpdate { count } => {
                        for _ in 0..count {
                            let rectangle = protocol::Rectangle::read_from(server_stream)?;
                            debug!("c<-s {:?}", rectangle);
                            protocol::Rectangle::write_to(&rectangle, &mut buffer_stream)?;

                            match rectangle.encoding {
                                protocol::Encoding::Raw => {
                                    let mut pixels = vec![0; (rectangle.width as usize) *
                                                             (rectangle.height as usize) *
                                                             (format.bits_per_pixel as usize / 8)];
                                    server_stream.read_exact(&mut pixels)?;
                                    debug!("c<-s ...raw pixels");
                                    buffer_stream.write_all(&pixels)?;
                                },
                                protocol::Encoding::CopyRect => {
                                    let copy_rect =
                                        protocol::CopyRect::read_from(server_stream)?;
                                    debug!("c<-s {:?}", copy_rect);
                                    protocol::CopyRect::write_to(&copy_rect,
                                                                      &mut buffer_stream)?;
                                },
                                protocol::Encoding::Zrle => {
                                    let zrle = Vec::<u8>::read_from(server_stream)?;
                                    debug!("c<-s ...ZRLE pixels");
                                    Vec::<u8>::write_to(&zrle, &mut buffer_stream)?;
                                }
                                protocol::Encoding::Cursor => {
                                    let mut pixels    = vec![0; (rectangle.width as usize) *
                                                                (rectangle.height as usize) *
                                                                (format.bits_per_pixel as usize / 8)];
                                    server_stream.read_exact(&mut pixels)?;
                                    buffer_stream.write_all(&pixels)?;
                                    let mut mask_bits = vec![0; ((rectangle.width as usize + 7) / 8) *
                                                                (rectangle.height as usize)];
                                    server_stream.read_exact(&mut mask_bits)?;
                                    buffer_stream.write_all(&mask_bits)?;
                                },
                                protocol::Encoding::DesktopSize => (),
                                _ => return Err(Error::Unexpected("encoding"))
                            }
                        }
                    },
                    _ => ()
                }

                let buffer = buffer_stream.into_inner();
                client_stream.write_all(&buffer)?;
            }
        }

        Ok(Proxy {
            c2s_thread: thread::spawn(move || {
                let result = forward_c2s(&mut c2s_server_stream, &mut c2s_client_stream);
                let _ = c2s_server_stream.shutdown(Shutdown::Both);
                let _ = c2s_client_stream.shutdown(Shutdown::Both);
                result
            }),
            s2c_thread: thread::spawn(move || {
                let result = forward_s2c(&mut s2c_server_stream, &mut s2c_client_stream,
                                         server_init.pixel_format);
                let _ = s2c_server_stream.shutdown(Shutdown::Both);
                let _ = s2c_client_stream.shutdown(Shutdown::Both);
                result
            })
        })
    }

    pub fn join(self) -> Result<()> {
        let c2s_result = self.c2s_thread.join().unwrap();
        let s2c_result = self.s2c_thread.join().unwrap();
        match c2s_result.and(s2c_result) {
            Err(Error::Disconnected) => Ok(()),
            result => result
        }
    }
}


text:
package com.swfarm.biz.warehouse.dao;

import com.swfarm.biz.warehouse.bo.ApvShippingCost;
import com.swfarm.pub.framework.dao.GenericDao;

public interface ApvShippingCostDao extends
GenericDao<ApvShippingCost, Long> {

}


text:
/* [ ---- Gebo Admin Panel - wizard ---- ] */

$(document).ready(function () {
    //* simple wizard
    gebo_wizard.simple();
    //* wizard with validation
    gebo_wizard.validation();
    //* add step numbers to titles
    gebo_wizard.steps_nb();
});

gebo_wizard = {
    simple: function () {
        $('#simple_wizard').stepy({
            titleClick: true,
            nextLabel: 'Next ',
            backLabel: ' Previous',
            finishButton: false
        });
    },
    validation: function () {
        $('#courseDetailForm').stepy({
            nextLabel: 'Next ',
            backLabel: ' Previous',
            block: true,
            errorImage: true,
            titleClick: true,
            validate: true,
            finishButton: false,
            next: function () {
                $.ajax({
                    url: "../../model/com.gogetrich.function/checkCourseDetailCreated.php",
                    type: 'POST',
                    beforeSend: function (xhr) {
                        $("html").addClass("js");
                    },
                    success: function (data, textStatus, jqXHR) {
                        if (data == 100) {
                            $('#courseDetailForm').stepy('step', 1);
                            setTimeout(function () {
                                $("#notificationDialog").modal("show");
                                $("#notiDetailDialog").html("Please choose course event date time");
                            }, 100);

                        }
                        $("html").removeClass("js");
                    }
                });
            }
        });
        stepy_validation = $('#courseDetailForm').validate({
            onfocusout: false,
            errorPlacement: function (error, element) {
                error.appendTo(element.closest("div.controls"));
            },
            highlight: function (element) {
                $(element).closest("div.control-group").addClass("error f_error");
                var thisStep = $(element).closest('form').prev('ul').find('.current-step');
                thisStep.addClass('error-image');
            },
            unhighlight: function (element) {
                $(element).closest("div.control-group").removeClass("error f_error");
                if (!$(element).closest('form').find('div.error').length) {
                    var thisStep = $(element).closest('form').prev('ul').find('.current-step');
                    thisStep.removeClass('error-image');
                }

            },
            rules: {
                'courseCate': 'required',
                'courseName': 'required',
                'courseStatus': 'required',
                'courseDuration': 'required'
//                'availableSeat': 'required'
            }, messages: {
                'courseCate': {required: 'Please select category!'},
                'courseName': {required: 'Course Name field is required!'},
                'courseStatus': {required: 'Course Status field is required!'},
                'courseDuration': {required: 'Please fill course duration (days)'}
//                'availableSeat': {required: 'Please fill available seats'}
            },
            ignore: ':hidden'
        });
    },
    //* add numbers to step titles
    steps_nb: function () {
        $('.stepy-titles').each(function () {
            $(this).children('li').each(function (index) {
                var myIndex = index + 1
                $(this).append('' + myIndex + '');
            });
        });
    }
};


text:
namespace YAF.Pages
{
    #region Using

    using System;

    using YAF.Classes;
    using YAF.Core;
    using YAF.Types;
    using YAF.Types.Interfaces;

    #endregion

    /// <summary>
    /// The shoutbox popup page.
    /// </summary>
    public partial class shoutbox : ForumPage
    {
        #region Constructors and Destructors

        /// <summary>
        ///   Initializes a new instance of the <see cref = "shoutbox" /> class.
        /// </summary>
        public shoutbox()
            : base("SHOUTBOX")
        {
            this.AllowAsPopup = true;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load([NotNull] object sender, [NotNull] EventArgs e)
        {
            this.ShoutBox1.Visible = this.Get<YafBoardSettings>().ShowShoutbox
                                     && this.Get<IPermissions>()
                                            .Check(this.Get<YafBoardSettings>().ShoutboxViewPermissions);
        }

        #endregion
    }
}


text:
from django.shortcuts import render
from django.urls import re_path, path
from django.contrib import admin
from django.views.defaults import server_error
from .models import MyModel

def raise_exception(request):
    raise Exception('This one should be handled by 500 technical view')

def show_mymodel(request):
    model = MyModel(afield='thisismine')
    model.save()
    return render(request, 'mymodel.html', {'model': model})

urlpatterns = [
    path('mymodel/', show_mymodel),
    re_path(r'^admin/', admin.site.urls),
    re_path(r'contacts/australia/(?P<value>[^/]+)/', lambda r, value: None, name='contacts_australia'),
    re_path(r'contacts/australia/(?P<value>\d+)/', lambda r, value: None, name='contacts_china'),
    re_path(r'raiser/', raise_exception, name='raiser'),
    re_path(r'^devices/(?P<grp>([\w() 0-9a-zA-Z!*:.?+=_-])+)$', lambda r, value: None, name='devices_grp'),
]

handler500 = lambda request: server_error(request, template_name='my500.html')


text:
#ifndef STATS_H
#define STATS_H

#include <stdio.h>
#include <stdbool.h>
#include <hdr/hdr_histogram.h>
#include "tinymt64.h"

#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))

typedef struct {
    uint32_t connect;
    uint32_t read;
    uint32_t write;
    uint32_t status;
    uint32_t timeout;
} errors;

typedef struct {
    uint64_t samples;
    uint64_t index;
    uint64_t limit;
    uint64_t min;
    uint64_t max;
    struct hdr_histogram *histogram;
    uint64_t data[];
} stats;

stats *stats_alloc(uint64_t);
void stats_free(stats *);
void stats_reset(stats *);
void stats_rewind(stats *);

void stats_record(stats *, uint64_t);

long double stats_summarize(stats *);
long double stats_mean(stats *);
long double stats_stdev(stats *stats, long double);
long double stats_within_stdev(stats *, long double, long double, uint64_t);
uint64_t stats_percentile(stats *, long double);

void stats_sample(stats *, tinymt64_t *, uint64_t, stats *);
uint64_t rand64(tinymt64_t *, uint64_t);

#endif /* STATS_H */


text:
package com.ait.lienzo.charts.client.core.resizer;

import com.google.gwt.event.shared.GwtEvent;

public class ChartResizeEvent extends GwtEvent<ChartResizeEventHandler>
{
    public static Type<ChartResizeEventHandler> TYPE = new Type<ChartResizeEventHandler>();

    private double                              width;

    private double                              height;

    private boolean                             apply;

    public ChartResizeEvent(double width, double height, boolean apply)
    {
        this.width = width;
        this.height = height;
        this.apply = apply;
    }

    @Override
    public Type<ChartResizeEventHandler> getAssociatedType()
    {
        return TYPE;
    }

    @Override
    protected void dispatch(ChartResizeEventHandler handler)
    {
        handler.onChartResize(this);
    }

    public double getHeight()
    {
        return height;
    }

    public double getWidth()
    {
        return width;
    }

    public boolean isApply()
    {
        return apply;
    }
}


text:
<?php
/**
 * Created by JetBrains PhpStorm.
 * User: quantum
 * Date: 2/14/14
 * Time: 9:31 AM
 * To change this template use File | Settings | File Templates.
 */

namespace Exam\WebBundle\Controller;

use Exam\DomainBundle\Entity\Test\Attempt;
use Exam\WebBundle\Service\AttemptService;
use Exam\WebBundle\Service\EnrollmentService;
use Exam\WebBundle\Service\LoginService;
use Exam\WebBundle\Service\PackageService;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use Exam\DomainBundle\Repository\EnrollmentRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Exam\AopBundle\Transactional;

/**
 * Class ExamController
 * @package Exam\WebBundle\Controller
 */
class ExamController extends BaseController {

    private $service,
            $enrollmentService,
            $attemptService;

    /**
     * @InjectParams({
     *      "service" = @Inject("loginService"),
     *      "enrollmentService" = @Inject("enrollmentService"),
     *      "attemptService" = @Inject("attemptService")
     * })
     */
    public function __construct(LoginService $service,
                                EnrollmentService $enrollmentService,
                                AttemptService $attemptService) {
        $this->service = $service;
        $this->enrollmentService = $enrollmentService;
        $this->attemptService = $attemptService;
    }

    /**
     * @Route("/exam")
     * @Method({"GET"})
     */
    public function startExam() {
        if(!$this->service->isLogin())  return $this->redirect('/login');

        if(!$this->enrollmentService->hasEnrollment()) {
            return $this->render('ExamWebBundle:Exam:enrollment.html.twig', [
                "all" => $this->enrollmentService->getAvailableEnrollments()
            ]);
        }

        return $this->render('ExamWebBundle:Exam:question.html.twig', [
            "package" => $this->enrollmentService->getCurrentPackage(),
            "enrollment" => $this->enrollmentService->getEnrollment()
        ]);
    }

    /**
     * @Route("/exam/{enrollmentId}")
     * @Method({"GET"})
     * @Transactional
     */
    public function setPackage($enrollmentId) {
        if(!$this->service->isLogin()) {
            return $this->redirect('/login');
        }

        $this->enrollmentService->startEnrollment($enrollmentId);

        return $this->redirect("/exam");
    }

    /**
     * @Route("/exam/attempt")
     * @Method({"POST"})
     * @Transactional
     */
    public function addAttempt(Request $request) {
        if($request->isXmlHttpRequest()) {
            $param = $request->request;

            $this->attemptService->addAttempt($param->get('questionId'), $param->get('answerId'));

            return new JsonResponse([
                'status'=> 200
            ]);
        }

        return new JsonResponse([
            'status' => 500
        ]);
    }

    /**
     * @Route("/exam/enrollment/finished")
     * @Method({"GET"})
     */
    public function sayThanks() {
        if(!$this->service->isLogin()) return $this->redirect('/login');

        if($this->enrollmentService->hasEnrollment()) return $this->redirect('/exam');

        return $this->render('ExamWebBundle:Exam:thanks.html.twig');
    }

    /**
     * @Route("/exam/enrollment/finished")
     * @Method({"POST"})
     * @Transactional
     */
    public function finishEnrollment(Request $request) {
        if($request->isXmlHttpRequest()) {
            $this->enrollmentService->finishEnrollment();

            return new JsonResponse([
                'status'=> 200,
                'url' => '/exam/enrollment/finished'
            ]);
        }

        if($request->isMethod('POST')) {
            $this->enrollmentService->finishEnrollment();

            return $this->redirect('/exam/enrollment/finished');
        }

        return new JsonResponse([
            'status' => 500
        ]);
    }

}


text:
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
-- |

module VK.API.Models.Types.User where

import           Data.Aeson
import           Data.Aeson.Casing
import qualified Data.Text                    as T
import qualified Data.Text.Read               as TR
import           Data.Time.Clock.POSIX        (POSIXTime)
import           GHC.Generics                 (Generic)

import           VK.API.CommonTypes
import           VK.API.Models.Types.Counters
import           VK.Internal.Utils

data City = City {
  cityId      :: Int
  , cityTitle :: T.Text
  }
               deriving (Show, Generic)

instance FromJSON City where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data Country = Country {
  countryId      :: Int
  , countryTitle :: T.Text
  }
               deriving (Show, Generic)

instance FromJSON Country where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data Contacts = Contacts {
  contactsMobilePhone :: !(Maybe T.Text)
  , contactsHomePhone :: !(Maybe T.Text)
  }
               deriving (Show, Generic)

instance FromJSON Contacts where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data Education = Education {
  educationUniversity       :: !(Maybe Int)
  , educationUniversityName :: !(Maybe T.Text)
  , educationFaculty        :: !(Maybe Int)
  , educationFacultyName    :: !(Maybe T.Text)
  , educationGraduation     :: !(Maybe Int)
  }
               deriving (Show, Generic)

instance FromJSON Education where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data University = University {
  universityId            :: !Int
  , universityCountry     :: !Int
  , universityCity        :: !Int
  , universityName        :: !T.Text
  , universityFaculty     :: !Int
  , universityFacultyName :: !T.Text
  , universityChair       :: !Int
  , universityChairName   :: !T.Text
  , universityGraduation  :: !Int
  }
               deriving (Show, Generic)

instance FromJSON University where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data School = School {
  schoolId              :: !Int
  , schoolCountry       :: !Int
  , schoolCity          :: !Int
  , schoolName          :: !T.Text
  , schoolYearFrom      :: !Int
  , schoolYearTo        :: !Int
  , schoolYearGraduated :: !Int
  , schoolClass         :: !T.Text
  , schoolSpeciality    :: !T.Text
  , schoolType          :: !Int
  , schoolTypeStr       :: !T.Text
  }
               deriving (Show, Generic)

instance FromJSON School where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data LastSeen = LastSeen {
  lastseenTime       :: !POSIXTime
  , lastseenPlatform :: !Int
  }
              deriving (Show, Generic)

instance FromJSON LastSeen where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data OccupationType = OccupationWork
                    | OccupationSchool
                    | OccupationUniversity
                    deriving (Show, Generic)

instance FromJSON OccupationType where
  parseJSON =
    withText "OccupationType"
    (\case
         "work" -> pure OccupationWork
         "school" -> pure OccupationSchool
         "university" -> pure OccupationUniversity
         _ -> mempty
    )

data Occupation = Occupation {
  occupationId     :: !Int
  , occupationName :: !T.Text
  , occupationType :: !OccupationType
  }
               deriving (Show, Generic)

instance FromJSON Occupation where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data UserRelative = UserRelative {
  userrelativeId     :: !(Maybe Int)
  , userrelativeName :: !(Maybe T.Text)
  , userrelativeType :: !(Maybe T.Text)
  }
                  deriving (Show, Generic)

instance FromJSON UserRelative where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data UserRelationPartner = UserRelationPartner {
  userrelationpartnerId     :: !Int
  , userrelationpartnerName :: !T.Text
  }
                         deriving (Show, Generic)

instance FromJSON UserRelationPartner where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data Personal = Personal {
  personalPolitical    :: !Political
  , personalLangs      :: ![T.Text]
  , personalReligion   :: !T.Text
  , personalInspiredBy :: !T.Text
  , personalPeopleMain :: !PeopleMain
  , personalLifeMain   :: !LifeMain
  , personalSmoking    ::  !Attitude
  , personalAlcohol    :: !Attitude
  }
              deriving (Show, Generic)

instance FromJSON Personal where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data Political = PoliticalUnknown
               | Communist
               | Socialist
               | Moderate
               | Liberal
               | Conservative
               | Monarchical
               | Ultraconservative
               | Indifferent
               | Libertarian
               deriving (Show, Generic, Enum, Bounded)

instance FromJSON Political where
  parseJSON =
    intEnumFromJSON "Political" ([minBound ..]::[Political])

data PeopleMain = PeopleMainUnknown
                | MindAndCreativity
                | KindnessAndHonesty
                | Beauty
                | PowerAndWealth
                | CourageAndPerseverance
                | HumorAndLoveForLife
               deriving (Show, Generic, Enum, Bounded)

instance FromJSON PeopleMain where
  parseJSON =
    intEnumFromJSON "PeopleMain" ([minBound ..]::[PeopleMain])

data LifeMain = LifeMainUnknown
     | FamilyAndChildren
     | CareerAndMoney
     | EntertainmentAndLeisure
     | ScienceAndResearch
     | ImprovementOfTheWorld
     | SelfDevelopment
     | BeautyAndArt
     | FameAndInfluence
     deriving (Show, Generic, Enum, Bounded)

instance FromJSON LifeMain where
  parseJSON =
    intEnumFromJSON "LifeMain" ([minBound ..]::[LifeMain])

data Attitude = AttitudeUnknown
      | VeryNegative
      | Negative
      | Neutral
      | Compromise
      | Positive
     deriving (Show, Generic, Enum, Bounded)

instance FromJSON Attitude where
  parseJSON =
    intEnumFromJSON "Attitude" ([minBound ..]::[Attitude])

-- | вырезанная фотография пользователя, поля: x, y, x2, y2, координаты указаны в процентах.
data Crop = Crop {
  x    :: !Int
  , y  :: !Int
  , x2 :: !Int
  , y2 :: !Int
  }
          deriving (Show, Generic)

instance FromJSON Crop where

data CropPhoto = CropPhoto {
  cropphotoPhoto  :: !T.Text
           -- ^ объект photo фотографии пользователя из которой
           -- вырезается профильная аватарка.
  , cropphotoCrop :: !Crop
  , cropphotoRect :: !Crop
                     -- ^ миниатюрная квадратная фотография,
                     -- вырезанная из фотографии crop
  }
               deriving (Show, Generic)

instance FromJSON CropPhoto where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data FriendStatus = NotFriend
                    -- ^ пользователь не является другом,
                  | SubscriptionSent
                    -- ^ отправлена заявка/подписка пользователю,
                  | SubscriptionExists
                    -- ^ имеется входящая заявка/подписка от пользователя,
                  | IsFriend
                    -- ^ пользователь является другом;
                  deriving (Show, Generic, Enum, Bounded)

instance FromJSON FriendStatus where
  parseJSON =
    intEnumFromJSON "FriendStatus" ([minBound ..]::[FriendStatus])

data Career = Career {
  careerGroupId     :: !(Maybe Int)
             -- ^ идентификатор сообщества (если доступно, иначе company);
  , careerCompany   :: !(Maybe T.Text)
               -- ^ название организации (если доступно, иначе group_id);
  , careerCountryId :: !(Maybe Int)
                 -- ^ идентификатор страны;
  , careerCityId    :: !(Maybe Int)
              -- ^ идентификатор города (если доступно, иначе city_name);
  , careerCityName  :: !(Maybe T.Text)
                -- ^ название города (если доступно, иначе city_id);
  , careerFrom      :: !(Maybe Int)
            -- ^ год начала работы;
  , careerUntil     :: !(Maybe Int)
             -- ^ год окончания работы;
  , careerPosition  :: !(Maybe T.Text)
                      -- ^ должность
  }
            deriving (Show, Generic)

instance FromJSON Career where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data Military = Military {
  militaryUnitId      :: !(Maybe Int)
  , militaryUnit      :: !(Maybe T.Text)
  , militaryCountryId :: !(Maybe Int)
  , militaryFrom      :: !(Maybe Int)
  , militaryUntil     :: !(Maybe Int)
  }
              deriving (Show, Generic)

instance FromJSON Military where
  parseJSON = genericParseJSON $ aesonPrefix snakeCase

data UserPhotoId = UserPhotoId {
  userphotoUserId    :: Int
  , userphotoPhotoId :: Int
  }
                   deriving Show

instance FromJSON UserPhotoId where
  parseJSON =
    withText "UserPhotoId" parsePhid
    where
      parsePhid v =
        either (const mempty) pure $ do
          let vs = T.splitOn "_" v

          case vs of
            [uidTxt, phidTxt] -> do
              uidv <- TR.decimal uidTxt
              phidv <- TR.decimal phidTxt
              case (uidv, phidv) of
                ((uid, ""), (phid, "")) ->
                  return $ UserPhotoId uid phid
                _ ->
                  fail "bad value"
            _ ->
              fail "bad value"

data User = User {
  userId                       :: !Int
  , userFirstName              :: !T.Text
  , userLastName               :: !T.Text
  , userDeactivated            :: !(Maybe Int)
  , userHidden                 :: !(Maybe Int)
  , userPhotoId                :: !(Maybe UserPhotoId)
  , userVerified               :: !(Maybe Int)
  , userBlacklisted            :: !(Maybe Int)
  , userSex                    :: !(Maybe Sex)
  , userBdate                  :: !(Maybe T.Text)
  , userCity                   :: !(Maybe City)
  , userCountry                :: !(Maybe Country)
  , userHomeTown               :: !(Maybe T.Text)
  , userPhoto50                :: !(Maybe T.Text)
  , userPhoto100               :: !(Maybe T.Text)
  , userPhoto200               :: !(Maybe T.Text)
  , userPhoto200Orig           :: !(Maybe T.Text)
  , userPhoto400               :: !(Maybe T.Text)
  , userPhoto400Orig           :: !(Maybe T.Text)
  , userPhotoMax               :: !(Maybe T.Text)
  , userPhotoMaxOrig           :: !(Maybe T.Text)
  , userOnline                 :: !(Maybe Int)
  , userLists                  :: !(Maybe [Int])
  , userDomain                 :: !(Maybe T.Text)
  , userHasMobile              :: !(Maybe Int)
  , userContacts               :: !(Maybe Contacts)
  , userSite                   :: !(Maybe T.Text)
  , userEducation              :: !(Maybe Education)
  , userUniversities           :: !(Maybe [University])
  , userSchools                :: !(Maybe [School])
  , userStatus                 :: !(Maybe T.Text)
  , userStatusAudio            :: !(Maybe T.Text)
  , userLastSeen               :: !(Maybe LastSeen)
  , userFollowersCount         :: !(Maybe Int)
  , userCommonCount            :: !(Maybe Int)
  , userCounters               :: !(Maybe Counters)
  , userOccupation             :: !(Maybe Occupation)
  , userNickname               :: !(Maybe T.Text)
  , userRelatives              :: !(Maybe [UserRelative])
  , userRelation               :: !(Maybe UserRelation)
  , userRelationPartner        :: !(Maybe UserRelationPartner)
  , userPersonal               :: !(Maybe Personal)

  , userConnections            :: !(Maybe T.Text)
                       -- ^ возвращает данные об указанных в профиле сервисах
                       -- пользователя, таких как: skype, facebook, twitter, livejournal,
                       -- instagram.
  , userExports                :: !(Maybe T.Text)
                   -- ^ внешние сервисы, в которые настроен экспорт из ВК (twitter,
                   -- facebook, livejournal, instagram).
  , userWallComments           :: !(Maybe Int)
                        -- ^ доступно ли комментирование стены (1 — доступно, 0 — недоступно).
  , userActivities             :: !(Maybe T.Text)
                                  -- ^ деятельность.
  , userInterests              :: !(Maybe T.Text)
                                  -- ^ интересы.
  , userMusic                  :: !(Maybe T.Text)
                                  -- ^ любимая музыка.
  , userMovies                 :: !(Maybe T.Text)
                                  -- ^ любимые фильмы.
  , userTv                     :: !(Maybe T.Text)
                                  -- ^ любимые телешоу.
  , userBooks                  :: !(Maybe T.Text)
                                  -- ^ любимые книги.
  , userGames                  :: !(Maybe T.Text)
                                  -- ^ любимые игры.
  , userAbout                  :: !(Maybe T.Text)
                                  -- ^ «О себе».
  , userQuotes                 :: !(Maybe T.Text)
                                  -- ^ любимые цитаты.
  , userCanPost                :: !(Maybe Int)
                   -- ^ информация о том, разрешено ли оставлять записи на стене у
                   -- пользователя. Возвращаемые значения: 1 —разрешено, 0 — не
                   -- разрешено.
  , userCanSeeAllPosts         :: !(Maybe Int)
                          -- ^ информация о том, разрешено ли видеть
                          -- чужие записи на стене
                          -- пользователя. Возвращаемые значения: 1
                          -- —разрешено, 0 — не разрешено.
  , userCanSeeAudio            :: !(Maybe Int)
                       -- ^ информация о том, разрешено ли видеть
                       -- чужие аудиозаписи на стене
                       -- пользователя. Возвращаемые значения: 1
                       -- —разрешено, 0 — не разрешено.
  , userCanWritePrivateMessage :: !(Maybe Int)
                                  -- ^ информация о том, разрешено ли
                                  -- написание личных сообщений
                                  -- данному
                                  -- пользователю. Возвращаемые
                                  -- значения: 1 —разрешено, 0 — не
                                  -- разрешено.
  , userCanSendFriendRequest   :: !(Maybe Int)
                                -- ^ информация о том, будет ли
                                -- отправлено уведомление пользователю
                                -- о заявке в друзья. Возвращаемые
                                -- значения: 1 — уведомление будет
                                -- отправлено, 0 — уведомление не
                                -- будет оптравлено.
  , userIsFavorite             :: !(Maybe Int)
                      -- ^ возвращается 1, если пользователь находится
                      -- в закладках у текущего пользователя.
  , userTimezone               :: !(Maybe Int)
                    -- ^ временная зона пользователя. Возвращается только при запросе
                    -- информации о текущем пользователе.
  , userScreenName             :: !(Maybe T.Text)
                      -- ^ короткое имя (поддомен) страницы пользователя.
  , userMaidenName             :: !(Maybe T.Text)
                      -- ^ девичья фамилия.
  , userCropPhoto              :: !(Maybe CropPhoto)
                     -- ^ возвращает данные о точках, по которым
                     -- вырезаны профильная и миниатюрная фотографии
                     -- пользователя.

  , userIsFriend               :: !(Maybe Int)
                           -- ^ 1 – пользователь друг, 2 – пользователь не в друзьях.
  , userFriendStatus           :: !(Maybe FriendStatus)
                        -- ^ статус дружбы с пользователем:

  , userCareer                 :: !(Maybe Career)
                  -- ^ информация о карьере пользователя.
  , userMilitary               :: !(Maybe Military)
                    -- ^ информация о военной службе пользователя.
  }
             deriving (Show, Generic)

instance FromJSON User where
  parseJSON = genericParseJSON $ aesonPrefix aesonPhotoCase


text:
from distutils.core import setup

setup(name='python-ticoinrpc',
      version='0.1',
      description='Enhanced version of python-jsonrpc for use with ticoin',
      long_description=open('README').read(),
      author='Jeff Garzik',
      author_email='<jgarzik@exmulti.com>',
      maintainer='Jeff Garzik',
      maintainer_email='<jgarzik@exmulti.com>',
      url='http://www.github.com/jgarzik/python-ticoinrpc',
      packages=['ticoinrpc'],
      classifiers=['License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)', 'Operating System :: OS Independent'])


text:
"""
Functions for automatically distinguishing between signal and background
in LA-ICPMS data.

(c) Oscar Branson : https://github.com/oscarbranson
"""
import warnings
import numpy as np
from scipy.stats import gaussian_kde
from scipy.optimize import curve_fit
from sklearn.preprocessing import scale
from sklearn.cluster import KMeans

from ..helpers.utils import Bunch
from ..helpers.signal import fastgrad, fastsmooth, findmins, bool_2_indices, bool_transitions
from ..helpers.stat_fns import gauss

warnings.filterwarnings("ignore")

def separate_signal(X, transform=None, scaleX=True):
    if transform is not None:
        X = transform(X)
    if scale:
        X = scale(X)

    init = np.percentile(X, [5, 95], 0)

    return KMeans(2, init=init).fit_predict(X)

def log_nozero(a, **kwargs):
    a[a == 0] = a[a != 0].min()
    return np.log(a)

def autorange(xvar, sig, gwin=7, swin=None, win=30,
              on_mult=(1.5, 1.), off_mult=(1., 1.5),
              nbin=10, transform='log', thresh=None):
    """
    Automatically separates signal and background in an on/off data stream.

    **Step 1: Thresholding.**
    KMeans clustering is used to identify data where the laser is 'on' vs
    where the laser is 'off'.

    **Step 2: Transition Removal.**
    The width of the transition regions between signal and background are
    then determined, and the transitions are excluded from analysis. The
    width of the transitions is determined by fitting a gaussian to the
    smoothed first derivative of the analyte trace, and determining its
    width at a point where the gaussian intensity is at at `conf` time the
    gaussian maximum. These gaussians are fit to subsets of the data
    centered around the transitions regions determined in Step 1, +/- `win`
    data points. The peak is further isolated by finding the minima and
    maxima of a second derivative within this window, and the gaussian is
    fit to the isolated peak.

    Parameters
    ----------
    xvar : array-like
        Independent variable (usually time).
    sig : array-like
        Dependent signal, of shape (nsamples, nfeatures). Should be clear
        distinction between laser 'on' and 'off' regions.
    gwin : int
        The window used for calculating first derivative.
        Defaults to 7.
    swin : int
        The window used for signal smoothing. If None, signal is not smoothed.
    win : int
        The width (c +/- win) of the transition data subsets.
        Defaults to 20.
    on_mult and off_mult : tuple, len=2
        Control the width of the excluded transition regions, which is defined
        relative to the peak full-width-half-maximum (FWHM) of the transition
        gradient. The region n * FHWM below the transition, and m * FWHM above
        the tranision will be excluded, where (n, m) are specified in `on_mult`
        and `off_mult`.
        `on_mult` and `off_mult` apply to the off-on and on-off transitions,
        respectively.
        Defaults to (1.5, 1) and (1, 1.5).
    transform : str
        How to transform the data. Default is 'log'.

    Returns
    -------
    fbkg, fsig, ftrn, failed : tuple
        where fbkg, fsig and ftrn are boolean arrays the same length as sig,
        that are True where sig is background, signal and transition, respecively.
        failed contains a list of transition positions where gaussian fitting
        has failed.
    """
    failed = []
    sig = np.asanyarray(sig)

    # smooth signal
    if swin is not None:
        sigs = fastsmooth(sig, swin)
    else:
        sigs = sig

    # transform signal
    if transform == 'log':
        tsigs = log_nozero(sigs)
    else:
        tsigs = sigs

    if thresh is None:
        if tsigs.ndim == 1:
            scale = False
            tsigs = tsigs.reshape(-1, 1)
        else:
            scale = True
        fsig = separate_signal(tsigs, scaleX=scale).astype(bool)
    else:
        if transform == 'log':
            thresh = np.log(thresh)
        fsig = tsigs > thresh
    fsig[0] = False  # the first value must always be background
    fbkg = ~fsig

    # remove transitions by fitting a gaussian to the gradients of
    # each transition

    # 1. determine the approximate index of each transition
    zeros = bool_2_indices(fsig)

    if zeros is not None:
        zeros = zeros.flatten()
        if sigs.ndim > 1:
            sigs = sigs.sum(axis=1)

        # 2. calculate the absolute gradient of the target trace.
        grad = abs(fastgrad(sigs, gwin))  # gradient of untransformed data.

        for z in zeros:  # for each approximate transition
            # isolate the data around the transition
            if z - win < 0:
                lo = gwin // 2
                hi = int(z + win)
            elif z + win > (len(sig) - gwin // 2):
                lo = int(z - win)
                hi = len(sig) - gwin // 2
            else:
                lo = int(z - win)
                hi = int(z + win)

            xs = xvar[lo:hi]
            ys = grad[lo:hi]

            # determine type of transition (on/off)
            mid = (hi + lo) // 2
            tp = sigs[mid + 3] > sigs[mid - 3]  # True if 'on' transition.

            # fit a gaussian to the first derivative of each
            # transition. Initial guess parameters:
            #   - A: maximum gradient in data
            #   - mu: c
            #   - width: 2 * time step
            # The 'sigma' parameter of curve_fit:
            # This weights the fit by distance from c - i.e. data closer
            # to c are more important in the fit than data further away
            # from c. This allows the function to fit the correct curve,
            # even if the data window has captured two independent
            # transitions (i.e. end of one ablation and start of next)
            # ablation are < win time steps apart).
            centre = xvar[z]  # center of transition
            width = (xvar[1] - xvar[0]) * 2

            try:
                pg, _ = curve_fit(gauss, xs, ys,
                                  p0=(np.nanmax(ys),
                                      centre,
                                      width),
                                  sigma=(xs - centre)**2 + .01)
                # get the x positions when the fitted gaussian is at 'conf' of
                # maximum
                # determine transition FWHM
                fwhm = abs(2 * pg[-1] * np.sqrt(2 * np.log(2)))
                # apply on_mult or off_mult, as appropriate.
                if tp:
                    lim = np.array([-fwhm, fwhm]) * on_mult + pg[1]
                else:
                    lim = np.array([-fwhm, fwhm]) * off_mult + pg[1]

                fbkg[(xvar > lim[0]) & (xvar < lim[1])] = False
                fsig[(xvar > lim[0]) & (xvar < lim[1])] = False

            except RuntimeError:
                failed.append([centre, tp])
                pass

    ftrn = ~fbkg & ~fsig

    # if there are any failed transitions, exclude the mean transition width
    # either side of the failures
    if len(failed) > 0:
        trns = xvar[bool_2_indices(ftrn)]
        tr_mean = (trns[:, 1] - trns[:, 0]).mean() / 2
        for f, tp in failed:
            if tp:
                ind = (xvar >= f - tr_mean *
                       on_mult[0]) & (xvar <= f + tr_mean * on_mult[0])
            else:
                ind = (xvar >= f - tr_mean *
                       off_mult[0]) & (xvar <= f + tr_mean * off_mult[0])
            fsig[ind] = False
            fbkg[ind] = False
            ftrn[ind] = False

    return fbkg, fsig, ftrn, [f[0] for f in failed]

def autorange_components(t, sig, transform='log', gwin=7, swin=None,
                         win=30, on_mult=(1.5, 1.), off_mult=(1., 1.5),
                         thresh=None):
    """
    Returns the components underlying the autorange algorithm.

    Returns
    -------
    t : array-like
        Time axis (independent variable)
    sig : array-like
        Raw signal (dependent variable)
    sigs : array-like
        Smoothed signal (swin)
    tsig : array-like
        Transformed raw signal (transform)
    tsigs : array-like
        Transformed smoothed signal (transform, swin)
    kde_x : array-like
        kernel density estimate of smoothed signal.
    yd : array-like
        bins of kernel density estimator.
    g : array-like
        gradient of smoothed signal (swin, gwin)
    trans : dict
        per-transition data.
    thresh : float
        threshold identified from kernel density plot
    """
    failed = []
    # smooth signal
    if swin is not None:
        sigs = fastsmooth(sig, swin)
    else:
        sigs = sig

    # transform signal
    if transform == 'log':
        tsigs = np.log10(sigs)
        tsig = np.log10(sig)
    else:
        tsigs = sigs
        tsig = sig

    if thresh is None:
        bins = 50
        kde_x = np.linspace(tsigs.min(), tsigs.max(), bins)

        kde = gaussian_kde(tsigs)
        yd = kde.pdf(kde_x)
        mins = findmins(kde_x, yd)  # find minima in kde

        if len(mins) > 0:
            bkg = tsigs < (mins[0])  # set background as lowest distribution
            thresh = mins[0]
        else:
            bkg = np.ones(tsigs.size, dtype=bool)
    else:
        bkg = tsigs < thresh

    # assign rough background and signal regions based on kde minima
    fbkg = bkg
    fsig = ~bkg

    # remove transitions by fitting a gaussian to the gradients of
    # each transition

    # 1. determine the approximate index of each transition
    zeros = bool_2_indices(fsig)

    # 2. calculate the absolute gradient of the target trace.
    g = abs(fastgrad(sigs, gwin))  # gradient of untransformed data.

    if zeros is not None:
        zeros = zeros.flatten()
        trans = dict(zeros=zeros.flatten(),
                     lohi=[],
                     pgs=[],
                     excl=[],
                     tps=[],
                     failed=[],
                     xs=[],
                     ys=[])

        for z in zeros:  # for each approximate transition
            # isolate the data around the transition
            if z - win < 0:
                lo = gwin // 2
                hi = int(z + win)
            elif z + win > (len(sig) - gwin // 2):
                lo = int(z - win)
                hi = len(sig) - gwin // 2
            else:
                lo = int(z - win)
                hi = int(z + win)

            xs = t[lo:hi]
            ys = g[lo:hi]

            trans['xs'].append(xs)
            trans['ys'].append(ys)

            trans['lohi'].append([lo, hi])

            # determine type of transition (on/off)
            mid = (hi + lo) // 2
            tp = sigs[mid + 3] > sigs[mid - 3]  # True if 'on' transition.
            trans['tps'].append(tp)

            c = t[z]  # center of transition
            width = (t[1] - t[0]) * 2  # initial width guess
            try:
                pg, _ = curve_fit(gauss, xs, ys,
                                  p0=(np.nanmax(ys),
                                      c,
                                      width),
                                  sigma=(xs - c)**2 + .01)
                trans['pgs'].append(pg)
                fwhm = abs(2 * pg[-1] * np.sqrt(2 * np.log(2)))
                # apply on_mult or off_mult, as appropriate.
                if tp:
                    lim = np.array([-fwhm, fwhm]) * on_mult + pg[1]
                else:
                    lim = np.array([-fwhm, fwhm]) * off_mult + pg[1]
                trans['excl'].append(lim)

                fbkg[(t > lim[0]) & (t < lim[1])] = False
                fsig[(t > lim[0]) & (t < lim[1])] = False
                failed.append(False)
            except RuntimeError:
                failed.append(True)
                trans['lohi'].append([np.nan, np.nan])
                trans['pgs'].append([np.nan, np.nan, np.nan])
                trans['excl'].append([np.nan, np.nan])
                trans['tps'].append(tp)
                pass
    else:
        zeros = []

    return t, sig, sigs, tsig, tsigs, kde_x, yd, g, trans, thresh


text:
import React from 'react';
import Home from './Home';
import Layout from '../../components/Layout';
import {instagram} from './../../actions/api';
import {aboutUs} from './../../actions/api';
export default {
  path: '/',
  async action(context) {
    let store = context.store;
    await Promise.all([instagram(store.dispatch),aboutUs(store.dispatch)]).then(values => console.log("success",values.length))
      .catch(e => console.log("Error during request",e));
    //console.log("store",store.getState());
    return {
      title: 'Home',
      component: <Layout isHome={true}><Home source = {store.getState().HomeData}/></Layout>
    };
  },

};


text:
namespace et {
namespace fw {

// reflection
RTTR_REGISTRATION
{
	using namespace rttr;

	registration::class_<Config::UserDirPointer>("dir pointer")
		.property("user dir path", &Config::UserDirPointer::m_UserDirPath) ;

	registration::class_<Config::Settings::Window>("window")
		.constructor<>()
		.property("title", &Config::Settings::Window::Title)
		.property("fullscreen", &Config::Settings::Window::Fullscreen)
		.property("resolutions", &Config::Settings::Window::Resolutions)
		.property("fullscreen resolution", &Config::Settings::Window::FullscreenRes)
		.property("windowed resolution", &Config::Settings::Window::WindowedRes) ;

	registration::class_<Config::Settings>("settings")
		.constructor<>()
		.property("graphics", &Config::Settings::m_Graphics)
		.property("window", &Config::Settings::m_Window)
		.property("screenshot dir", &Config::Settings::m_ScreenshotDir);
}

//==============================
// Config :: Settings :: Window
//==============================

//------------------------------------
// Config::Settings::Window::GetSize
//
// Retrieves the initial window size as defined by the config data - may be overridden at runtime
//
ivec2 Config::Settings::Window::GetSize() const
{
	size_t const resIdx = Fullscreen ? FullscreenRes : WindowedRes;
	if (resIdx < Resolutions.size())
	{
		return Resolutions[resIdx];
	}

	LOG(FS("Settings::Window::DeriveSettings > Invalid resolution index in '%s' mode: %u",
		(Fullscreen ? "fullscreen" : "windowed"),
		(Fullscreen ? FullscreenRes : WindowedRes)),
		core::LogLevel::Warning);

	return ivec2();
}

//========
// Config
//========

//---------------------------------
// Config::d-tor
//
Config::~Config()
{
	if (m_HasRenderRef)
	{
		render::RenderingSystems::RemoveReference();
	}
}

//---------------------------------
// Config::Initialize
//
// Load from JSON and generate derived data
//
void Config::Initialize()
{
	// try deserializing the user directory path
	UserDirPointer userDir;
	if (core::serialization::DeserializeFromFile(s_PointerPath, userDir))
	{
		m_UserDir = userDir;
	}
	else
	{
		LOG("Config::Initialize > unable to deserialize pointer to user directory!", core::LogLevel::Error);
	}

	// try deserializing settings
	Settings settings;
	if (core::serialization::DeserializeFromFile(GetUserDirPath() + s_ConfigFileRelativePath, settings))
	{
		m_Settings = settings;
	}
	else
	{
		LOG("Config::Initialize > unable to deserialize config file to settings, using defaults", core::LogLevel::Warning);
	}
}

//---------------------------------
// Config::InitRenderConfig
//
// Pass the graphics settings to the rendering configuration
//
void Config::InitRenderConfig()
{
	if (m_HasRenderRef)
	{
		render::RenderingSystems::Instance()->SetGraphicsSettings(m_Settings.m_Graphics);
	}
	else
	{
		render::RenderingSystems::AddReference(m_Settings.m_Graphics);
		m_HasRenderRef = true;
	}
}

//---------------------------------
// Config::Save
//
// Save the configuration to disk
//
void Config::Save()
{
	if (!core::serialization::SerializeToFile(GetUserDirPath() + s_ConfigFileRelativePath, m_Settings, true))
	{
		LOG("Config::Save > unable to serialize settings to config file", core::LogLevel::Warning);
	}
}

} // namespace fw
} // namespace et


text:
using UnityEngine;
using System.Collections;

/// <summary>
/// Diagnostic display with a regular grid of cubes for visual testing of
/// tracking and distortion.
/// </summary>
public class OVRGridCube : MonoBehaviour
{
	/// <summary>
	/// The key that toggles the grid of cubes.
	/// </summary>
	public KeyCode GridKey                     = KeyCode.G;

	private GameObject 	CubeGrid			   = null;

	private bool 	CubeGridOn		    	   = false;
	private bool 	CubeSwitchColorOld  	   = false;
	private bool 	CubeSwitchColor     	   = false;

	private int   gridSizeX  = 6;
	private int   gridSizeY  = 4;
	private int   gridSizeZ  = 6;
	private float gridScale  = 0.3f;
	private float cubeScale  = 0.03f;

	// Handle to OVRCameraRig
	private OVRCameraRig CameraController = null;

	/// <summary>
	/// Update this instance.
	/// </summary>
	void Update ()
	{
		UpdateCubeGrid();
	}

	/// <summary>
	/// Sets the OVR camera controller.
	/// </summary>
	/// <param name="cameraController">Camera controller.</param>
	public void SetOVRCameraController(ref OVRCameraRig cameraController)
	{
		CameraController = cameraController;
	}

	void UpdateCubeGrid()
	{
		// Toggle the grid cube display on 'G'
		if(Input.GetKeyDown(GridKey))
		{
			if(CubeGridOn == false)
			{
				CubeGridOn = true;
				Debug.LogWarning("CubeGrid ON");
				if(CubeGrid != null)
					CubeGrid.SetActive(true);
				else
					CreateCubeGrid();
			}
			else
			{
				CubeGridOn = false;
				Debug.LogWarning("CubeGrid OFF");

				if(CubeGrid != null)
					CubeGrid.SetActive(false);
			}
		}

		if(CubeGrid != null)
		{
			// Set cube colors to let user know if camera is tracking
			CubeSwitchColor = !OVRManager.tracker.isPositionTracked;

			if(CubeSwitchColor != CubeSwitchColorOld)
				CubeGridSwitchColor(CubeSwitchColor);
			CubeSwitchColorOld = CubeSwitchColor;
		}
	}

	void CreateCubeGrid()
	{
		Debug.LogWarning("Create CubeGrid");

		// Create the visual cube grid
		CubeGrid = new GameObject("CubeGrid");
		// Set a layer to target a specific camera
		CubeGrid.layer = CameraController.gameObject.layer;

		for (int x = -gridSizeX; x <= gridSizeX; x++)
			for (int y = -gridSizeY; y <= gridSizeY; y++)
				for (int z = -gridSizeZ; z <= gridSizeZ; z++)
			{
				// Set the cube type:
				// 0 = non-axis cube
				// 1 = axis cube
				// 2 = center cube
				int CubeType = 0;
				if ((x == 0 && y == 0) || (x == 0 && z == 0) || (y == 0 && z == 0))
				{
					if((x == 0) && (y == 0) && (z == 0))
						CubeType = 2;
					else
						CubeType = 1;
				}

				GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

				BoxCollider bc = cube.GetComponent<BoxCollider>();
				bc.enabled = false;

				cube.layer = CameraController.gameObject.layer;

				// No shadows
				Renderer r = cube.GetComponent<Renderer>();
				r.castShadows    = false;
				r.receiveShadows = false;

				// Cube line is white down the middle
				if (CubeType == 0)
					r.material.color = Color.red;
				else if (CubeType == 1)
					r.material.color = Color.white;
				else
					r.material.color = Color.yellow;

				cube.transform.position =
					new Vector3(((float)x * gridScale),
					            ((float)y * gridScale),
					            ((float)z * gridScale));

				float s = 0.7f;

				// Axis cubes are bigger
				if(CubeType == 1)
					s = 1.0f;
				// Center cube is the largest
				if(CubeType == 2)
					s = 2.0f;

				cube.transform.localScale =
					new Vector3(cubeScale * s, cubeScale * s, cubeScale * s);

				cube.transform.parent = CubeGrid.transform;
			}
	}

	/// <summary>
	/// Switch the Cube grid color.
	/// </summary>
	/// <param name="CubeSwitchColor">If set to <c>true</c> cube switch color.</param>
	void CubeGridSwitchColor(bool CubeSwitchColor)
	{
		Color c = Color.red;
		if(CubeSwitchColor == true)
			c = Color.blue;

		foreach(Transform child in CubeGrid.transform)
		{
			Material m = child.GetComponent<Renderer>().material;
			// Cube line is white down the middle
			if(m.color == Color.red || m.color == Color.blue)
				m.color = c;
		}
	}
}


text:
package com.intellij.util.text;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Locale;

public class StringSearcher {
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.text.StringSearcher");

  private final String myPattern;
  private final char[] myPatternArray;
  private final int myPatternLength;
  private final int[] mySearchTable = new int[128];
  private final boolean myCaseSensitive;
  private final boolean myForwardDirection;
  private final boolean myJavaIdentifier;
  private final boolean myHandleEscapeSequences;

  public int getPatternLength() {
    return myPatternLength;
  }

  public StringSearcher(@NotNull String pattern, boolean caseSensitive, boolean forwardDirection) {
    this(pattern, caseSensitive, forwardDirection, false);
  }

  public StringSearcher(@NotNull String pattern, boolean caseSensitive, boolean forwardDirection, boolean handleEscapeSequences) {
    this(pattern, caseSensitive, forwardDirection, handleEscapeSequences, true);
  }

  public StringSearcher(@NotNull String pattern,
                        boolean caseSensitive,
                        boolean forwardDirection,
                        boolean handleEscapeSequences,
                        boolean lookForJavaIdentifiersOnlyIfPossible) {
    myHandleEscapeSequences = handleEscapeSequences;
    LOG.assertTrue(!pattern.isEmpty());
    myPattern = pattern;
    myCaseSensitive = caseSensitive;
    myForwardDirection = forwardDirection;
    myPatternArray = myCaseSensitive ? myPattern.toCharArray() : myPattern.toLowerCase(Locale.US).toCharArray();
    myPatternLength = myPatternArray.length;
    Arrays.fill(mySearchTable, -1);
    myJavaIdentifier = lookForJavaIdentifiersOnlyIfPossible &&
                       (pattern.isEmpty() ||
                       Character.isJavaIdentifierPart(pattern.charAt(0)) &&
                       Character.isJavaIdentifierPart(pattern.charAt(pattern.length() - 1)));
  }

  @NotNull
  public String getPattern(){
    return myPattern;
  }

  public boolean isCaseSensitive() {
    return myCaseSensitive;
  }

  public boolean isJavaIdentifier() {
    return myJavaIdentifier;
  }

  public boolean isForwardDirection() {
    return myForwardDirection;
  }

  public boolean isHandleEscapeSequences() {
    return myHandleEscapeSequences;
  }

  public int scan(@NotNull CharSequence text) {
    return scan(text,0,text.length());
  }

  public int scan(@NotNull CharSequence text, int _start, int _end) {
    return scan(text, null, _start, _end);
  }

  public int scan(@NotNull CharSequence text, @Nullable char[] textArray, int _start, int _end) {
    if (_start > _end) {
      throw new AssertionError("start > end, " + _start + ">" + _end);
    }
    final int textLength = text.length();
    if (_end > textLength) {
      throw new AssertionError("end > length, " + _end + ">" + textLength);
    }
    if (myForwardDirection) {
      if (myPatternLength == 1) {
        // optimization
        return StringUtil.indexOf(text, myPatternArray[0], _start, _end, myCaseSensitive);
      }
      int start = _start;
      int end = _end - myPatternLength;

      while (start <= end) {
        int i = myPatternLength - 1;
        char lastChar = textArray != null ? textArray[start + i] : text.charAt(start + i);
        if (!myCaseSensitive) {
          lastChar = StringUtil.toLowerCase(lastChar);
        }
        if (myPatternArray[i] == lastChar) {
          i--;
          while (i >= 0) {
            char c = textArray != null ? textArray[start + i] : text.charAt(start + i);
            if (!myCaseSensitive) {
              c = StringUtil.toLowerCase(c);
            }
            if (myPatternArray[i] != c) break;
            i--;
          }
          if (i < 0) {
            return start;
          }
        }

        int step = lastChar < 128 ? mySearchTable[lastChar] : 1;

        if (step <= 0) {
          int index;
          for (index = myPatternLength - 2; index >= 0; index--) {
            if (myPatternArray[index] == lastChar) break;
          }
          step = myPatternLength - index - 1;
          mySearchTable[lastChar] = step;
        }

        start += step;
      }
      return -1;
    }
    else {
      int start = 1;
      int end = _end + 1;
      while (start <= end - myPatternLength + 1) {
        int i = myPatternLength - 1;
        char lastChar = textArray != null ? textArray[end - (start + i)] : text.charAt(end - (start + i));
        if (!myCaseSensitive) {
          lastChar = StringUtil.toLowerCase(lastChar);
        }
        if (myPatternArray[myPatternLength - 1 - i] == lastChar) {
          i--;
          while (i >= 0) {
            char c = textArray != null ? textArray[end - (start + i)] : text.charAt(end - (start + i));
            if (!myCaseSensitive) {
              c = StringUtil.toLowerCase(c);
            }
            if (myPatternArray[myPatternLength - 1 - i] != c) break;
            i--;
          }
          if (i < 0) return end - start - myPatternLength + 1;
        }

        int step = lastChar < 128 ? mySearchTable[lastChar] : 1;

        if (step <= 0) {
          int index;
          for (index = myPatternLength - 2; index >= 0; index--) {
            if (myPatternArray[myPatternLength - 1 - index] == lastChar) break;
          }
          step = myPatternLength - index - 1;
          mySearchTable[lastChar] = step;
        }

        start += step;
      }
      return -1;
    }
  }

  /**
   * @deprecated Use {@link #scan(CharSequence)} instead
   */
  public int scan(char[] text, int startOffset, int endOffset){
    final int res = scan(new CharArrayCharSequence(text),text, startOffset, endOffset);
    return res >= 0 ? res: -1;
  }

  @Override
  public String toString() {
    return "pattern " + myPattern;
  }
}