text:
  <ng-form name="forms.cube_measure_form">

 0">

                          Name
                          Expression
                          Parameters
                          Return Type
                          Actions

                              {{measure.name}}

                              {{measure.function.expression}}

                                <parametertree ng-if="measure.function.parameter!=null && measure.function.expression!=='TOP_N'" nextpara="measure.function.parameter"></parametertree>
                                <topntree ng-if="measure.function.parameter!=null && measure.function.expression=='TOP_N'" nextpara="measure.function.parameter"></topntree>

                              {{measure.function.returntype}}

                              <button class="btn btn-xs btn-info" ng-click="addNewMeasure(measure)">

                              </button>

                              <button class="btn btn-xs  btn-danger" ng-click="removeElement(cubeMetaFrame.measures, measure)">

                              </button>

  </ng-form>

      <button class="btn btn-sm btn-info" ng-click="addNewMeasure()" ng-show="state.mode=='edit' && !newMeasure">
           Measure
      </button>

  <ng-form name="edit_mes_form">

Edit Measure

                              Name

                                  <input type="text" placeholder="Name.." class="form-control"
                                      tooltip="measure name.." tooltip-trigger="focus"
                                      ng-model="newMeasure.name" required />

                              Expression

                                  <select class="form-control"
                                      ng-init="newMeasure.function.expression = (!!newMeasure.function.expression)?newMeasure.function.expression:cubeConfig.dftSelections.measureExpression" chosen ng-model="newMeasure.function.expression" required
                                      ng-change="measureReturnTypeUpdate();"
                                      ng-options="me as me for me in cubeConfig.measureExpressions">
                                      <option value=""></option>
                                  </select>

                              Param Type

                                  <select class="form-control" ng-if="newMeasure.function.expression != 'COUNT'"
                                      ng-init="newMeasure.function.parameter.type=(!!newMeasure.function.parameter.type)?newMeasure.function.parameter.type: 'column' "
                                      chosen ng-model="newMeasure.function.parameter.type" required
                                      ng-change="measureReturnTypeUpdate();"
                                      ng-options="mpt as mpt for mpt in cubeConfig.measureParamType">
                                      <option value=""></option>
                                  </select>
                                    constant

                              Param Value

                                    1

                                <select class="form-control" chosen
                                        ng-if="newMeasure.function.parameter.type == 'column' && newMeasure.function.expression!=='COUNT_DISTINCT'&& newMeasure.function.expression!=='TOP_N'"
                                        ng-model="newMeasure.function.parameter.value"
                                        ng-change="measureReturnTypeUpdate();"
                                        ng-options="column as column for column in getCommonMetricColumns()" >
                                  <option value="">-- Select a Column --</option>
                                </select>

                                <select class="form-control" chosen
                                        ng-if="newMeasure.function.parameter.type == 'column' && (newMeasure.function.expression=='COUNT_DISTINCT' || newMeasure.function.expression=='TOP_N')"
                                        ng-model="newMeasure.function.parameter.value"
                                        ng-change="measureReturnTypeUpdate();"
                                        ng-options="column as column for column in getMetricColumns()" >
                                  <option value="">-- Select a Column --</option>
                                </select>

                              Return Type

                                  <select class="form-control"
                                      ng-if="newMeasure.function.expression == 'COUNT_DISTINCT'"
                                      ng-init="newMeasure.function.returntype = (!!newMeasure.function.returntype)?newMeasure.function.returntype:cubeConfig.dftSelections.distinctDataType.value"
                                      chosen ng-model="newMeasure.function.returntype" required
                                      ng-options="ddt.value as ddt.name for ddt in cubeConfig.distinctDataTypes">
                                      <option value=""></option>
                                  </select>
                                <select class="form-control"
                                        ng-if="newMeasure.function.expression == 'TOP_N'"
                                        ng-init="newMeasure.function.returntype = (!!newMeasure.function.returntype)?newMeasure.function.returntype:cubeConfig.dftSelections.topN.value"
                                        chosen ng-model="newMeasure.function.returntype" required
                                        ng-options="ddt.value as ddt.name for ddt in cubeConfig.topNTypes">
                                  <option value=""></option>
                                </select>
                                    {{newMeasure.function.returntype | uppercase}}

                                Type
                                Value

                                {{n_parameter.type}}
                                {{n_parameter.value}}

                                  <button class="btn btn-xs btn-info" ng-click="editNextParameter(n_parameter)">

                                  </button>
                                  <button class="btn btn-xs btn-info" ng-click="removeParameter(nextParameters, $index)">
                                  </button>

                                Order By Column
                                {{n_parameter.value}}

                                  <button class="btn btn-xs btn-info" ng-click="editNextParameter(n_parameter)">

                                  </button>
                                  <button class="btn btn-xs btn-info" ng-click="removeParameter(nextParameters, $index)">
                                  </button>

                            <button class="btn btn-sm btn-info" ng-click="addNextParameter()" ng-show="newMeasure.function.expression != 'TOP_N'"
                                    ng-show="state.mode=='edit'">Parameter
                            </button>
                            <button class="btn btn-sm btn-info" ng-click="addNextParameter()" ng-show="newMeasure.function.expression == 'TOP_N' && nextParameters.length==0"
                                    ng-show="state.mode=='edit'"> Order by Column
                            </button>

Tips

All cubes have to contain one measure for Count(1), suggest use "_Count_" as name (Has been generated automatically)

Only accept single column in param value with "Column" type

Distinct Count is approximate, please indicate Error Rate, higher accuracy degree accompanied with larger storage size and longer build time

          <button class="btn btn-sm btn-info" ng-disabled="edit_mes_form.$invalid"
                  ng-click="saveNewMeasure()" ng-show="state.mode=='edit'">OK</button>
          <button class="btn btn-link" ng-click="clearNewMeasure()">Cancel</button>

  </ng-form>


text:
package test.process;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.snaker.engine.entity.Process;
import org.snaker.engine.helper.StreamHelper;
import org.snaker.engine.test.TestSnakerBase;

/**
 * @author yuqs
 * @version 1.0
 */
public class TestProcess extends TestSnakerBase {
	@Test
	public void test() {
		processId = engine.process().deploy(StreamHelper.
				getStreamFromClasspath("test/task/simple/process.snaker"));
		Process process = engine.process().getProcessById(processId);
		System.out.println("output 1="+process);
		process = engine.process().getProcessByVersion(process.getName(), process.getVersion());
		System.out.println("output 2="+process);
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("task1.operator", "1");
		engine.startInstanceById(processId, "1", args);
		engine.process().undeploy(processId);
		//engine.startInstanceById(processId, "1", args);
	}
}

redpajama_set_name: RedPajamaGithub


text:
class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :npi

      t.timestamps
    end

    add_index :users, :name, unique: true
    add_index :users, :npi, unique: true
  end
end


text:
var searchData=
[
  ['temperaturehistory',['TemperatureHistory',['../d7/d99/interface_temperature_history.html',1,'']]],
  ['temperaturelist',['TemperatureList',['../dc/def/interface_temperature_list.html',1,'']]],
  ['temperaturereceiveddelegate_2dp',['temperatureReceivedDelegate-p',['../d4/dc0/protocoltemperature_received_delegate-p.html',1,'']]],
  ['temperatureview',['TemperatureView',['../d9/d42/interface_temperature_view.html',1,'']]],
  ['tempertureentry',['TempertureEntry',['../de/d13/interface_temperture_entry.html',1,'']]],
  ['threadednotification',['ThreadedNotification',['../dd/d8d/interface_threaded_notification.html',1,'']]],
  ['threedconfig',['ThreeDConfig',['../d6/d80/interface_three_d_config.html',1,'']]],
  ['threedcontainer',['ThreeDContainer',['../db/d1d/interface_three_d_container.html',1,'']]],
  ['threedmodel',['ThreeDModel',['../dd/d90/interface_three_d_model.html',1,'']]],
  ['threedview',['ThreeDView',['../d4/dda/interface_three_d_view.html',1,'']]]
];


text:
import matplotlib.pyplot as plt
from pylab import *

#### GENERAL IMPORTS ###
import os
import numpy as np
import scipy
import scipy.optimize
import scipy.stats
import scipy.signal
import math
import copy
#from scikits.statsmodels.sandbox.regression.numdiff import approx_hess3 as approx_hess
from statsmodels.tools.numdiff import approx_hess

### own imports
import generaltools as gt
import posterior
import powerspectrum

### global variables ####
logmin = -100.0

#### AUXILIARY FUNCTIONS ############################################

#### MAKE A SIMPLE LINEAR FUNCTION
#
# f(x) = a*x + b
# a = slope
# b = intercept
#
#
def straight(freq, a, b):
    return (a*np.array(freq) + b)

def sigmoid(z, a,b,c,d):
    return a*z + b/(c + np.exp(-d*z))

def const(freq, a):
    return np.array([np.exp(a) for x in freq])

#### FAST-RISE EXPONENTIAL DECAY (FRED) PROFILE
#
# useful for light curves, not strictly for MLE fitting
#
# f(x) = a*exp(2*t1/t2)**(1/2) * exp(-t1/(time-ts) - (time-ts)/t2)
# a = normalization
# ts = pulse start time
# t1 = characteristic of burst rise
# t2 = characteristic of burst decay
def fred(time, a, tau1, tau2, c = 0.0, t0 = 0.5):

    #print("a: " + str(a))
    #print("tau1: " + str(tau1))
    #print("tau2: " + str(tau2))
    #print("len time: " + str(len(time)))
    tdiff = time - (time[0]) - t0
    #print("len tdiff: " + str(len(tdiff)))
    dt = tdiff[1]-tdiff[0]
    ts = stepfun(time, time[0]+t0+dt, max(time)+dt)
    #tdiff = time - t0
    #print("tdiff: " + str(tdiff[0]))
    e1 = (-tau1/(tdiff)) - ((tdiff)/tau2)
    e2 = (np.exp(2.0*(tau1/tau2)**0.5))
    counts = a*np.exp(e1)*e*ts + c
    cmod = []
    for co in counts:
        if isnan(co) or co == np.inf:
           cmod.append(c)
        else:
           cmod.append(co)
    #print('funcval in FRED: ' + str(counts))

    return cmod

def envelope(x, tstart, tend, trise, tfall, amplitude):

    rexp = -trise/(x - tstart)
    fexp = tfall/(x - tend)

    res = amplitude*np.exp(rexp + fexp)

    #xsind = np.array(x).searchsorted(tstart)-1
    #xeind = np.array(x).searchsorted(tend)+1
    #xnew = x[xsind:xeind]
    #xnew = xnew[1:] - xnew[0]

    #rexp = -trise/(xnew)
    #fexp = tfall/(xnew)

    #resb = amplitude*np.exp(rexp + fexp)
    #res = np.zeros(len(x))
    #res[xsind+1:xeind] = resb

    return res

def combfred(x, norm1, norm2, norm3, norm4, tau11, tau12, tau13, tau14, tau21, tau22, tau23, tau24, t01, t02, t03, t04, c):

        fred1 = fred(x, norm1, tau11, tau21, 1.0, t01)
        fred2 = fred(x, norm2, tau12, tau22, 1.0, t02)
        fred3 = fred(x, norm3, tau13, tau23, 1.0, t03)
        fred4 = fred(x, norm4, tau14, tau24, 1.0, t04)
        fredsum = np.array(np.array(fred1) + np.array(fred2) + np.array(fred3) + np.array(fred4))# + c

        return fredsum

def stepfun(x, a, b):
    y = np.zeros(len(x))
    x = np.array(x)
    minind = x.searchsorted(a)
    maxind = x.searchsorted(b)
    y[minind:maxind] = 1.0
    return y

### LORENTZIAN PROFILE ######################
#
# Lorentzian Profile
#
# gamma: width parameter
# norm: normalization
# x0: location of centroid
def lorentzian(x, gamma, norm, x0):
    l = norm*(1.0/np.pi)*0.5*gamma/((x-x0)**2.0 + (0.5*gamma)**2.0)
    return l

def gaussian(x, mean, scale, norm, c=0.0):
    norm = np.exp(norm)
    c = np.exp(c)
    g = norm*np.exp(-(x-mean)**2.0/(2.0*scale**2.0)) + c
    return g

#### POWER LAW ########
#
# f(x) = b*x**a + c
# a = power law index
# b = normalization (LOG)
# c = white noise level (LOG), optional
#
def pl(freq, a, b, c=None):
    res = -a*np.log(freq) + b
    if c:
        return (np.exp(res) + np.exp(c))
    else:
        return np.exp(res)

### POWER LAW DERIVATIVE
# Derivative of the likelihood function for a power law profile
# Probably not correct.
#
# DON'T USE!
#
def ml_pl_prime(freq, a, b, c):
    aprime = -(b*np.log(freq)*((c-1.0)*(freq**a) + b))/(c*freq**-a + b)**2.0
    bprime = ((c-1.0)*freq**a + b)/(c*freq**a + b)**2.0
    cprime = ((freq**2)*((c-1)*(freq**2.0)*b))/(c*freq**2.0 + b)**2.0
    return aprime, bprime, cprime

#### Lorentzian Profile for QPOs
#
# f(x) = (a*b/(2*pi))/((x-c)**2 + a**2)
#
# a = full width half maximum
# b = log(normalization)
# c = centroid frequency
# d = log(noise level) (optional)
#
def qpo(freq, a, b, c, d=None):

    gamma = np.exp(a)
    norm = np.exp(b)
    nu0 = c

    alpha = norm*gamma/(math.pi*2.0)
    y = alpha/((freq - nu0)**2.0 + gamma**2.0)

    if d:
        y = y + np.exp(d)

    return y

### auxiliary function that makes a Lorentzian with a fixed centroid frequency
### needed for QPO search algorithm
def make_lorentzians(x):
   ### loop creates many function definitions lorentz, each differs only by the value
   ### of the centroid frequency f used in computing the spectrum
   for f in x:
       def create_my_func(f):
           def lorentz(x, a, b, e):
               result = qpo(x, a, b, f, e)
               return result
           return lorentz
       yield(create_my_func(f))

def plqpo(freq, plind, beta, noise,a, b, c, d=None):
#def plqpo(freq, plind, beta, noise,a, b, d=None):

    #c = 93.0943061934
    powerlaw = pl(freq, plind, beta, noise)
    quasiper = qpo(freq, a, b, c, d)
    return powerlaw+quasiper

def bplqpo(freq, lplind, beta, hplind, fbreak, noise, a, b, c, d=None):
#def bplqpo(freq, lplind, beta, hplind, fbreak, noise, a, b,d=None):

    #c = 93.0943061934
    powerlaw = bpl(freq, lplind, beta, hplind, fbreak, noise)
    quasiper = qpo(freq, a, b, c, d)
    return powerlaw+quasiper

###  BENT POWER LAW
# f(x) = (a[1]*x**a[0])/(1.0 + (x/a[3])**(a[2]-a[0]))+a[4])
# a = low-frequency index, usually between 0 and 1
# b = log(normalization)
# c = high-frequency index, usually between 1 and 4
# d = log(frequency where model bends)
# e = log(white noise level) (optional)
# f = smoothness parameter

#def bpl(freq, a, b, c, d, f=-1.0, e=None):
def bpl(freq, a, b, c, d, e=None):

    ### compute bending factor
    logz = (c - a)*(np.log(freq) - d)

    ### be careful with very large or very small values
    logqsum = sum(np.where(logz<-100, 1.0, 0.0))
    if logqsum > 0.0:
        logq = np.where(logz<-100, 1.0, logz)
    else:
        logq = logz
    logqsum = np.sum(np.where((-100<=logz) & (logz<=100.0), np.log(1.0 + np.exp(logz)), 0.0))
    if logqsum > 0.0:
        logqnew = np.where((-100<=logz) & (logz<=100.0), np.log(1.0 + np.exp(logz)), logq)
    else:
        logqnew = logq

    logy = -a*np.log(freq) - logqnew + b

#    logy = -a*np.log(freq) + f*logqnew + b
    if e:
        y = np.exp(logy) + np.exp(e)
    else:
        y = np.exp(logy)
    return y

# f(x) = (a[1]*x**a[0])/(1.0 + (x/a[3])**(a[2]-a[0]))+a[4])
# a = low-frequency index, usually between 0 and 1
# b = log(normalization)
# c = high-frequency index, usually between 1 and 4
# d = log(frequency where model bends)
# e = log(white noise level) (optional)
# f = smoothness parameter

#def bpl(freq, a, b, c, d, f=-1.0, e=None):
def bpl2(freq, a, b, c, d, e=None):

    ### compute bending factor
    logz = (c - a)*(np.log(freq) - d)

    ### be careful with very large or very small values
    logqsum = sum(np.where(logz<-100, 1.0, 0.0))
    if logqsum > 0.0:
        logq = np.where(logz<-100, 1.0, logz)
    else:
        logq = logz
    logqsum = np.sum(np.where((-100<=logz) & (logz<=100.0), np.log(1.0 + np.exp(logz)), 0.0))
    if logqsum > 0.0:
        logqnew = np.where((-100<=logz) & (logz<=100.0), np.log(1.0 + np.exp(logz)), logq)
    else:
        logqnew = logq

    logy = -a*np.log(freq) + logqnew + b

#    logy = -a*np.log(freq) + f*logqnew + b
    if e:
        y = np.exp(logy) + np.exp(e)
    else:
        y = np.exp(logy)
    return y

### FIT CONSTRAINED BENT POWER LAW
# f(x) = (a[1]*x**a[0])/(1.0 + (x/a[3])**(a[2]-a[0]))+a[4])
# a = low-frequency index, MANUALLY SET TO -1
# b = log(normalization)
# c = high-frequency index, usually between 1 and 4
# d = log(frequency where model bends)
# e = log(white noise level)

def cbpl(freq, c, b, d, e):

    ### compute bending factor
    logz = (1.0 - c)*(np.log(freq) - np.log(d))

    logqsum = sum(np.where(logz<-16, 1.0, 0.0))
    if logqsum > 0.0:
        logq = np.where(logz<-16, 1.0, logz)
    else:
        logq = logz
    logqsum = np.sum(np.where((-16<=logz) & (logz<=16.0), np.log(1.0 + np.exp(logz)), 0.0))
    if logqsum > 0.0:
        logqnew = np.where((-16<=logz) & (logz<=16.0), np.log(1.0 + np.exp(logz)), logq)
    else:
        logqnew = logq

    y = np.exp(-1.0*np.log(freq) - logqnew + b) + np.exp(e)

    return y

#### COMBINE FUNCTIONS INTO A NEW FUNCTION
#
# This function will return a newly created function,
# combining all functions giving in *funcs
#
# *funcs should be tuples of (function name, no. of parameters)
# where the number of parameters is that of the function minus the
# x-coordinate ('freq').
#
#
# **kwargs should only really have one keyword:
# mode = 'add'
# which defines whether the model components will be added ('add')
# or multiplied ('multiply').
# By default, if nothing is given, components will be added
#
#
# NOTE: When calling combmod, make sure you put in the RIGHT NUMBER OF
#       PARAMETERS and IN THE RIGHT ORDER!
#
#
# Example:
# - make a combined power law and QPO model, multiplying components together.
#   The power law includes white noise (3 parameters, otherwise two), the
#   QPO model doesn't (3 parameters, otherwise 4):
#   >>> combmod = combine_models((pl, 3), qpo(3), mode='multiply')
#
#
def combine_models(*funcs, **kwargs):

    ### assert that keyword 'mode' is given in function call
    try:
        assert kwargs.has_key('mode')
    ### if that's not true, catch Assertion error and manually set mode = 'add'
    except AssertionError:
        kwargs["mode"] = 'add'

    ### tell the user what mode the code is using, exit if mode not recognized
    if kwargs["mode"]  == 'add':
        print("Model components will be added.")
    elif kwargs['mode'] == 'multiply':
        print('Model components will be multiplied.')
    else:
        raise Exception("Operation on model components not recognized.")

    ### this is the combined function returned by combined_models
    ### 'freq': x-coordinate of the model
    ### '*args': model parameters
    def combmod(freq, *args):
        ### create empty list for result of the model
        res = np.zeros(len(freq))

        ### initialize the parameter count to make sure the right parameters
        ### go into the right function
        parcount = 0

        ### for each function, compute f(x) and add or multiply the result with the previous iteration
        for i,x in enumerate(funcs):
           funcargs = args[parcount:parcount+int(x[1])]
           if kwargs['mode'] == 'add':
               res = res + x[0](freq, *funcargs)
           elif kwargs['mode'] == 'multiply':
               res = res * x[0](freq, *funcargs)
           parcount = parcount + x[1]
        return res
    return combmod

## MAXIMUM LIKELIHOOD FUNCTION
# Not sure I need this!
def maxlike(freq, power, func, pars):
    funcval = func(freq, *pars)
    res = np.sum(np.log(funcval))+ np.sum(power/funcval)
    if res == inf or np.isnan(res):
        res = 0.0
    return res

#### ANALYTIC EXPRESSION FOR THE HESSIAN FOR THE POWER LAW FITTING
#
# This function computes the Hessian (second derivative tensor) of the
# maximum likelihood function of the power law model.
# I can use this to cross-check results from numerical
# estimation of the covariance matrix (=inverse of the hessian).
#
# This function returns the Hessian matrix.
#
#
def pl_covariance(freq, power, pars):

    ### power law index
    b = pars[0]
    ### normalization
    a = np.exp(pars[1])
    ### noise level
    c = np.exp(pars[2])

    exppos = freq**b
    expneg = freq**(-b)
    exptwo = freq**(2*b)
    ### denominator appearing often:
    denoma = a*exppos + c
    denomc = a + c*exppos
    funcval = a*expneg + c

    logfreq = np.log(freq)

    ### d^2L/da^2
    datwo = np.sum((2.0*power*exptwo/denoma**2.0) - 1.0/(denomc**2.0))

    ### d^2L/db^2
    dbout = a*logfreq**2.0
    dbfirst = power*(a*exptwo - c*exppos)/denoma**3.0
    dbsecond = c*exppos/denomc**2.0

    dbtwo = np.sum(dbout*(dbfirst + dbsecond))

    ### d^2L/dc^2
    dctwo = np.sum((2.0*power/denoma**3.0) - 1.0/funcval**2.0)

    ### d^2L/dadb

    dadbfirst = power*exppos/denoma
    dadbcurly = (2.0*power*exptwo/denoma**3.0) + 1.0/denomc**2.0
    dadblast = 1.0/denomc

    dadb = np.sum(logfreq*(-dadbfirst + a*dadbcurly - dadblast))

    ### d^2L/dadc
    dadc = np.sum(exppos*((2.0*power/denoma**3.0) - 1.0/denomc**2.0))

    ### d^2L/dbdc
    dbdcfactor = a*expneg*logfreq
    dbdcrest = (2.0*power*exptwo/denoma**3.0) + 1.0/funcval**2.0

    dbdc = np.sum(dbdcfactor*dbdcrest)

    ### assemble Hessian
    hess = [[datwo, dadb, dadc],[dadb, dbtwo, dbdc],[dadb, dbdc, dctwo]]

    return hess

#### CLASS THAT FITS POWER SPECTRA USING MLE ##################
#
# This class provides functionality for maximum likelihood fitting
# of periodogram data to a set of models defined above.
#
# It draws heavily on the various optimization routines provided in
# scipy.optimize, and additionally has the option to use R functionality
# via rPy and a given set of functions defined in an R-script.
#
# Note that many different optimization routines are available, and not all
# may be appropriate for a given problem.
# Constrained optimization is available via the constrained BFGS and TNC routines.
#
#
class MaxLikelihood(object):

    ### x = x-coordinate of data
    ### y = y-coordinate of data
    ### obs= if True, compute covariances and print summary to screen
    ###
    ###  fitmethod = choose optimization method
    ###  options are:
    ### 'simplex': use simplex downhill algorithm
    ### 'powell': use modified Powell's algorithm
    ### 'gradient': use nonlinear conjugate gradient
    ### 'bfgs': use BFGS algorithm
    ### 'newton': use Newton CG
    ### 'leastsq' : use least-squares method
    ### 'constbfgs': constrained BFGS algorithm
    ### 'tnc': constrained optimization via a truncated Newton algorithm
    ### 'nlm': optimization via R's non-linear minimization routine
    ### 'anneal': simulated annealing for convex problems
    def __init__(self, x, y, obs=True, fitmethod='powell'):

        ### save power spectrum in attributes
        self.x= x
        self.y= y
        ### Is this a real observation or a fake periodogram to be fitted?
        self.obs = obs

        self.nlmflag = False

        MaxLikelihood._set_fitmethod(self, fitmethod)

    def _set_fitmethod(self, fitmethod):
        ### select fitting method
        if fitmethod.lower() in ['simplex']:
            self.fitmethod = scipy.optimize.fmin
        elif fitmethod.lower() in ['powell']:
            self.fitmethod = scipy.optimize.fmin_powell
        elif fitmethod.lower() in ['gradient']:
            self.fitmethod = scipy.optimize.fmin_cg
        elif fitmethod.lower() in ['bfgs']:
            self.fitmethod = scipy.optimize.fmin_bfgs
        ### this one breaks because I can't figure out the syntax for fprime
        elif fitmethod.lower() in ['newton']:
            self.fitmethod = scipy.optimize.fmin_ncg
        elif fitmethod.lower() in ['leastsq']:
            self.fitmethod = scipy.optimize.leastsq
        elif fitmethod.lower() in ['constbfgs']:
            self.fitmethod = scipy.optimize.fmin_l_bfgs_b
        elif fitmethod.lower() in ['tnc']:
            self.fitmethod = scipy.optimize.fmin_tnc

        else:
            print("Minimization method not recognized. Using standard (Powell's) method.")
            self.fitmethod = scipy.optimize.fmin_powell

        if ndim(self.y) ==1:
            ### smooth data by three different factors
            self.smooth3 = scipy.signal.wiener(self.y, 3)
            self.smooth5 = scipy.signal.wiener(self.y, 5)
            self.smooth11 = scipy.signal.wiener(self.y, 11)

    ### Do a maximum likelihood fitting with function func and
    ### initial parameters ain
    ### if residuals are to be fit, put a list of residuals into keyword 'residuals'

    ### func = function to be fitted
    ### ain = list with set of initial parameters
    ### bounds = bounds on parameter ranges (for constrained optimization
    ### obs = if True, compute covariance and print summary to screen
    ### noise = if True, the last parameter in ain is noise and will be renormalized
    ### residuals = put list of residuals here if they should be fit rather than self.y

    def mlest(self, func, ain, bounds = None, obs=True, neg=True, functype='posterior'):

        ### extract frequency and powers from periodogram
        #freq = np.array(self.x)
        #if not residuals is None:
        #    power = residuals
        #else:
        #    power = np.array(self.y)

        #lenpower = float(len(self.y))

        ## renormalize normalization so it's in the right range
        #varobs = np.sum(power)
        #varmod = np.sum(func(freq, *ain))
        #renorm = varobs/varmod

        #if len(ain) > 1:
        #    ain[1] = ain[1] + np.log(renorm)

        ### If last parameter is noise level, renormalize noise level
        ### to something useful:
        #if not noise is None:
        #    ### take the last 50 elements of the power spectrum
        #    noisepower = power[-50:]
        #    meannoise = np.log(np.mean(noisepower))

#            if func == pl_qpo:
#                ain[2] = meannoise
#            if func == bpl_qpo:
#                ain[4] = meannoise

#            else:
        #    ain[noise] = meannoise

        ### definition of the likelihood function
        #def maxlike(pars):
        #    funcval = func(freq, *pars)
        #    res = np.sum(np.log(funcval))+ np.sum(power/funcval)
        #    return res

        #res = maxlike(ain)

        fitparams = _fitting(func, ain, bounds, obs=True)

        #fitparams["model"] = str(func).split()[1]
        #fitparams["mfit"] = func(self.x, *fitparams['popt'])

        ### calculate model power spectrum from optimal parameters
        #fitparams['mfit'] = func(self.x, *fitparams['popt'])
        ### figure-of-merit (SSE)
        #fitparams['merit'] = np.sum(((self.y-fitparams['mfit'])/fitparams['mfit'])**2.0)

        ### find highest outlier
        #plrat = 2.0*(self.y/fitparams['mfit'])
        #fitparams['sobs'] = np.sum(plrat)

        #if nmax ==1:
            ### plmaxpow is the maximum of 2*data/model
        #    plmaxpow = max(plrat[1:])
            #print('plmaxpow: ' + str(plmaxpow))
        #    plmaxind = np.where(plrat == plmaxpow)[0][0]
            #print('plmaxind: ' + str(plmaxind))
        #    plmaxfreq = self.x[plmaxind]

        #else:

        #    plratsort = plrat.sort()
        #    plmaxpow = plrat[-nmax:]

        #    plmaxind, plmaxfreq = [], []
        #    for p in plmaxpow:
        #        plmaxind_temp = np.where(plrat == p)[0][0]
        #        plmaxind.append(plmaxind_temp)
        #        plmaxfreq.append(self.x[plmaxind_temp])

        #fitparams['maxpow'] =  plmaxpow
        #fitparams['maxind'] = plmaxind
        #fitparams['maxfreq'] = plmaxfreq

        ## do a KS test comparing residuals to the exponential distribution
        #plks = scipy.stats.kstest(plrat/2.0, 'expon', N=len(plrat))
        #fitparams['ksp'] = plks[1]

        #print("The figure-of-merit function for this model is: " + str(fitparams['merit']) + " and the fit for " + str(fitparams['dof']) + " dof is " + str(fitparams['merit']/fitparams['dof']) + ".")

        if functype in ['p', 'post', 'posterior']:
            fitparams['deviance'] = 2.0*func.loglikelihood(fitparams['popt'], neg=True)
        elif functype in ['l', 'like', 'likelihood']:
            fitparams['deviance'] = -2.0*func(fitparams['popt'])

        print("Fitting statistics: ")
        print(" -- number of frequencies: " + str(len(self.x)))
        print(" -- Deviance [-2 log L] D = " + str(fitparams['deviance']))
        #print(" -- Highest data/model outlier(s) 2I/S = " + str(fitparams['maxpow']))
        #print("    at frequency(ies) f_max = " + str(fitparams['maxfreq']))
        #print(" -- Summed Residuals S = " + str(fitparams['sobs']))
        #print(" -- Expected S ~ " + str(fitparams['sexp']) + " +- " + str(fitparams['ssd']))
        #print(" -- KS test p-value (use with caution!) p = " + str(fitparams['ksp']))
        #print(" -- merit function (SSE) M = " + str(fitparams['merit']))

        return fitparams

    ### Fitting Routine
    ### optfunc: function to be minimized
    ### ain: initial parameter set
    ### bounds: bounds for constrained optimization
    ### optfuncprime: analytic derivative of optfunc (if required)
    ### neg: bool keyword for MAP estimation (if done):
    ###      if True: compute the negative of the posterior
    def _fitting(self, optfunc, ain, bounds, optfuncprime=None, neg = True, obs=True):

        #print("optfunc in _fitting:" + str(optfunc))

        lenpower = float(len(self.y))

        if neg == True:
            if scipy.__version__ < "0.10.0":
                args = [neg]
            else:
                args = (neg,)
        else:
            args = ()

        #print("args: " + str(args))

        #print("args: " + str(args))

        ### different commands for different fitting methods,
        ### at least until scipy 0.11 is out

        funcval = 100.0
        while funcval == 100 or funcval == 200 or funcval == 0.0 or funcval == np.inf or funcval == -np.inf:
        ## constrained minimization with truncated newton or constrained bfgs
            if self.fitmethod == scipy.optimize.fmin_tnc or self.fitmethod == scipy.optimize.fmin_l_bfgs_b:
                if bounds is None:
                    bounds = [[None, None] for x in range(len(ain))]
                #print("No bounds given. Using no bounds.")

                aopt = self.fitmethod(optfunc, ain, disp=0, args=args, bounds = bounds, approx_grad=True, maxfun=1000)

            ## Newton conjugate gradient, which doesn't work
            elif self.fitmethod == scipy.optimize.fmin_ncg:
                aopt = self.fitmethod(optfunc, ain, optfuncprime, disp=0,args=args)

            ## use R's non-linear minimization
            elif self.nlmflag == True:
                if func == pl:
                    mod = [0,1]
                elif func == bpl:
                    mod = [1,1]
                elif func == pl_qpo:
                    mod = [5,1]
                elif func == bpl_qpo:
                    mod = [6,1]
                elif func.__name__ == 'lorentz':
                    mod = [7,1]

                aopt = self.fitmethod(robjects.r['lpost'], p=robjects.FloatVector(ain), x=robjects.FloatVector(self.x), y=robjects.FloatVector(power), hessian=True, mod=robjects.IntVector(mod))

                ### BFGS algorithm
            elif self.fitmethod == scipy.optimize.fmin_bfgs:
                aopt = self.fitmethod(optfunc, ain, disp=0,full_output=True, args=args)

                warnflag = aopt[6]
                if warnflag == 1 :
                    print("*** ACHTUNG! Maximum number of iterations exceeded! ***")
                elif warnflag == 2:
                    print("Gradient and/or function calls not changing!")

            ## all other methods: Simplex, Powell, Gradient
            else:
                aopt = self.fitmethod(optfunc, ain, disp=0,full_output = True, args=args)

            funcval = aopt[1]
            ain = np.array(ain)*((np.random.rand(len(ain))-0.5)*4.0)

        ### make a dictionary with best-fit parameters:
        ##  popt: best fit parameters (list)
        ##  result: value of ML function at minimum
        ##  model: the model used

        if self.nlmflag:
             fitparams = {'popt':np.array(aopt[1]), 'result':aopt[0]}
        else:
             fitparams = {'popt':aopt[0], 'result':aopt[1]}

        ### calculate model power spectrum from optimal parameters
        #fitparams['mfit'] = func(self.x, *fitparams['popt'])
        ### degrees of freedom
        fitparams['dof'] = lenpower - float(len(fitparams['popt']))
        ### Akaike Information Criterion
        fitparams['aic'] = fitparams['result']+2.0*len(ain)
        ### Bayesian Information Criterion
        fitparams['bic'] = fitparams['result'] + len(ain)*len(self.x)

        ### compute deviance
        try:
            fitparams['deviance'] = 2.0*optfunc.loglikelihood(fitparams['popt'])
        except AttributeError:
            fitparams['deviance'] = 2.0*optfunc(fitparams['popt'])

        fitparams['sexp'] = 2.0*len(self.x)*len(fitparams['popt'])
        fitparams['ssd'] = np.sqrt(2.0*fitparams['sexp'])

        ### smooth data by three different factors
        if ndim(self.y) == 1:
            fitparams['smooth3'] = scipy.signal.wiener(self.y, 3)
            fitparams['smooth5'] = scipy.signal.wiener(self.y, 5)
            fitparams['smooth11'] = scipy.signal.wiener(self.y, 11)

        ### if this is an observation (not fake data), compute the covariance matrix
        if obs == True:
            ### for BFGS, get covariance from algorithm output
            if self.fitmethod == scipy.optimize.fmin_bfgs:
                print("Approximating covariance from BFGS: ")
                covar = aopt[3]
            ### for NLM, get covariance from R output
            elif self.nlmflag:
                print("Getting Hessian from R routine: ")

                phess = aopt[3]
                covar = robjects.r.solve(phess)
                covar = np.array(covar)

            else:
                #print("neg: " + str(args))
            ### calculate Hessian approximating with finite differences
                print("Approximating Hessian with finite differences ...")
                phess = approx_hess(aopt[0], optfunc, neg=args)

                covar = np.linalg.pinv(phess)
                #phess2 = pl_covariance(self.x, self.y, fitparams['popt'])
                ### covariance is the inverse of the Hessian
                print "Hessian (empirical): " + str(phess)

                covar = np.linalg.inv(phess)
            print "Covariance (empirical): " + str(covar)

            fitparams['cov'] = covar

            ### errors of parameters are on the diagonal of the covariance
            ### matrix; take square root to get standard deviation
            stderr = np.sqrt(np.diag(covar))
            fitparams['err'] = stderr

            ### Print results to screen
            print("The best-fit model parameters plus errors are:")
            for i,(x,y) in enumerate(zip(fitparams['popt'], stderr)):
                print("Parameter " + str(i) + ": " + str(x) + " +/- " + str(y))
            print("The Akaike Information Criterion of the power law model is: "+ str(fitparams['aic']) + ".")

        return fitparams

    #### This function computes the Likelihood Ratio Test between two nested models
    ###
    ### mod1: model 1 (simpler model)
    ### ain1: list of input parameters for model 1
    ### mod2: model 2 (more complex model)
    ### ain2: list of input parameters for model 2
    ### bounds1: bounds for model 1 (constrained optimization)
    ### bounds2: bounds for model 2 (constrained optimization)
    def compute_lrt(self, mod1, ain1, mod2, ain2, bounds1=None, bounds2=None, noise1 = -1, noise2 = -1):

        ### fit data with both models
        par1 = self.mlest(mod1, ain1, bounds=bounds1, obs=self.obs, noise=noise1, nmax=nmax)
        par2 = self.mlest(mod2, ain2, bounds=bounds2, obs=self.obs, noise=noise2, nmax=nmax)

        ### extract dictionaries with parameters for each
        varname1 = str(mod1).split()[1] + 'fit'
        varname2 = str(mod2).split()[1] + 'fit'

        self.__setattr__(varname1, par1)
        self.__setattr__(varname2, par2)

        ### compute log likelihood ratio as difference between the deviances
        self.lrt = par1['deviance'] - par2['deviance']

        if self.obs == True:
            print("The Likelihood Ratio for models " + str(mod1).split()[1] + " and " + str(mod2).split()[1] + " is: LRT = " + str(self.lrt))

        return self.lrt

    ### auxiliary function that makes a Lorentzian with a fixed centroid frequency
    ### needed for QPO search algorithm
    def __make_lorentzians(self,x):
       ### loop creates many function definitions lorentz, each differs only by the value
       ### of the centroid frequency f used in computing the spectrum
       for f in x:
           def create_my_func(f):
               def lorentz(x, a, b, e):
                   result = qpo(x, a, b, f, e)
                   return result
               return lorentz
           yield(create_my_func(f))

    #### Fit Lorentzians at each frequency in the spectrum
    #### and return a list of log-likelihoods at each value
    ### fitpars = parameters of broadband noise fit
    ### residuals: if true: divide data by best-fit model in fitpars
    def fitqpo(self, fitpars=None, residuals=False):

        if residuals:
            ### extract model fit
            mfit = fitpars['mfit']

            ### compute residuals: data/model
            residuals = np.array(fitpars["smooth5"])/mfit

        else:
            residuals = np.array(fitpars["smooth5"])

        ### constraint on width of QPO: must be bigger than 2*frequency resolution
        gamma_min = 2.0*(self.x[2]-self.x[1])

        ### empty list for log-likelihoods
        like_rat = []

        ### fit a Lorentzian at every frequency
        for f, func, res in zip(self.x[3:-3], self.__make_lorentzians(self.x[3:-3]), residuals[3:-3]):
            ### constraint on width of QPO: must be narrower than the centroid frequency/2
            gamma_max = f/2.0
            norm = np.mean(residuals)+np.var(residuals)
            ain = [gamma_min, norm, 0.0]

            ### fit QPO to data
            #pars = self.mlest(func, ain, bounds=[[gamma_min, gamma_max], [None, None], [None, None]], noise = True, obs=False, residuals=None)
            pars = self.mlest(func, ain, noise = -1, obs=False, residuals=residuals)

            ### save fitted frequency and data residuals in parameter dictionary
            pars['fitfreq'] = f
            pars['residuals'] = residuals
            like_rat.append(pars)

        ### returns a list of parameter dictionaries
        return like_rat

    #### Find QPOs in Periodogram data
    ### func = broadband noise model
    ### ain = input parameters for broadband noise model
    ### fitmethod = which method to use for fitting the QPOs
    ### plot = if True, save a plot with log-likelihoods
    ### plotname = string used in filename if plot == True
    ### obs = if True, compute covariances and print out stuff
    def find_qpo(self, func, ain,
                 bounds=None,
                 fitmethod='nlm',
                 plot=False,
                 plotname=None,
                 obs = False):

        ### fit broadband noise model to the data
        optpars = self.mlest(func, ain, obs=obs, noise=-1)

        ### fit a variable Lorentzian to every frequency and return parameter values
        lrts = self.fitqpo(fitpars=optpars, residuals=True)

        ### list of likelihood ratios
        like_rat = np.array([x['deviance'] for x in lrts])

        ### find minimum likelihood ratio
        minind = np.where(like_rat == min(like_rat))
        minind = minind[0][0]+3
        #print(minind)
        minfreq = self.x[minind] ### ... maybe not! Needs to be +1 because first frequency left out in psfit

        print("The frequency of the tentative QPO is: " + str(minfreq))

        residuals = self.smooth5/optpars['mfit']

        best_lorentz = self.__make_lorentzians([minfreq])

        noiseind = len(optpars['popt']) - 1

#        for z in self.__make_lorentzians([minfreq]):
#            qpofit_res = self.mlest(z, lrts[minind+1]['popt'], obs=False, noise=-1, residuals=residuals)

#        print("qpofit_res: " + str(qpofit_res['popt']))

        ### minimum width of QPO
        gamma_min = np.log((self.x[1]-self.x[0])*3.0)
        ### maximum width of QPO
        gamma_max = minfreq/1.5

        print('combmod first component: ' + str(func))
        ### create a combined model of broadband noise model + QPO
        combmod = combine_models((func, len(optpars['popt'])), (qpo, 3), mode='add')

        ### make a list of input parameters
        inpars = list(optpars['popt'].copy())
        inpars.extend(lrts[minind-3]['popt'][:2])
        inpars.extend([minfreq])

        qpobounds = [[None, None] for x in range(len(inpars)-3)]
        qpobounds.extend([[gamma_min, gamma_max], [None, None], [None,None]])

        ### fit broadband QPO + noise model, using best-fit parameters as input
        qpopars = self.mlest(combmod, inpars, bounds=qpobounds, obs=obs, noise=noiseind, smooth=0)

        ### likelihood ratio of func+QPO to func
        lrt = optpars['deviance'] - qpopars['deviance']

        like_rat_norm = like_rat/np.mean(like_rat)*np.mean(self.y)*100.0

        if plot:
            plt.figure()
            axL = plt.subplot(1,1,1)
            plt.plot(self.x, self.y, lw=3, c='navy')
            plt.plot(self.x, qpopars['mfit'], lw=3, c='MediumOrchid')
            plt.xscale("log")
            plt.yscale("log")
            plt.xlabel('Frequency')
            plt.ylabel('variance normalized power')
            #yticks_left, ylabels_left = plt.yticks()
            #nr_yticks_left = len(yticks_left)

            axR = plt.twinx()
            #axR = plt.subplot(1,1,1, sharex=axL, frameon=False)
            axR.yaxis.tick_right()
            axR.yaxis.set_label_position("right")
            plt.plot(self.x[3:-3], like_rat, 'r--', lw=2, c="DeepSkyBlue")
            plt.ylabel("-2*log-likelihood")

            #yticks_right, ylabels_right = plt.yticks()
            #tickmin, tickmax = yticks_right[0], yticks_right[-1]
            #tickloc_yleft = np.linspace(tickmin, tickmax, num=nr_yticks_left)
            #axR.yaxis.set_ticks(tickloc_yleft)
            #axR.yaxis.set_ticklabels(["%.2f" % val for val in tickloc_yleft])
            #plt.axis([min(self.x), max(self.x), min(self.y), max(self.y)])

            plt.axis([min(self.x), max(self.x), min(like_rat)-np.var(like_rat), max(like_rat)+np.var(like_rat)])

            plt.savefig(plotname+'.png', format='png')
            plt.close()

        return lrt, optpars, qpopars

    ### plot two fits of broadband models against each other
    def plotfits(self, par1, par2 = None, namestr='test', log=False):

        ### make a figure
        f = plt.figure(figsize=(12,10))
        ### adjust subplots such that the space between the top and bottom of each are zero
        plt.subplots_adjust(hspace=0.0, wspace=0.4)

        ### first subplot of the grid, twice as high as the other two
        ### This is the periodogram with the two fitted models overplotted
        s1 = plt.subplot2grid((4,1),(0,0),rowspan=2)

        if log:
            logx = np.log10(self.x)
            logy = np.log10(self.y)
            logpar1 = np.log10(par1['mfit'])
            logpar1s5 = np.log10(par1['smooth5'])

            p1, = plt.plot(logx, logy, color='black', linestyle='steps-mid')
            p1smooth = plt.plot(logx, logpar1s5, lw=3, color='orange')
            p2, = plt.plot(logx, logpar1, color='blue', lw=2)

        else:
            p1, = plt.plot(self.x, self.y, color='black', linestyle='steps-mid')
            p1smooth = plt.plot(self.x, par1['smooth5'], lw=3, color='orange')
            p2, = plt.plot(self.x, par1['mfit'], color='blue', lw=2)
        if par2:
            if log:
                logpar2 = np.log10(par2['mfit'])
                p3, = plt.plot(logx, logpar2, color='red', lw=2)
            else:
                p3, = plt.plot(self.x, par2['mfit'], color='red', lw=2)
            plt.legend([p1, p2, p3], ["observed periodogram", par1['model'] + " fit", par2['model'] + " fit"])
        else:
            plt.legend([p1, p2], ["observed periodogram", par1['model'] + " fit"])

        if log:
            plt.axis([min(logx), max(logx), min(logy)-1.0, max(logy)+1])
            plt.ylabel('log(Leahy-Normalized Power)', fontsize=18)

        else:
            plt.xscale("log")
            plt.yscale("log")

            plt.axis([min(self.x), max(self.x), min(self.y)/10.0, max(self.y)*10.0])
            plt.ylabel('Leahy-Normalized Power', fontsize=18)
        plt.title("Periodogram and fits for burst " + namestr, fontsize=18)

        ### second subplot: power/model for Power law and straight line
        s2 = plt.subplot2grid((4,1),(2,0),rowspan=1)
        pldif = self.y/par1['mfit']
        if par2:
            bpldif = self.y/par2['mfit']

        if log:
            plt.plot(logx, pldif, color='black', linestyle='steps-mid')
            plt.plot(logx, np.ones(len(self.x)), color='blue', lw=2)

        else:
            plt.plot(self.x, pldif, color='black', linestyle='steps-mid')
            plt.plot(self.x, np.ones(len(self.x)), color='blue', lw=2)
        plt.ylabel("Residuals, \n" + par1['model'] + " model", fontsize=18)

        if log:
            plt.axis([min(logx), max(logx), min(pldif), max(pldif)])

        else:
            plt.xscale("log")
            plt.yscale("log")
            plt.axis([min(self.x), max(self.x), min(pldif), max(pldif)])

        if par2:
            bpldif = self.y/par2['mfit']

        ### third subplot: power/model for bent power law and straight line
            s3 = plt.subplot2grid((4,1),(3,0),rowspan=1)

            if log:
                plt.plot(logx, bpldif, color='black', linestyle='steps-mid')
                plt.plot(logx, np.ones(len(self.x)), color='red', lw=2)
                plt.axis([min(logx), max(logx), min(bpldif), max(bpldif)])

            else:
                plt.plot(self.x, bpldif, color='black', linestyle='steps-mid')
                plt.plot(self.x, np.ones(len(self.x)), color='red', lw=2)
                plt.xscale("log")
                plt.yscale("log")
                plt.axis([min(self.x), max(self.x), min(bpldif), max(bpldif)])

            plt.ylabel("Residuals, \n" + par2['model'] + " model", fontsize=18)

        ax = plt.gca()

        for label in ax.get_xticklabels() + ax.get_yticklabels():
            label.set_fontsize(14)

        if log:
            plt.xlabel("log(Frequency) [Hz]", fontsize=18)
        else:
            plt.xlabel("Frequency [Hz]", fontsize=18)

        ### make sure xticks are taken from first plots, but don't appear there
        plt.setp(s1.get_xticklabels(), visible=False)

        ### save figure in png file and close plot device
        plt.savefig(namestr + '_ps_fit.png', format='png')
        plt.close()

        return

##########################################################
##########################################################
##########################################################

#### PERIODOGRAM FITTING SUBCLASS ################
#
# Compute Maximum A Posteriori (MAP) parameters
# for periodograms via Maximum Likelihood
# using the
# posterior class above
#
#
#
#
#
#
#
#

class PerMaxLike(MaxLikelihood):

    ### ps = PowerSpectrum object with periodogram
    ### obs= if True, compute covariances and print summary to screen
    ###
    ###  fitmethod = choose optimization method
    ###  options are:
    ### 'simplex': use simplex downhill algorithm
    ### 'powell': use modified Powell's algorithm
    ### 'gradient': use nonlinear conjugate gradient
    ### 'bfgs': use BFGS algorithm
    ### 'newton': use Newton CG
    ### 'leastsq' : use least-squares method
    ### 'constbfgs': constrained BFGS algorithm
    ### 'tnc': constrained optimization via a truncated Newton algorithm
    ### 'nlm': optimization via R's non-linear minimization routine
    ### 'anneal': simulated annealing for convex problems
    def __init__(self, ps, obs=True, fitmethod='powell'):

        ### ignore first elements in ps.freq and ps.ps (= no. of photons)
        #ps.freq = np.array(ps.freq[1:])
        self.x = ps.freq[1:]
        #ps.ps = np.array(ps.ps[1:])
        self.y = ps.ps[1:]

        self.ps = ps

        ### Is this a real observation or a fake periodogram to be fitted?
        self.obs = obs

        self.nlmflag = False

        ### set fitmethod
        self._set_fitmethod(fitmethod)

    def mlest(self, func, ain, bounds = None, obs=True, noise=None, nmax=1, residuals = None, smooth=0, m=1, map=True):

        if smooth == 0 :
            power = self.y
        elif smooth == 3:
            power = self.smooth3
        elif smooth == 5:
            power = self.smooth5
        elif smooth == 11:
            power = self.smooth11
        else:
            raise Exception('No valid option for kwarg "smooth". Options are 0,3,5 and 11!')

        if not residuals is None:
            power = residuals

        lenpower = float(len(power))

        ### renormalize normalization so it's in the right range
        varobs = np.sum(power)
        varmod = np.sum(func(self.x, *ain))
        renorm = varobs/varmod

        if len(ain) > 1:
            ain[1] = ain[1] + np.log(renorm)

        #print('noise index: ' + str(noise))
        ### If last parameter is noise level, renormalize noise level
        ### to something useful:
        if not noise is None:
            #print("Renormalizing noise level ...")
            ### take the last 50 elements of the power spectrum
            noisepower = power[-51:-1]
            meannoise = np.log(np.mean(noisepower))
            ain[noise] = meannoise

        ### set function to be minimized: posterior density for periodograms:

        pstemp = powerspectrum.PowerSpectrum()
        pstemp.freq = self.x
        pstemp.ps = power
        pstemp.df = self.ps.df

        if m == 1:
            #print("I am here")
            lposterior = posterior.PerPosterior(pstemp, func)
        elif m > 1:
            lposterior = posterior.StackPerPosterior(pstemp, func, m)

        else:
            raise Exception("Number of power spectra is not a valid number!")

        #print("ain: " + str(ain))
        #print("lposterior(initial value): " + str(lposterior(ain)))

        if not map:
            lpost = lposterior.loglikelihood
        else:
            lpost = lposterior

#        lpost = posterior.PerPosterior(pstemp, func)

        lpostain = lpost(ain)
        #print(lpostain)

#        fitparams = self._fitting(lpost.loglikelihood, ain, bounds, neg = False, obs=obs)

        fitparams = self._fitting(lpost, ain, bounds, neg = True, obs=obs)
        #print("fitparams: " + str(fitparams["popt"]))

        fitparams["model"] = str(func).split()[1]
        fitparams["mfit"] = func(self.x, *fitparams['popt'])

        #print("mfit: " + str(fitparams["mfit"]))

        ### calculate model power spectrum from optimal parameters
        #fitparams['mfit'] = func(self.x, *fitparams['popt'])
        ### figure-of-merit (SSE)
        fitparams['merit'] = np.sum(((power-fitparams['mfit'])/fitparams['mfit'])**2.0)

        ### find highest outlier
        plrat = 2.0*(self.y/fitparams['mfit'])
        #print(plrat)
        fitparams['sobs'] = np.sum(plrat)

        #### plmaxpow is the maximum of 2*data/model
        #plmaxpow = max(plrat)
        ##print("plmaxpow: " + str(plmaxpow))
        #try:

        #    plmaxind = np.where(plrat == plmaxpow)[0]

        ##print("plmaxind: " + str(plmaxind))
        #    plmaxfreq = self.x[plmaxind]
        #except TypeError:
        #    plmaxfreq = None

        if nmax ==1:
            ### plmaxpow is the maximum of 2*data/model
            plmaxpow = max(plrat[1:])
            #print('plmaxpow: ' + str(plmaxpow))
            plmaxind = np.where(plrat == plmaxpow)[0]
            #print('plmaxind: ' + str(plmaxind))
            if len(plmaxind) > 1:
                plmaxind = plmaxind[0]
            elif len(plmaxind) == 0:
                plmaxind = -2
            plmaxfreq = self.x[plmaxind]

        else:

            plratsort = copy.copy(plrat)
            plratsort.sort()
            plmaxpow = plratsort[-nmax:]

            plmaxind, plmaxfreq = [], []
            for p in plmaxpow:
                try:
                    plmaxind_temp = np.where(plrat == p)[0]
                    if len(plmaxind_temp) > 1:
                        plmaxind_temp = plmaxind_temp[0]
                    elif len(plmaxind_temp) == 0:
                        plmaxind_temp = -2
                    plmaxind.append(plmaxind_temp)
                    plmaxfreq.append(self.x[plmaxind_temp])

                except TypeError:
                    plmaxind.append(None)
                    plmaxfreq.append(None)

        fitparams['maxpow'] =  plmaxpow
        fitparams['maxind'] = plmaxind
        fitparams['maxfreq'] = plmaxfreq

        s3rat = 2.0*(fitparams['smooth3']/fitparams['mfit'])
        fitparams['s3max'] = max(s3rat[1:])
        try:
            s3maxind = np.where(s3rat == fitparams['s3max'])[0]
            if len(s3maxind) > 1:
                s3maxind = s3maxind[0]
            fitparams['s3maxfreq'] = self.x[s3maxind]
        except TypeError:
            fitparams["s3maxfreq"] = None
        s5rat = 2.0*(fitparams['smooth5']/fitparams['mfit'])
        fitparams['s5max'] = max(s5rat[1:])
        try:
            s5maxind = np.where(s5rat == fitparams['s5max'])[0]
            if len(s5maxind) > 1:
                s5maxind = s5maxind[0]
            fitparams['s5maxfreq'] = self.x[s5maxind]
        except TypeError:
            fitparams['s5maxfreq'] = None

        s11rat = 2.0*(fitparams['smooth11']/fitparams['mfit'])
        fitparams['s11max'] = max(s11rat[1:])
        try:
            s11maxind = np.where(s11rat == fitparams['s11max'])[0]
            if len(s11maxind) > 1:
                s11maxind = s11maxind[0]
            fitparams['s11maxfreq'] = self.x[s11maxind]
        except TypeError:
            fitparams['s11maxfreq'] = None

        ### compute binned periodograms and find highest outlier in those:

        df = (self.x[1]-self.x[0])
        ### first, compute the maximum binning that would even make sense
        bmax = int(self.x[-1]/(2.0*(self.x[1]-self.x[0])))
        #print('bmax: ' + str(bmax))
        bins = [1,3,5,7,10,15,20,30,50,70,100,200,300,500]

        bindict = {}

        for b in bins:
            #print('bmax: ' + str(bmax))
            #print('b: ' + str(b))
            if b < bmax:
                if b == 1:
                    binps = self.ps
                else:
                    binps = self.ps.rebinps(b*df)
                binpsname = "bin" + str(b)
#                setattr(self, "bin" + str(b), binps)
                bindict[binpsname] = binps
                binpl = func(binps.freq, *fitparams["popt"])
                binratio = 2.0*np.array(binps.ps)/binpl
                #print("len(binratio): " + str(len(binratio)))
                #print("mean(binratio): " + str(mean(binratio)))
                maxind = np.where(binratio[1:] == max(binratio[1:]))[0]
                if len(maxind) > 1:
                    maxind = maxind[0]
                elif len(maxind) == 0 :
                    maxind = -2
                #print('maxind: ' + str(maxind))
                binmaxpow = "bmax" + str(b)
                bindict[binmaxpow] = max(binratio[1:])
                binmaxfreq = "bmaxfreq" + str(b)
                #print("maxind[0]: " + str(maxind[0]+1))
                bindict[binmaxfreq] = binps.freq[maxind+1]
                #setattr(self, "bmax" + str(b), max(binratio[1:]))
                #setattr(self, "b" + str(b) + "maxfreq", binps.freq[maxind+1])
                bindict['binpl' + str(b)] = binpl

        fitparams["bindict"] = bindict

        ## do a KS test comparing residuals to the exponential distribution
        plks = scipy.stats.kstest(plrat/2.0, 'expon', N=len(plrat))
        fitparams['ksp'] = plks[1]

        if obs == True:
            print("The figure-of-merit function for this model is: " + str(fitparams['merit']) + " and the fit for " + str(fitparams['dof']) + " dof is " + str(fitparams['merit']/fitparams['dof']) + ".")

            print("Fitting statistics: ")
            print(" -- number of frequencies: " + str(len(self.x)))
            print(" -- Deviance [-2 log L] D = " + str(fitparams['deviance']))
            print(" -- Highest data/model outlier 2I/S = " + str(fitparams['maxpow']))
            print("    at frequency f_max = " + str(fitparams['maxfreq']))

            print(" -- Highest smoothed data/model outlier for smoothing factor [3] 2I/S = " + str(fitparams['s3max']))
            print("    at frequency f_max = " + str(fitparams['s3maxfreq']))
            print(" -- Highest smoothed data/model outlier for smoothing factor [5] 2I/S = " + str(fitparams['s5max']))
            print("    at frequency f_max = " + str(fitparams['s5maxfreq']))
            print(" -- Highest smoothed data/model outlier for smoothing factor [11] 2I/S = " + str(fitparams['s11max']))
            print("    at frequency f_max = " + str(fitparams['s11maxfreq']))

            print(" -- Summed Residuals S = " + str(fitparams['sobs']))
            print(" -- Expected S ~ " + str(fitparams['sexp']) + " +- " + str(fitparams['ssd']))
            print(" -- KS test p-value (use with caution!) p = " + str(fitparams['ksp']))
            print(" -- merit function (SSE) M = " + str(fitparams['merit']))

        return fitparams

    def compute_lrt(self, mod1, ain1, mod2, ain2, bounds1=None, bounds2=None, noise1=-1, noise2=-1, m=1, map=True, nmax=1):

        ### fit data with both models
        par1 = self.mlest(mod1, ain1, bounds=bounds1, obs=self.obs, noise=noise1, m = m, map = map, nmax=nmax)
        par2 = self.mlest(mod2, ain2, bounds=bounds2, obs=self.obs, noise=noise2, m = m, map = map, nmax=nmax)

        ### extract dictionaries with parameters for each
        varname1 = str(mod1).split()[1] + 'fit'
        varname2 = str(mod2).split()[1] + 'fit'

        self.__setattr__(varname1, par1)
        self.__setattr__(varname2, par2)

        ### compute log likelihood ratio as difference between the deviances
        self.lrt = par1['deviance'] - par2['deviance']

        if self.obs == True:
            print("The Likelihood Ratio for models " + str(mod1).split()[1] + " and " + str(mod2).split()[1] + " is: LRT = " + str(self.lrt))

        return self.lrt

#### MAXIMUM LIKELIHOOD FITTING FOR POISSON DATA
#
# This subclass implements maximum likelihood fitting
# using the modified Cash statistic as implemented in
# XSPEC
#
#
#
class LightcurveMaxLike(MaxLikelihood):

    def __init__(self, lc, obs=True, fitmethod='bfgs'):

        ### ignore first elements in ps.freq and ps.ps (= no. of photons)
        lc.time  = np.array(lc.time)
        self.x = lc.time
        lc.counts = np.array(lc.counts)
        self.y = lc.counts

        self.lc = lc

        ### Is this a real observation or a fake periodogram to be fitted?
        self.obs = obs

        self.nlmflag = False

        ### set fitmethod
        self._set_fitmethod(fitmethod)

    def mlest(self, func, ain, bounds = None, obs=True, noise=None, residuals = None, nmax=1):

        if not residuals is None:
            counts = residuals
            self.y= residuals
            self.lc.counts = residuals
        else:
            counts = self.y

        lencounts = float(len(counts))

        ### set function to be minimized: posterior density for periodograms:

        lpost = posterior.LightcurvePosterior(self.lc, func)

        lpostain = lpost(ain)
        #print(lpostain)

#        fitparams = self._fitting(lpost.loglikelihood, ain, bounds, neg = False, obs=obs)

        fitparams = self._fitting(lpost, ain, bounds, neg = True, obs=obs)
        #print("fitparams: " + str(fitparams["popt"]))

        fitparams["model"] = str(func).split()[1]
        fitparams["mfit"] = func(self.x, *fitparams['popt'])

        #print("mfit: " + str(fitparams["mfit"]))

        ### calculate model power spectrum from optimal parameters
        #fitparams['mfit'] = func(self.x, *fitparams['popt'])
        ### figure-of-merit (SSE)
        fitparams['merit'] = np.sum(((self.y-fitparams['mfit'])/fitparams['mfit'])**2.0)

        ### find highest outlier
        plrat = 2.0*(self.y/fitparams['mfit'])
        fitparams['sobs'] = np.sum(plrat[1:])

       ## do a KS test comparing residuals to the exponential distribution
        plks = scipy.stats.kstest(plrat/2.0, 'expon', N=len(plrat))
        fitparams['ksp'] = plks[1]

        if obs == True:
            print("The figure-of-merit function for this model is: " + str(fitparams['merit']) + " and the fit for " + str(fitparams['dof']) + " dof is " + str(fitparams['merit']/fitparams['dof']) + ".")

            print("Fitting statistics: ")
            print(" -- Deviance [-2 log L] D = " + str(fitparams['deviance']))
            print(" -- Summed Residuals S = " + str(fitparams['sobs']))
            print(" -- Expected S ~ " + str(fitparams['sexp']) + " +- " + str(fitparams['ssd']))
            print(" -- KS test p-value (use with caution!) p = " + str(fitparams['ksp']))
            print(" -- merit function (SSE) M = " + str(fitparams['merit']))

        return fitparams

########################################
########################################

class GaussMaxLike(MaxLikelihood):

    def __init__(self, x, y, obs=True, fitmethod='bfgs'):
        MaxLikelihood.__init__(self, x, y, obs, fitmethod)

    def mlest(self, lpost, ain, func=None, bounds = None, obs=True):

        lpostain = lpost(ain)

        fitparams = self._fitting(lpost, ain, bounds, neg = True, obs=obs)

        #model1 = str(covar).split()[1]
        #fitparams['covariance function'] = model1

        if func:
        #    model2 = str(func).split()[1]
        #    fitparams["function model"] = model2
            fitparams["function fit"] = func(self.x, *fitparams['popt'][lpost.ncovar:])

        #fitparams['merit'] = np.sum(((self.y-fitparams['mfit'])/fitparams['mfit'])**2.0)

        if obs == True:
            #print("The figure-of-merit function for this model is: " + str(fitparams['merit']) + " and the fit for " + str(fitparams['dof']) + " dof is " + str(fitparams['merit']/fitparams['dof']) + ".")

            print("Fitting statistics: ")
            print(" -- number of data points: " + str(len(self.x)))
            print(" -- Deviance [-2 log L] D = " + str(fitparams['deviance']))

        return fitparams


text:
Media

Images

Responsive images

Images can be made responsive-friendly via the addition of the .img-responsive class. This makes sure the image scales nicely to the parent element.

    <hljs data-format="html">

    </hljs>

To center images which use the .img-responsive class, use .center-block instead of .text-center.

Image styles

        <hljs data-format="html">

        </hljs>

        <hljs data-format="html">

        </hljs>

        <hljs data-format="html">

        </hljs>

        <hljs data-format="html">

        </hljs>

Image style helpers

Set an image left, right or centered in its parent container.

        <hljs data-format="html">
</hljdiv>

        <hljs data-format="html">
</hljdiv>

        <hljs data-format="html">
</hljdiv>

Media Object

Abstract object styles for building various types of components (like blog comments, Tweets, etc) that feature a left- or right-aligned image alongside textual content.

Media heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras hendrerit diam ut auctor pellentesque. Cras quis interdum neque.

        <hljs data-format="html">

Media heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras hendrerit diam ut auctor pellentesque. Cras quis interdum neque.

        </hljs>

Media heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras hendrerit diam ut auctor pellentesque. Cras quis interdum neque.

Nested media heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras hendrerit diam ut auctor pellentesque. Cras quis interdum neque.

        <hljs data-format="html">

Media heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras hendrerit diam ut auctor pellentesque. Cras quis interdum neque.

Nested media heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras hendrerit diam ut auctor pellentesque. Cras quis interdum neque.

        </hljs>

Responsive wrapper

A responsive wrapper for video or other content so it behaves fluid and responsive towards various viewport sizes. Resize your browser to see it in action.

        <hljs data-format="html">

            <iframe allowfullscreen="" frameborder="0" src="//www.youtube.com/embed/mHyRCeKxhss" width="853" height="480"></iframe>

        </hljs>


text:
(function() {

native function GetChromeHidden();
native function GetNextContextMenuId();

var chromeHidden = GetChromeHidden();

chromeHidden.registerCustomHook('contextMenus', function(bindingsAPI) {
  var apiFunctions = bindingsAPI.apiFunctions;
  var sendRequest = bindingsAPI.sendRequest;

  chromeHidden.contextMenus = {};
  chromeHidden.contextMenus.handlers = {};
  var eventName = "contextMenus";
  chromeHidden.contextMenus.event = new chrome.Event(eventName);
  chromeHidden.contextMenus.ensureListenerSetup = function() {
    if (chromeHidden.contextMenus.listening) {
      return;
    }
    chromeHidden.contextMenus.listening = true;
    chromeHidden.contextMenus.event.addListener(function() {
      // An extension context menu item has been clicked on - fire the onclick
      // if there is one.
      var id = arguments[0].menuItemId;
      var onclick = chromeHidden.contextMenus.handlers[id];
      if (onclick) {
        onclick.apply(null, arguments);
      }
    });
  };

  apiFunctions.setHandleRequest("contextMenus.create", function() {
    var args = arguments;
    var id = GetNextContextMenuId();
    args[0].generatedId = id;
    sendRequest(this.name,
                args,
                this.definition.parameters,
                {customCallback: this.customCallback});
    return id;
  });

  apiFunctions.setCustomCallback("contextMenus.create",
      function(name, request, response) {
    if (chrome.extension.lastError) {
      return;
    }

    var id = request.args[0].generatedId;

    // Set up the onclick handler if we were passed one in the request.
    var onclick = request.args.length ? request.args[0].onclick : null;
    if (onclick) {
      chromeHidden.contextMenus.ensureListenerSetup();
      chromeHidden.contextMenus.handlers[id] = onclick;
    }
  });

  apiFunctions.setCustomCallback("contextMenus.remove",
      function(name, request, response) {
    if (chrome.extension.lastError) {
      return;
    }
    var id = request.args[0];
    delete chromeHidden.contextMenus.handlers[id];
  });

  apiFunctions.setCustomCallback("contextMenus.update",
      function(name, request, response) {
    if (chrome.extension.lastError) {
      return;
    }
    var id = request.args[0];
    if (request.args[1].onclick) {
      chromeHidden.contextMenus.handlers[id] = request.args[1].onclick;
    }
  });

  apiFunctions.setCustomCallback("contextMenus.removeAll",
      function(name, request, response) {
    if (chrome.extension.lastError) {
      return;
    }
    chromeHidden.contextMenus.handlers = {};
  });
});

})();


text:
using Enigma.Serialization;

namespace Enigma.Test.Serialization.Fakes
{
    public interface IWriteStatistics
    {
        int VisitCount { get; }
        int LeaveCount { get; }
        int VisitByteCount { get; }
        int VisitInt16Count { get; }
        int VisitInt32Count { get; }
        int VisitInt64Count { get; }
        int VisitUInt16Count { get; }
        int VisitUInt32Count { get; }
        int VisitUInt64Count { get; }
        int VisitBooleanCount { get; }
        int VisitSingleCount { get; }
        int VisitDoubleCount { get; }
        int VisitDecimalCount { get; }
        int VisitTimeSpanCount { get; }
        int VisitDateTimeCount { get; }
        int VisitStringCount { get; }
        int VisitGuidCount { get; }
        int VisitBlobCount { get; }
        int VisitValueCount { get; }
        void AssertVisitOrderExact(params LevelType[] expectedLevels);
        void AssertVisitOrderBeginsWith(params LevelType[] expectedLevels);
    }
}


text:
/* TEMPLATE GENERATED TESTCASE FILE
Filename: CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81.h
Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml
Template File: sources-vasinks-81.tmpl.h
*/
/*
 * @description
 * CWE: 134 Uncontrolled Format String
 * BadSource: file Read input from a file
 * GoodSource: Copy a fixed string into data
 * Sinks: w32_vsnprintf
 *    GoodSink: _vsnwprintf with a format string
 *    BadSink : _vsnwprintf without a format string
 * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference
 *
 * */

#include "std_testcase.h"

#ifndef _WIN32
#include <wchar.h>
#endif

namespace CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81
{

class CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81_base
{
public:
    /* pure virtual function */
    virtual void action(wchar_t * data) const = 0;
};

#ifndef OMITBAD

class CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81_bad : public CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81_base
{
public:
    void action(wchar_t * data) const;
};

#endif /* OMITBAD */

#ifndef OMITGOOD

class CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81_goodG2B : public CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81_base
{
public:
    void action(wchar_t * data) const;
};

class CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81_goodB2G : public CWE134_Uncontrolled_Format_String__wchar_t_file_w32_vsnprintf_81_base
{
public:
    void action(wchar_t * data) const;
};

#endif /* OMITGOOD */

}


text:
var async = require("async");

/*
    async.waterfall
 */

// simplest form
async.waterfall(
    [
        // step 1
        function(callback) {
            console.log("simple step 1");
            callback(null);
        },
        // step 2
        function(callback) {
            console.log("simple step 2");
            callback(null);
        },
        // step 3
        function(callback) {
            console.log("simple step 3");
            callback(null, "OK");
        }
    ],
    function(err, results) {
        if (err)
            console.error("simple Error:" + err.message);
        else {
            console.log("simple Done." + results);
        }
    }
);

// passing parameters to the next step
(function () {
    var iterations:number = 0;
    var str:string[] = ["arg1", "arg2", "arg3"];

    async.waterfall(
        [
            // step 1
            function(callback) {
                console.log("step 1");
                callback(null, str[iterations++]);
            },
            // step 2
            function(arg, callback) {
                console.log("step 2:" + arg);
                callback(null, str[iterations++]);
            },
            // step 3
            function(arg, callback) {
                console.log("step 3:" + arg);
                callback(null, str[iterations++]);
            }
        ],
        function(err, results) {
            if (err)
                console.error("Error:" + err.message);
            else {
                console.log("Done:" + results);
                console.log(iterations + " iterations");
            }
        }
    )
})();


text:
class ArtistsController < ApplicationController
  layout "admin"

  def index
    @artists_grid = initialize_grid(Artist,:per_page => 20)

    respond_to do |format|
      format.html # index.html.erb
      format.json
    end
  end

  def show
    @artist = Artist.find_by_pinyin(params[:pinyin])

    respond_to do |format|
       format.html {render :layout=>"application"}# show.html.erb
      format.json
    end
  end

  def new
    @artist = Artist.new

    respond_to do |format|
      format.html # new.html.erb
      format.json
    end
  end

  def edit
    @artist = Artist.find(params[:id])
  end

  def create
    @artist = Artist.new(params[:artist])

    respond_to do |format|
      if @artist.save
        format.html { redirect_to(artists_path, :notice => '艺术家 创建成功。') }
        format.json
      else
        format.html { render :action => "new" }
        format.json
      end
    end
  end

  def update
    @artist = Artist.find(params[:id])

    respond_to do |format|
      if @artist.update_attributes(params[:artist])
        format.html { redirect_to(artists_path, :notice => '艺术家 更新成功。') }
        format.json
      else
        format.html { render :action => "edit" }
        format.json
      end
    end
  end

  def destroy
    @artist = Artist.find(params[:id])
    @artist.destroy

    respond_to do |format|
      format.html { redirect_to(artists_path,:notice => "删除成功。") }
      format.json
    end
  end
end


text:
layout: home
title: Allythy
subtitle: Use  e contribua com Software Livre
---


text:
require 'chef/provider/aws_provider'
require 'date'

class Chef::Provider::AwsS3Bucket < Chef::Provider::AwsProvider
  action :create do
    if existing_bucket == nil
      converge_by "Creating new S3 bucket #{fqn}" do
        bucket = s3.buckets.create(fqn)
        bucket.tags['Name'] = new_resource.name
        if new_resource.enable_website_hosting
          bucket.website_configuration = AWS::S3::WebsiteConfiguration.new(
            new_resource.website_options)
        end
      end
    elsif requires_modification?
      converge_by "Modifying S3 Bucket #{fqn}" do
        if existing_bucket.website_configuration == nil && new_resource.enable_website_hosting ||
          existing_bucket.website_configuration != nil && new_resource.enable_website_hosting
          existing_bucket.website_configuration = AWS::S3::WebsiteConfiguration.new(
            new_resource.website_options)
        else
          existing_bucket.remove_website_configuration
        end
      end
    end

    new_resource.save
  end

  action :delete do
    if existing_bucket
      converge_by "Deleting S3 bucket #{fqn}" do
        existing_bucket.delete
      end
    end

    new_resource.delete
  end

  def existing_bucket
    Chef::Log.debug("Checking for S3 bucket #{fqn}")
    @existing_bucket ||= s3.buckets[fqn] if s3.buckets[fqn].exists?
  end

  def requires_modification?
    if existing_bucket.website_configuration == nil
      new_resource.enable_website_hosting
    else
      !new_resource.enable_website_hosting ||
        !compare_website_configuration
    end
  end

  def compare_website_configuration
    # This is incomplete, routing rules have many optional values, so its
    # possible aws will put in default values for those which won't be in
    # the requested config.
    new_web_config = new_resource.website_options
    current_web_config = existing_bucket.website_configuration.to_hash

    current_web_config[:index_document] == new_web_config.fetch(:index_document, {}) &&
      current_web_config[:error_document] == new_web_config.fetch(:error_document, {}) &&
      current_web_config[:routing_rules] == new_web_config.fetch(:routing_rules, [])
  end

  # Fully qualified bucket name (i.e resource_region unless otherwise specified)
  def id
    new_resource.bucket_name || new_resource.name
  end
end


text:
<?php

/**
 * @namespace
 */
namespace Jb\Bundle\ConfigKnpMenuBundle\Tests\Config\Definition\Builder;

use Jb\Bundle\ConfigKnpMenuBundle\Config\Definition\Builder\MenuTreeBuilder;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Jb\Bundle\ConfigKnpMenuBundle\Config\Definition\Builder\MenuNodeDefinition;

/**
 * Tests for Jb\Bundle\ConfigKnpMenuBundle\Config\Definition\Builder\MenuTreeBuilder
 */
class MenuTreeBuilderTest extends TestCase
{
    protected MenuTreeBuilder $builder;

    /**
     * Init builder
     */
    protected function setUp(): void
    {
        $this->builder = new MenuTreeBuilder();
    }

    /**
     * Test constructor
     * Verify if the menu node has been registered
     */
    public function testConstructor(): void
    {
        $reflection = new ReflectionClass($this->builder);
        $property = $reflection->getProperty('nodeMapping');
        $property->setAccessible(true);
        $nodeMapping = $property->getValue($this->builder);

        $this->assertArrayHasKey('menu', $nodeMapping);
        $this->assertEquals(
            MenuNodeDefinition::class,
            $nodeMapping['menu']
        );
    }

    /**
     * Test if builder return a menu node
     */
    public function testMenuNode(): void
    {
        $nodeDefinition = $this->builder->menuNode('test');
        $this->assertInstanceOf(
            MenuNodeDefinition::class,
            $nodeDefinition
        );
        $this->assertEquals('test', $nodeDefinition->getNode()->getName());
    }
}


text:
package se.culvertsoft.mgen.cpppack.generator.impl.classh

import se.culvertsoft.mgen.api.model.ClassType
import se.culvertsoft.mgen.compiler.internal.BuiltInGeneratorUtil.ln
import se.culvertsoft.mgen.compiler.util.SourceCodeBuffer

object MkFieldById {

  def apply(t: ClassType)(implicit txtBuffer: SourceCodeBuffer) {

    ln(1, s"const mgen::Field * _fieldById(const short id) const;")
    ln(1, s"const mgen::Field * _fieldByName(const std::string& name) const;")

  }

}


text:
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.mail;

import com.google.common.collect.Iterables;
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.mail.ProjectWatch.Watchers;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Notify interested parties of a brand new change. */
public class CreateChangeSender extends NewChangeSender {
  private static final Logger log =
      LoggerFactory.getLogger(CreateChangeSender.class);

  public static interface Factory {
    public CreateChangeSender create(Change change);
  }

  @Inject
  public CreateChangeSender(EmailArguments ea, @Assisted Change c) {
    super(ea, c);
  }

  @Override
  protected void init() throws EmailException {
    super.init();

    if (change.getStatus() == Change.Status.NEW) {
      try {
        // Try to mark interested owners with TO and CC or BCC line.
        Watchers matching = getWatchers(NotifyType.NEW_CHANGES);
        for (Account.Id user : Iterables.concat(
            matching.to.accounts,
            matching.cc.accounts,
            matching.bcc.accounts)) {
          if (isOwnerOfProjectOrBranch(user)) {
            add(RecipientType.TO, user);
          }
        }

        // Add everyone else. Owners added above will not be duplicated.
        add(RecipientType.TO, matching.to);
        add(RecipientType.CC, matching.cc);
        add(RecipientType.BCC, matching.bcc);
      } catch (OrmException err) {
        // Just don't CC everyone. Better to send a partial message to those
        // we already have queued up then to fail deliver entirely to people
        // who have a lower interest in the change.
        log.warn("Cannot notify watchers for new change", err);
      }

      includeWatchers(NotifyType.NEW_PATCHSETS);
    }
  }

  private boolean isOwnerOfProjectOrBranch(Account.Id user) {
    return projectState != null
        && projectState.controlFor(args.identifiedUserFactory.create(user))
          .controlForRef(change.getDest())
          .isOwner();
  }
}


text:
package gov.sandia.cognition.text.term;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A default implementation of the {@code TermIndex} interface.
 *
 * @author  Justin Basilico
 * @since   3.0
 */
public class DefaultTermIndex
    extends AbstractTermIndex
{

    /** The mapping of terms to their corresponding (unique) indices. */
    protected Map<Term, DefaultIndexedTerm> termMap;

    /** The list of terms ordered by indices. */
    protected List<DefaultIndexedTerm> termList;

    /**
     * Creates a new, empty {@code DefaultTermIndex}.
     */
    public DefaultTermIndex()
    {
        super();

        this.setTermMap(new HashMap<Term, DefaultIndexedTerm>());
        this.setTermList(new ArrayList<DefaultIndexedTerm>());
    }

    @Override
    public DefaultTermIndex clone()
    {
        final DefaultTermIndex clone = (DefaultTermIndex) super.clone();
        clone.termMap = new HashMap<Term, DefaultIndexedTerm>(this.termMap);
        clone.termList = new ArrayList<DefaultIndexedTerm>(this.termList);
        return clone;
    }

    public IndexedTerm add(
        final Term term)
    {
        if (term == null)
        {
            // Don't add a null term.
            return null;
        }

        // Get the current index of the term.
        DefaultIndexedTerm indexedTerm = this.getIndexedTerm(term);
        if (indexedTerm == null)
        {
            // The term is not in the index, so add it.
            int index = termList.size();
            indexedTerm = new DefaultIndexedTerm(index, term);
            this.termMap.put(term, indexedTerm);
            this.termList.add(indexedTerm);
        }

        // Return the index for the term.
        return indexedTerm;
    }

    public int getTermCount()
    {
        return this.termList.size();
    }

    public List<DefaultIndexedTerm> getTerms()
    {
        return Collections.unmodifiableList(this.getTermList());
    }

    public DefaultIndexedTerm getIndexedTerm(
        final Term term)
    {
        return this.termMap.get(term);
    }

    public DefaultIndexedTerm getIndexedTerm(
        final int index)
    {
        if (index < 0 || index >= this.getTermCount())
        {
            // Bad index.
            return null;
        }
        else
        {
            // Get the index of the term.
            return this.termList.get(index);
        }
    }

    /**
     * Gets the mapping of terms to their indices.
     *
     * @return
     *      The mapping of terms to their indices.
     */
    protected Map<Term, DefaultIndexedTerm> getTermMap()
    {
        return this.termMap;
    }

    /**
     * Sets the mapping of terms to their indices.
     *
     * @param   termMap
     *      The mapping of terms to their indices.
     */
    protected void setTermMap(
        final Map<Term, DefaultIndexedTerm> termMap)
    {
        this.termMap = termMap;
    }

    /**
     * Sets the list of terms, ordered by index.
     *
     * @return
     *      The list of terms, ordered by index.
     */
    protected List<DefaultIndexedTerm> getTermList()
    {
        return this.termList;
    }

    /**
     * Gets the list of terms, ordered by index.
     *
     * @param   termList
     *      The list of terms, ordered by index.
     */
    protected void setTermList(
        final List<DefaultIndexedTerm> termList)
    {
        this.termList = termList;
    }

}


text:
import DB from '../db';
import BadWords from '../bad_words/index';
import { IPost, ICache, IComment } from '~/types/schema';
const { Post, Cache, Category, Comment, Guestbook, Setting, Profile } = DB.Models;
const badWords = BadWords.instance;

export async function getCategories () {
  const categories = await Category.find(
    {},
    {},
    { sort: 'sequence cateName' }
  ).exec();
  return categories;
}

export async function getPosts (params) {
  let page = 1;
  const pageSize = parseInt(params.pageSize);
  page = parseInt(params.pageIndex) || 1;
  page = page > 0 ? page : 1;
  const conditions: any = {
    isDraft: false,
    isActive: true
  };
  if (params.category) {
    conditions.category = params.category;
  }
  const keyword = params.keyword;
  if (keyword) {
    switch (params.filterType) {
      case 'title':
        conditions.title = { $regex: keyword, $options: 'gi' };
        break;
      case 'tag':
        conditions.labels = { $regex: keyword, $options: 'gi' };
        break;
      case 'date':
        if (
          Array.isArray(keyword) &&
                    keyword.length === 2 &&
                    keyword[0] &&
                    keyword[1]
        ) {
          const start = new Date(keyword[0]);
          const end = new Date(keyword[1]);
          conditions.publishTime = { $gte: start, $lt: end };
        }
        break;
      default:
        conditions.$or = [
          {
            title: {
              $regex: keyword,
              $options: 'gi'
            }
          },
          {
            labels: {
              $regex: keyword,
              $options: 'gi'
            }
          },
          {
            content: {
              $regex: keyword,
              $options: 'gi'
            }
          }
        ];
    }
  }
  const data = await Promise.all([
    Post.find(
      conditions,
      {},
      {
        skip: (page - 1) * pageSize,
        limit: pageSize,
        sort: '-publishTime'
      }
    )
      .populate('category')
      .populate('comments', '_id')
      .exec(),
    Post.countDocuments(conditions).exec()
  ]);
  const postList = data[0];
  const count = data[1];
  const pageCount = Math.ceil(count / pageSize);

  return {
    postList,
    count,
    hasNext: pageCount > page
  };
}

export async function getPopArticles () {
  const conditions: any = {
    isDraft: false,
    isActive: true
  };
  const articles = await Post.find(conditions, '-content', {
    sort: '-viewCount',
    limit: 7
  })
    .populate('category')
    .exec();
  return {
    articles
  };
}
export async function getPopLabels () {
  const labels = await Post.aggregate([
    {
      $unwind: '$labels'
    },
    {
      $group: {
        _id: '$labels',
        count: { $sum: 1 }
      }
    },
    {
      $sort: { count: -1, _id: 1 }
    },
    {
      $limit: 10
    }
  ]).exec();
  return {
    labels
  };
}

export async function getArticle (params, user) {
  const alias = params.alias;
  const conditions = {
    alias
  } as IPost;
  if (!user) {
    // 未登录,即不是管理员账号,则限制只展示已发布和未删除的文章
    conditions.isDraft = false;
    conditions.isActive = true;
  }
  const article = await Post.findOne(conditions)
    .populate('category')
    .exec();
  return article;
}

export async function getPostsCountByCate (category) {
  const count = await Post.countDocuments({
    category,
    isDraft: false,
    isActive: true
  } as IPost).exec();
  return count;
}

export async function increaseViews ({ postID, clientIP }) {
  // 判断该IP用户是否已看过该文章
  const exists = await Cache.exists({
    clientIP,
    ext1: postID,
    ext2: 'viewCount'
  } as ICache);

  // 如果没看过
  if (!exists) {
    // 文章浏览数+1
    Post.findByIdAndUpdate(postID, {
      $inc: { viewCount: 1 }
    }).exec();

    // 同时,将用户IP和文章ID存入缓存
    Cache.create({
      clientIP,
      ext1: postID,
      ext2: 'viewCount'
    });
  }
}

export async function getComments (params) {
  let page = 1;
  const pageSize = parseInt(params.pageSize);
  page = parseInt(params.pageIndex) || 1;
  page = page > 0 ? page : 1;
  const options: any = {};
  options.skip = (page - 1) * pageSize;
  options.limit = pageSize;
  options.sort = '-createTime';
  const query = {
    post: params.articleId
  } as IComment;
  const data = await Promise.all([
    Comment.find(query, {}, options).exec(),
    Comment.countDocuments(query).exec()
  ]);
  const comments = data[0];
  const count = data[1];
  const pageCount = Math.ceil(count / pageSize);
  return {
    comments,
    count,
    hasNext: pageCount > page
  };
}

export async function saveComment (params) {
  const entity = new Comment({
    post: params.articleId,
    username: badWords.filter(params.username),
    website: params.website,
    content: badWords.filter(params.content),
    createTime: new Date()
  } as IComment);
  const comment = await entity.save();
  return {
    comment
  };
}

export async function getGuestbook (params) {
  let page = 1;
  const pageSize = parseInt(params.pageSize);
  page = parseInt(params.pageIndex) || 1;
  page = page > 0 ? page : 1;
  const options: any = {};
  options.skip = (page - 1) * pageSize;
  options.limit = pageSize;
  options.sort = '-createTime';
  const data = await Promise.all([
    Guestbook.find({}, {}, options).exec(),
    Guestbook.countDocuments({}).exec()
  ]);
  const comments = data[0];
  const count = data[1];
  const pageCount = Math.ceil(count / pageSize);
  return {
    comments,
    count,
    hasNext: pageCount > page
  };
}

export async function saveGuestbook (params) {
  const entity = new Guestbook({
    username: badWords.filter(params.username),
    website: params.website,
    content: badWords.filter(params.content),
    createTime: new Date()
  } as IComment);
  const comment = await entity.save();
  return {
    comment
  };
}

export async function getSettings () {
  const settings = await Setting.findOne().exec();
  return {
    settings
  };
}

export async function getProfile () {
  const profile = await Profile.findOne().exec();
  return {
    profile
  };
}


text:
require 'minitest_helper'

describe HAT::RootController do
  include Rack::Test::Methods

  def app
    HAT::Application.new
  end

  describe 'GET /' do
    it 'must be ok' do
      get '/'
      last_response.must_be :ok?
      last_response.body.must_match /Hello HAT!/
    end
  end
end


text:
using System.Globalization;
using uBinding.Converters;
using uBinding.Descriptions;

namespace uBinding.Extensions
{
    public static class BindingDescriptionExtensions
    {
        public static BindingDescription<float, string> WithDefaultConverter(
           this BindingDescription<float, string> description)
        {
            return description.With(new GenericConverter<float, string>(
                s =>
                {
                    float result;
                    return float.TryParse(s, out result) ? result : 0;
                },
                f => f.ToString(CultureInfo.InvariantCulture)));
        }

        public static BindingDescription<int, float> WithDefaultConverter(
            this BindingDescription<int, float> description)
        {
            return description.With(new GenericConverter<int, float>(f => (int)f, i => (float)i));
        }
    }
}


text:
@class TableViewIllustratedEmptyView;

// Protocol that delegates of `TableViewIllustratedEmptyView` must implement.
// Delegates get notified when links in the `TableViewIllustratedEmptyView`'s
// subtitle are tapped.
@protocol TableViewIllustratedEmptyViewDelegate

// Invoked when a link in `view`'s subtitle is tapped.
- (void)tableViewIllustratedEmptyView:(TableViewIllustratedEmptyView*)view
                   didTapSubtitleLink:(NSURL*)URL;

@end

// Displays an UIImage on top of a message over a clearBackground.
@interface TableViewIllustratedEmptyView
    : UIView <ChromeEmptyTableViewBackground>

// Convenience initializer for a view that displays a large `image`, a `title`
// and a `subtitle`.
- (instancetype)initWithFrame:(CGRect)frame
                        image:(UIImage*)image
                        title:(NSString*)title
                     subtitle:(NSString*)subtitle;

// Designated initializer for a view that displays a large `image`, a `title`
// and an `attributedSubtitle`.
- (instancetype)initWithFrame:(CGRect)frame
                        image:(UIImage*)image
                        title:(NSString*)title
           attributedSubtitle:(NSAttributedString*)attributedSubtitle
    NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;

// Delegate to notify when a link in the subtitle is tapped.
@property(nonatomic, weak) id<TableViewIllustratedEmptyViewDelegate> delegate;

// The empty view's accessibility identifier.
+ (NSString*)accessibilityIdentifier;

// Returns an NSDictionary suitable for use as the default/base attributes of
// a subtitle in a TableViewIllustratedEmptyView. The appearance will match the
// result of passing an NSString* as `subtitle` in the convenience initializer
// above.
+ (NSDictionary*)defaultTextAttributesForSubtitle;

@end

#endif  // IOS_CHROME_BROWSER_UI_TABLE_VIEW_TABLE_VIEW_ILLUSTRATED_EMPTY_VIEW_H_


text:
<?php

/**
 * @category   Payment Gateway
 * @package    Adyen_Payment
 * @author     Adyen
 * @property   Adyen B.V
 * @copyright  Copyright (c) 2014 Adyen BV (http://www.adyen.com)
 */
class Adyen_Payment_Block_Form_Openinvoice extends Mage_Payment_Block_Form {

    protected $_dateInputs = array();

    protected function _construct() {
        $this->setTemplate('adyen/form/openinvoice.phtml');

        /* Check if the customer is logged in or not */
        if (Mage::getSingleton('customer/session')->isLoggedIn()) {

            /* Get the customer data */
            $customer = Mage::getSingleton('customer/session')->getCustomer();

            // set the default value
            $this->setDate($customer->getDob());
            $this->setGender($customer->getGender());
        }

        parent::_construct();
    }

    public function setDate($date)
    {
        $this->setTime($date ? strtotime($date) : false);
        $this->setData('date', $date);
        return $this;
    }

    public function getDay()
    {
        return $this->getTime() ? date('d', $this->getTime()) : '';
    }

    public function getMonth()
    {
        return $this->getTime() ? date('m', $this->getTime()) : '';
    }

    public function getYear()
    {
        return $this->getTime() ? date('Y', $this->getTime()) : '';
    }

    /**
     * Returns format which will be applied for DOB in javascript
     *
     * @return string
     */
    public function getDateFormat()
    {
        return Mage::app()->getLocale()->getDateStrFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
    }

    /**
     * Add date input html
     *
     * @param string $code
     * @param string $html
     */
    public function setDateInput($code, $html)
    {
        $this->_dateInputs[$code] = $html;
    }

    /**
     * Sort date inputs by dateformat order of current locale
     *
     * @return string
     */
    public function getSortedDateInputs()
    {
        $strtr = array(
            '%b' => '%1$s',
            '%B' => '%1$s',
            '%m' => '%1$s',
            '%d' => '%2$s',
            '%e' => '%2$s',
            '%Y' => '%3$s',
            '%y' => '%3$s'
        );

        $dateFormat = preg_replace('/[^\%\w]/', '\\1', $this->getDateFormat());

        return sprintf(strtr($dateFormat, $strtr),
            $this->_dateInputs['m'], $this->_dateInputs['d'], $this->_dateInputs['y']);
    }

    public function genderShow() {
        return $this->getMethod()->genderShow();
    }

    public function dobShow() {
        return $this->getMethod()->dobShow();
    }

}


text:
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';

import {EventHandlerComponent} from './event-handler/event-handler.component';
import {RouterOutletComponent} from './router-outlet/router-outlet.component';

const routes: Routes = [
  {path: 'event-handler.found', component: EventHandlerComponent},
  {path: 'router-outlet.found', component: RouterOutletComponent},
];

@NgModule({
  declarations: [EventHandlerComponent, RouterOutletComponent],
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {
}


text:
using System;
using System.Data.SqlClient;

namespace TSqlWatcher
{
    class LambdaCommand : ICommand
    {
    	private readonly Action<SqlTransaction, SqlProjectInfo, CommandAdder> lambda;
    	public LambdaCommand(Action<SqlTransaction, SqlProjectInfo, CommandAdder> lambda)
    	{
    		this.lambda = lambda;
    	}

    	public void Perform(SqlTransaction transaction, SqlProjectInfo project, CommandAdder commandAdder)
    	{
    		lambda(transaction, project, commandAdder);
    	}
    }
}


text:
unzip yf_data.dat.zip
python long_stress_test.py --seed=1 --nhidden=1000  --init_range=0.1 --log_dir=./results/seed_1_long_stress_test --debug  --use_cuda --use_lstm


text:
class FinalClassificationSerializer < ActiveModel::MongoidSerializer
  attributes :id, :task_key, :started_at, :finished_at, :subject_id, :annotation
  def id
    object._id.to_s
  end
end