<commit_before>import numpy as np
from numba import njit
from math import exp, floor
from typing import Callable

from flare.kernels.cutoffs import quadratic_cutoff

from time import time

def grid_kernel_sephyps(kern_type,
                  data, grids, fj, fdj,
                  c2, etypes2,
                  cutoff_2b, cutoff_3b, cutoff_mb,
                  nspec, spec_mask,
                  nbond, bond_mask,
                  ntriplet, triplet_mask,
                  ncut3b, cut3b_mask,
                  nmb, mb_mask,
                  sig2, ls2, sig3, ls3, sigm, lsm,
                  cutoff_func=quadratic_cutoff):
    '''
    Args:
        data: a single env of a list of envs
    '''

    bc1 = spec_mask[c2]
    bc2 = spec_mask[etypes2[0]]
    bc3 = spec_mask[etypes2[1]]
    ttype = triplet_mask[nspec * nspec * bc1 + nspec*bc2 + bc3]
    ls = ls3[ttype]
    sig = sig3[ttype]
    cutoffs = [cutoff_2b, cutoff_3b] # TODO: check if this is correct

    hyps = [sig, ls]
    return grid_kernel(kern_type,
                   data, grids, fj, fdj,
                   c2, etypes2,
                   hyps, cutoffs, cutoff_func)

def grid_kernel(kern_type,
                struc, grids, fj, fdj,
                c2, etypes2,
                hyps: 'ndarray', cutoffs,
                cutoff_func: Callable = quadratic_cutoff):

    r_cut = cutoffs[1]

    if not isinstance(struc, list):
        struc = [struc]

    kern = 0
    for env in struc:
        kern += grid_kernel_env(kern_type,
                    env, grids, fj, fdj,
                    c2, etypes2,
                    hyps, r_cut, cutoff_func)

    return kern

def grid_kernel_env(kern_type,
                env1, grids, fj, fdj,
                c2, etypes2,
                hyps: 'ndarray', r_cut: float,
                cutoff_func: Callable = quadratic_cutoff):

    # pre-compute constants that appear in the inner loop
    sig = hyps[0]
    ls = hyps[1]
    derivative = derv_dict[kern_type]

    # collect all the triplets in this training env
    triplet_coord_list = get_triplets_for_kern(env1.bond_array_3, env1.ctype, env1.etypes,
        env1.cross_bond_inds, env1.cross_bond_dists, env1.triplet_counts,
        c2, etypes2)

    if len(triplet_coord_list) == 0: # no triplets
        if derivative:
            return np.zeros((3, grids.shape[0]), dtype=np.float64)
        else:
            return np.zeros(grids.shape[0], dtype=np.float64)

    triplet_coord_list = np.array(triplet_coord_list)
    triplet_list = triplet_coord_list[:, :3] # (n_triplets, 3)
    coord_list = triplet_coord_list[:, 3:] # ((n_triplets, 9)
    del triplet_coord_list

    # calculate distance difference & exponential part
    ls1 = 1 / (2 * ls * ls)
    D = 0
    rij_list = []
    for r in range(3):
        rj, ri = np.meshgrid(grids[:, r], triplet_list[:, r])
        rij = ri - rj
        D += rij * rij # (n_triplets, n_grids)
        rij_list.append(rij)

    kern_exp = (sig * sig) * np.exp(- D * ls1)
    del D

    # calculate cutoff of the triplets
    fi, fdi = triplet_cutoff(triplet_list, r_cut, coord_list, derivative,
        cutoff_func) # (n_triplets, 1)
    del triplet_list

    # calculate the derivative part
    kern_func = kern_dict[kern_type]
    kern = kern_func(kern_exp, fi, fj, fdi, fdj,
             rij_list, coord_list, ls)

    return kern

def en_en(kern_exp, fi, fj, *args):
    '''energy map + energy block'''
    fifj = fi @ fj.T # (n_triplets, n_grids)
    kern = np.sum(kern_exp * fifj, axis=0) / 9 # (n_grids,)
    return kern

def en_force(kern_exp, fi, fj, fdi, fdj,
             rij_list, coord_list, ls):
    '''energy map + force block'''
    fifj = fi @ fj.T # (n_triplets, n_grids)
    ls2 = 1 / (ls * ls)
    n_trplt, n_grids = kern_exp.shape
    kern = np.zeros((3, n_grids), dtype=np.float64)
    for d in range(3):
        B = 0
        fdij = fdi[:, [d]] @ fj.T
        for r in range(3):
            rij = rij_list[r]
            # column-wise multiplication
            # coord_list[:, [r]].shape = (n_triplets, 1)
            B += rij * coord_list[:, [3*d+r]] # (n_triplets, n_grids)

        kern[d, :] = - np.sum(kern_exp * (B * ls2 * fifj + fdij), axis=0) / 3 # (n_grids,)
    return kern

def force_en(kern_exp, fi, fj, fdi, fdj,
             rij_list, coord_list, ls):
    '''force map + energy block'''
    ls2 = 1 / (ls * ls)
    fifj = fi @ fj.T # (n_triplets, n_grids)
    fdji = fi @ fdj.T
    # only r = 0 is non zero, since the grid coords are all (1, 0, 0)
    B = rij_list[0] # (n_triplets, n_grids)
    kern = np.sum(kern_exp * (B * ls2 * fifj - fdji), axis=0) / 3 # (n_grids,)
    return kern

def force_force(kern_exp, fi, fj, fdi, fdj,
                rij_list, coord_list, ls):
    '''force map + force block'''
    ls2 = 1 / (ls * ls)
    ls3 = ls2 * ls2

    n_trplt, n_grids = kern_exp.shape
    kern = np.zeros((3, n_grids), dtype=np.float64)

    fifj = fi @ fj.T # (n_triplets, n_grids)
    fdji = (fi * ls2) @ fdj.T

    B = rij_list[0] * ls3 # B and C both have opposite signs with that in the three_body_helper_1

    for d in range(3):
        fdij = (fdi[:, [d]] * ls2) @ fj.T
        I = fdi[:, [d]] @ fdj.T
        J = rij_list[0] * fdij # (n_triplets, n_grids)

        A = np.repeat(ls2 * coord_list[:, [3*d]], n_grids, axis=1)
        C = 0
        for r in range(3):
            rij = rij_list[r]
            # column-wise multiplication
            # coord_list[:, [r]].shape = (n_triplets, 1)
            C += rij * coord_list[:, [3*d+r]] # (n_triplets, n_grids)

        IJKL = I - J + C * fdji + (A - B * C) * fifj
        kern[d, :] = np.sum(kern_exp * IJKL, axis=0)

    return kern

def triplet_cutoff(triplets, r_cut, coords, derivative=False, cutoff_func=quadratic_cutoff):

    dfj_list = np.zeros((len(triplets), 3), dtype=np.float64)

    if derivative:
        for d in range(3):
            s = 3 * d
            e = 3 * (d + 1)
            f0, df0 = cutoff_func(r_cut, triplets, coords[:, s:e])
            dfj = df0[:, 0] *  f0[:, 1] *  f0[:, 2] + \
                   f0[:, 0] * df0[:, 1] *  f0[:, 2] + \
                   f0[:, 0] *  f0[:, 1] * df0[:, 2]
            dfj_list[:, d] = dfj
    else:
        f0, _ = cutoff_func(r_cut, triplets, 0) # (n_grid, 3)

    fj = f0[:, 0] * f0[:, 1] * f0[:, 2] # (n_grid,)
    fj = np.expand_dims(fj, axis=1)

    return fj, dfj_list

@njit
def get_permutations(c1, ei1, ei2, c2, ej1, ej2):
    perms = []
    if (c1 == c2):
        if (ei1 == ej1) and (ei2 == ej2): perms.append([0, 1, 2])
        if (ei1 == ej2) and (ei2 == ej1): perms.append([1, 0, 2])
    if (c1 == ej1):
        if (ei1 == ej2) and (ei2 == c2):  perms.append([1, 2, 0])
        if (ei1 == c2) and (ei2 == ej2):  perms.append([0, 2, 1])
    if (c1 == ej2):
        if (ei1 == ej1) and (ei2 == c2):  perms.append([2, 1, 0])
        if (ei1 == c2) and (ei2 == ej1):  perms.append([2, 0, 1])
    return perms

@njit
def get_triplets_for_kern(bond_array_1, c1, etypes1,
                          cross_bond_inds_1, cross_bond_dists_1,
                          triplets_1,
                          c2, etypes2):

    #triplet_list = np.empty((0, 6), dtype=np.float64)
    triplet_list = []

    ej1 = etypes2[0]
    ej2 = etypes2[1]

    all_spec = [c2, ej1, ej2]
    if c1 in all_spec:
        c1_ind = all_spec.index(c1)
        ind_list = [0, 1, 2]
        ind_list.remove(c1_ind)
        all_spec.remove(c1)

        for m in range(bond_array_1.shape[0]):
            two_inds = ind_list.copy()

            ri1 = bond_array_1[m, 0]
            ci1 = bond_array_1[m, 1:]
            ei1 = etypes1[m]

            two_spec = [all_spec[0], all_spec[1]]
            if (ei1 in two_spec):

                ei1_ind = ind_list[0] if ei1 == two_spec[0] else ind_list[1]
                two_spec.remove(ei1)
                two_inds.remove(ei1_ind)
                one_spec = two_spec[0]
                ei2_ind = two_inds[0]

                for n in range(triplets_1[m]):
                    ind1 = cross_bond_inds_1[m, m + n + 1]
                    ei2 = etypes1[ind1]
                    if (ei2 == one_spec):

                        ri2 = bond_array_1[ind1, 0]
                        ci2 = bond_array_1[ind1, 1:]

                        ri3 = cross_bond_dists_1[m, m + n + 1]
                        ci3 = np.zeros(3)

                        perms = get_permutations(c1, ei1, ei2, c2, ej1, ej2)

                        tri  = np.array([ri1, ri2, ri3])
                        crd1 = np.array([ci1[0], ci2[0], ci3[0]])
                        crd2 = np.array([ci1[1], ci2[1], ci3[1]])
                        crd3 = np.array([ci1[2], ci2[2], ci3[2]])

                        # append permutations
                        nperm = len(perms)
                        for iperm in range(nperm):
                            perm = perms[iperm]
                            tricrd = np.take(tri, perm)
                            crd1_p = np.take(crd1, perm)
                            crd2_p = np.take(crd2, perm)
                            crd3_p = np.take(crd3, perm)
                            tricrd = np.hstack((tricrd, crd1_p, crd2_p, crd3_p))
                            triplet_list.append(tricrd)

    return triplet_list

def self_kernel_sephyps(map_force,
                  grids, fj, fdj,
                  c2, etypes2,
                  cutoff_2b, cutoff_3b, cutoff_mb,
                  nspec, spec_mask,
                  nbond, bond_mask,
                  ntriplet, triplet_mask,
                  ncut3b, cut3b_mask,
                  nmb, mb_mask,
                  sig2, ls2, sig3, ls3, sigm, lsm,
                  cutoff_func=quadratic_cutoff):
    '''
    Args:
        data: a single env of a list of envs
    '''

    bc1 = spec_mask[c2]
    bc2 = spec_mask[etypes2[0]]
    bc3 = spec_mask[etypes2[1]]
    ttype = triplet_mask[nspec * nspec * bc1 + nspec*bc2 + bc3]
    ls = ls3[ttype]
    sig = sig3[ttype]
    cutoffs = [cutoff_2b, cutoff_3b]

    hyps = [sig, ls]
    return self_kernel(map_force, grids, c2, etypes2, hyps,
                       cutoffs, cutoff_func)

def self_kernel(map_force, grids, fj, fdj, c2, etypes2, hyps, cutoffs,
                cutoff_func: Callable = quadratic_cutoff):

    kern = 0

    # pre-compute constants
    r_cut = cutoffs[1]
    sig = hyps[0]
    ls = hyps[1]
    sig2 = sig * sig
    ls1 = 1 / (2 * ls * ls)
    ls2 = 1 / (ls * ls)
    ls3 = ls2 * ls2

    ej1 = etypes2[0]
    ej2 = etypes2[1]

    perm_list = get_permutations(c2, ej1, ej2, c2, ej1, ej2)
    coord = np.array([1., 0., 0.])

    for perm in perm_list:
        perm_grids = np.take(grids, perm, axis=1)
        rij = grids - perm_grids
        D = np.sum(rij * rij, axis=1) # (n_grids, ) adding up three bonds
        kern_exp = np.exp(-D * ls1) * sig2
        fjfj = fj ** 2
        if map_force:
            crd = np.take(coord, perm)
            A = ls2 * crd[0]
            B = rij[:, [0]]
            C = rij[:, [0]] * crd[0]

            I = fdj ** 2
            J = B * ls2 * fj * fdj
            K = - C * ls2 * fdj * fj
            L = (A - B * C * ls3) * fjfj
            IJKL = np.sum(I + J + K + L, axis=1)
            kern += IJKL * kern_exp
        else:
            kern += kern_exp * fjfj / 9 # (n_grids,)

    return kern

kern_dict = {'energy_energy': en_en,
             'energy_force': en_force,
             'force_energy': force_en,
             'force_force': force_force}

derv_dict = {'energy_energy': False,
             'energy_force': True,
             'force_energy': False,
             'force_force': True}

<commit_msg>debugged self_kern gengrid<commit_after>import numpy as np
from numba import njit
from math import exp, floor
from typing import Callable

from flare.kernels.cutoffs import quadratic_cutoff

from time import time

def grid_kernel_sephyps(kern_type,
                  data, grids, fj, fdj,
                  c2, etypes2,
                  cutoff_2b, cutoff_3b, cutoff_mb,
                  nspec, spec_mask,
                  nbond, bond_mask,
                  ntriplet, triplet_mask,
                  ncut3b, cut3b_mask,
                  nmb, mb_mask,
                  sig2, ls2, sig3, ls3, sigm, lsm,
                  cutoff_func=quadratic_cutoff):
    '''
    Args:
        data: a single env of a list of envs
    '''

    bc1 = spec_mask[c2]
    bc2 = spec_mask[etypes2[0]]
    bc3 = spec_mask[etypes2[1]]
    ttype = triplet_mask[nspec * nspec * bc1 + nspec*bc2 + bc3]
    ls = ls3[ttype]
    sig = sig3[ttype]
    cutoffs = [cutoff_2b, cutoff_3b] # TODO: check if this is correct

    hyps = [sig, ls]
    return grid_kernel(kern_type,
                   data, grids, fj, fdj,
                   c2, etypes2,
                   hyps, cutoffs, cutoff_func)

def grid_kernel(kern_type,
                struc, grids, fj, fdj,
                c2, etypes2,
                hyps: 'ndarray', cutoffs,
                cutoff_func: Callable = quadratic_cutoff):

    r_cut = cutoffs[1]

    if not isinstance(struc, list):
        struc = [struc]

    kern = 0
    for env in struc:
        kern += grid_kernel_env(kern_type,
                    env, grids, fj, fdj,
                    c2, etypes2,
                    hyps, r_cut, cutoff_func)

    return kern

def grid_kernel_env(kern_type,
                env1, grids, fj, fdj,
                c2, etypes2,
                hyps: 'ndarray', r_cut: float,
                cutoff_func: Callable = quadratic_cutoff):

    # pre-compute constants that appear in the inner loop
    sig = hyps[0]
    ls = hyps[1]
    derivative = derv_dict[kern_type]

    # collect all the triplets in this training env
    triplet_coord_list = get_triplets_for_kern(env1.bond_array_3, env1.ctype, env1.etypes,
        env1.cross_bond_inds, env1.cross_bond_dists, env1.triplet_counts,
        c2, etypes2)

    if len(triplet_coord_list) == 0: # no triplets
        if derivative:
            return np.zeros((3, grids.shape[0]), dtype=np.float64)
        else:
            return np.zeros(grids.shape[0], dtype=np.float64)

    triplet_coord_list = np.array(triplet_coord_list)
    triplet_list = triplet_coord_list[:, :3] # (n_triplets, 3)
    coord_list = triplet_coord_list[:, 3:] # ((n_triplets, 9)
    del triplet_coord_list

    # calculate distance difference & exponential part
    ls1 = 1 / (2 * ls * ls)
    D = 0
    rij_list = []
    for r in range(3):
        rj, ri = np.meshgrid(grids[:, r], triplet_list[:, r])
        rij = ri - rj
        D += rij * rij # (n_triplets, n_grids)
        rij_list.append(rij)

    kern_exp = (sig * sig) * np.exp(- D * ls1)
    del D

    # calculate cutoff of the triplets
    fi, fdi = triplet_cutoff(triplet_list, r_cut, coord_list, derivative,
        cutoff_func) # (n_triplets, 1)
    del triplet_list

    # calculate the derivative part
    kern_func = kern_dict[kern_type]
    kern = kern_func(kern_exp, fi, fj, fdi, fdj,
             rij_list, coord_list, ls)

    return kern

def en_en(kern_exp, fi, fj, *args):
    '''energy map + energy block'''
    fifj = fi @ fj.T # (n_triplets, n_grids)
    kern = np.sum(kern_exp * fifj, axis=0) / 9 # (n_grids,)
    return kern

def en_force(kern_exp, fi, fj, fdi, fdj,
             rij_list, coord_list, ls):
    '''energy map + force block'''
    fifj = fi @ fj.T # (n_triplets, n_grids)
    ls2 = 1 / (ls * ls)
    n_trplt, n_grids = kern_exp.shape
    kern = np.zeros((3, n_grids), dtype=np.float64)
    for d in range(3):
        B = 0
        fdij = fdi[:, [d]] @ fj.T
        for r in range(3):
            rij = rij_list[r]
            # column-wise multiplication
            # coord_list[:, [r]].shape = (n_triplets, 1)
            B += rij * coord_list[:, [3*d+r]] # (n_triplets, n_grids)

        kern[d, :] = - np.sum(kern_exp * (B * ls2 * fifj + fdij), axis=0) / 3 # (n_grids,)
    return kern

def force_en(kern_exp, fi, fj, fdi, fdj,
             rij_list, coord_list, ls):
    '''force map + energy block'''
    ls2 = 1 / (ls * ls)
    fifj = fi @ fj.T # (n_triplets, n_grids)
    fdji = fi @ fdj.T
    # only r = 0 is non zero, since the grid coords are all (1, 0, 0)
    B = rij_list[0] # (n_triplets, n_grids)
    kern = np.sum(kern_exp * (B * ls2 * fifj - fdji), axis=0) / 3 # (n_grids,)
    return kern

def force_force(kern_exp, fi, fj, fdi, fdj,
                rij_list, coord_list, ls):
    '''force map + force block'''
    ls2 = 1 / (ls * ls)
    ls3 = ls2 * ls2

    n_trplt, n_grids = kern_exp.shape
    kern = np.zeros((3, n_grids), dtype=np.float64)

    fifj = fi @ fj.T # (n_triplets, n_grids)
    fdji = (fi * ls2) @ fdj.T

    B = rij_list[0] * ls3 # B and C both have opposite signs with that in the three_body_helper_1

    for d in range(3):
        fdij = (fdi[:, [d]] * ls2) @ fj.T
        I = fdi[:, [d]] @ fdj.T
        J = rij_list[0] * fdij # (n_triplets, n_grids)

        A = np.repeat(ls2 * coord_list[:, [3*d]], n_grids, axis=1)
        C = 0
        for r in range(3):
            rij = rij_list[r]
            # column-wise multiplication
            # coord_list[:, [r]].shape = (n_triplets, 1)
            C += rij * coord_list[:, [3*d+r]] # (n_triplets, n_grids)

        IJKL = I - J + C * fdji + (A - B * C) * fifj
        kern[d, :] = np.sum(kern_exp * IJKL, axis=0)

    return kern

def triplet_cutoff(triplets, r_cut, coords, derivative=False, cutoff_func=quadratic_cutoff):

    dfj_list = np.zeros((len(triplets), 3), dtype=np.float64)

    if derivative:
        for d in range(3):
            s = 3 * d
            e = 3 * (d + 1)
            f0, df0 = cutoff_func(r_cut, triplets, coords[:, s:e])
            dfj = df0[:, 0] *  f0[:, 1] *  f0[:, 2] + \
                   f0[:, 0] * df0[:, 1] *  f0[:, 2] + \
                   f0[:, 0] *  f0[:, 1] * df0[:, 2]
            dfj_list[:, d] = dfj
    else:
        f0, _ = cutoff_func(r_cut, triplets, 0) # (n_grid, 3)

    fj = f0[:, 0] * f0[:, 1] * f0[:, 2] # (n_grid,)
    fj = np.expand_dims(fj, axis=1)

    return fj, dfj_list

@njit
def get_permutations(c1, ei1, ei2, c2, ej1, ej2):
    perms = []
    if (c1 == c2):
        if (ei1 == ej1) and (ei2 == ej2): perms.append([0, 1, 2])
        if (ei1 == ej2) and (ei2 == ej1): perms.append([1, 0, 2])
    if (c1 == ej1):
        if (ei1 == ej2) and (ei2 == c2):  perms.append([1, 2, 0])
        if (ei1 == c2) and (ei2 == ej2):  perms.append([0, 2, 1])
    if (c1 == ej2):
        if (ei1 == ej1) and (ei2 == c2):  perms.append([2, 1, 0])
        if (ei1 == c2) and (ei2 == ej1):  perms.append([2, 0, 1])
    return perms

@njit
def get_triplets_for_kern(bond_array_1, c1, etypes1,
                          cross_bond_inds_1, cross_bond_dists_1,
                          triplets_1,
                          c2, etypes2):

    #triplet_list = np.empty((0, 6), dtype=np.float64)
    triplet_list = []

    ej1 = etypes2[0]
    ej2 = etypes2[1]

    all_spec = [c2, ej1, ej2]
    if c1 in all_spec:
        c1_ind = all_spec.index(c1)
        ind_list = [0, 1, 2]
        ind_list.remove(c1_ind)
        all_spec.remove(c1)

        for m in range(bond_array_1.shape[0]):
            two_inds = ind_list.copy()

            ri1 = bond_array_1[m, 0]
            ci1 = bond_array_1[m, 1:]
            ei1 = etypes1[m]

            two_spec = [all_spec[0], all_spec[1]]
            if (ei1 in two_spec):

                ei1_ind = ind_list[0] if ei1 == two_spec[0] else ind_list[1]
                two_spec.remove(ei1)
                two_inds.remove(ei1_ind)
                one_spec = two_spec[0]
                ei2_ind = two_inds[0]

                for n in range(triplets_1[m]):
                    ind1 = cross_bond_inds_1[m, m + n + 1]
                    ei2 = etypes1[ind1]
                    if (ei2 == one_spec):

                        ri2 = bond_array_1[ind1, 0]
                        ci2 = bond_array_1[ind1, 1:]

                        ri3 = cross_bond_dists_1[m, m + n + 1]
                        ci3 = np.zeros(3)

                        perms = get_permutations(c1, ei1, ei2, c2, ej1, ej2)

                        tri  = np.array([ri1, ri2, ri3])
                        crd1 = np.array([ci1[0], ci2[0], ci3[0]])
                        crd2 = np.array([ci1[1], ci2[1], ci3[1]])
                        crd3 = np.array([ci1[2], ci2[2], ci3[2]])

                        # append permutations
                        nperm = len(perms)
                        for iperm in range(nperm):
                            perm = perms[iperm]
                            tricrd = np.take(tri, perm)
                            crd1_p = np.take(crd1, perm)
                            crd2_p = np.take(crd2, perm)
                            crd3_p = np.take(crd3, perm)
                            tricrd = np.hstack((tricrd, crd1_p, crd2_p, crd3_p))
                            triplet_list.append(tricrd)

    return triplet_list

def self_kernel_sephyps(map_force,
                  grids, fj, fdj,
                  c2, etypes2,
                  cutoff_2b, cutoff_3b, cutoff_mb,
                  nspec, spec_mask,
                  nbond, bond_mask,
                  ntriplet, triplet_mask,
                  ncut3b, cut3b_mask,
                  nmb, mb_mask,
                  sig2, ls2, sig3, ls3, sigm, lsm,
                  cutoff_func=quadratic_cutoff):
    '''
    Args:
        data: a single env of a list of envs
    '''

    bc1 = spec_mask[c2]
    bc2 = spec_mask[etypes2[0]]
    bc3 = spec_mask[etypes2[1]]
    ttype = triplet_mask[nspec * nspec * bc1 + nspec*bc2 + bc3]
    ls = ls3[ttype]
    sig = sig3[ttype]
    cutoffs = [cutoff_2b, cutoff_3b]

    hyps = [sig, ls]
    return self_kernel(map_force, grids, c2, etypes2, hyps,
                       cutoffs, cutoff_func)

def self_kernel(map_force, grids, fj, fdj, c2, etypes2, hyps, cutoffs,
                cutoff_func: Callable = quadratic_cutoff):

    kern = 0

    # pre-compute constants
    r_cut = cutoffs[1]
    sig = hyps[0]
    ls = hyps[1]
    sig2 = sig * sig
    ls1 = 1 / (2 * ls * ls)
    ls2 = 1 / (ls * ls)
    ls3 = ls2 * ls2

    ej1 = etypes2[0]
    ej2 = etypes2[1]

    perm_list = get_permutations(c2, ej1, ej2, c2, ej1, ej2)
    ci = np.array([1., 0., 0.])

    for perm in perm_list:
        perm_grids = np.take(grids, perm, axis=1)
        rij = grids - perm_grids
        D = np.sum(rij * rij, axis=1) # (n_grids, ) adding up three bonds
        kern_exp = np.exp(-D * ls1) * sig2
        fjfj = fj ** 2
        if map_force:
            cj = np.take(ci, perm)
            A = ci[0] * cj[0]
            B = rij[:, [0]] * ci[0]
            C = 0
            for d in range(3):
                C += rij[:, [d]] * cj[d]

            I = fdj ** 2
            J = B * ls2 * fj * fdj
            K = - C * ls2 * fdj * fj
            L = (A * ls2 - B * C * ls3) * fjfj
            IJKL = np.sum(I + J + K + L, axis=1)
            kern += IJKL * kern_exp
        else:
            kern += kern_exp * fjfj / 9 # (n_grids,)

    return kern

kern_dict = {'energy_energy': en_en,
             'energy_force': en_force,
             'force_energy': force_en,
             'force_force': force_force}

derv_dict = {'energy_energy': False,
             'energy_force': True,
             'force_energy': False,
             'force_force': True}

<|endoftext|>


<commit_before># coding: utf-8
"""
    flask.ext.makestatic
    ~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2013 by Daniel Neuhäuser
    :license: BSD, see LICENSE.rst for details
"""
import os
import re
import warnings
import subprocess
try:
    from configparser import RawConfigParser, ParsingError
except ImportError:
    from ConfigParser import RawConfigParser, ParsingError

from flask.ext.makestatic._compat import iteritems, PY2
from flask.ext.makestatic.watcher import ThreadedWatcher

class RuleMissing(Warning):
    """
    Warning that is emitted if a rule cannot be found.
    """

class MakeStatic(object):
    """
    This class provides the extension interface. You can use it by calling it
    directly with a :class:`flask.Flask` instance::

        app = Flask(__name__)
        make_static = MakeStatic(app)

    Once initialized :class:`MakeStatic` will parse the `assets.cfg`
    configuration file corresponding to the initialized application.
    """
    def __init__(self, app):
        self.app = app

        with self.app.open_resource('assets.cfg', 'r') as config_file:
            self.matcher, self.rulesets = self.parse_config(config_file)

    def parse_config(self, config_file):
        parser = RawConfigParser(allow_no_value=True)
        if PY2:
            parser.readfp(config_file)
        else:
            parser.read_file(config_file)
        config = {}
        for file_regex in parser.sections():
            for rule in parser.options(file_regex):
                if parser.get(file_regex, rule) is not None:
                    raise ParsingError('unexpected value for %r' % file_regex)
                config.setdefault(file_regex, []).append(rule)

        file_regexes = []
        rulesets = []
        for file_regex, rules in iteritems(config):
            file_regexes.append('(%s)' % file_regex)
            rulesets.append(rules)
        matcher = re.compile('^%s$' % '|'.join(file_regexes)).match
        return matcher, rulesets

    def get_rules(self, filename):
        match = self.matcher(filename)
        if match:
            return self.rulesets[match.lastindex - 1]

    def watch(self, sleep=0.1):
        """
        Starts a daemon thread that watches the `static` directory for changes
        and calls :meth:`compile` if any occur.

        Returns a :class:`ThreadedWatcher`, if you want to turn of watching you
        can call :meth:`ThreadedWatcher.stop`.

        :param sleep: The amount of time in seconds that should be slept
                      between checks for changes, may be ignored.
        """
        watcher = ThreadedWatcher()
        for signal in [watcher.file_added, watcher.file_modified, watcher.file_removed]:
            signal.connect(lambda f: self.compile)
        watcher.add_directory(self.app.static_folder)
        watcher.watch(sleep=sleep)
        self.compile() # initial compile
        return watcher

    def compile(self):
        """
        Compiles all assets to static files in one go.

        This works only when done within an application context of an
        initialized application.

        Emits a :class:`RuleMissing` warning for each file in `assets` for
        which no rule exists.
        """
        assets = os.path.join(self.app.root_path, 'assets')
        for root, _, files in os.walk(assets):
            for file in files:
                file = os.path.join(root, file)
                relative_file = os.path.relpath(file, assets)
                rules = self.get_rules(relative_file)
                if rules is None:
                    warnings.warn(
                        'cannot find a rule for %s' % relative_file,
                        RuleMissing,
                    )
                else:
                    for rule in rules:
                        subprocess.check_call(
                            rule.format(
                                asset=file,
                                static=os.path.join(self.app.static_folder,
                                                    relative_file),
                                static_dir=self.app.static_folder
                            ),
                            shell=True
                        )

__all__ = ['MakeStatic']
<commit_msg>Be more efficient about compiling changed files<commit_after># coding: utf-8
"""
    flask.ext.makestatic
    ~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2013 by Daniel Neuhäuser
    :license: BSD, see LICENSE.rst for details
"""
import os
import re
import warnings
import subprocess
try:
    from configparser import RawConfigParser, ParsingError
except ImportError:
    from ConfigParser import RawConfigParser, ParsingError

from flask.ext.makestatic._compat import iteritems, PY2
from flask.ext.makestatic.watcher import ThreadedWatcher

class RuleMissing(Warning):
    """
    Warning that is emitted if a rule cannot be found.
    """

class MakeStatic(object):
    """
    This class provides the extension interface. You can use it by calling it
    directly with a :class:`flask.Flask` instance::

        app = Flask(__name__)
        make_static = MakeStatic(app)

    Once initialized :class:`MakeStatic` will parse the `assets.cfg`
    configuration file corresponding to the initialized application.
    """
    def __init__(self, app):
        self.app = app

        with self.app.open_resource('assets.cfg', 'r') as config_file:
            self.matcher, self.rulesets = self.parse_config(config_file)

    @property
    def assets_folder(self):
        return os.path.join(self.app.root_path, 'assets')

    def parse_config(self, config_file):
        parser = RawConfigParser(allow_no_value=True)
        if PY2:
            parser.readfp(config_file)
        else:
            parser.read_file(config_file)
        config = {}
        for file_regex in parser.sections():
            for rule in parser.options(file_regex):
                if parser.get(file_regex, rule) is not None:
                    raise ParsingError('unexpected value for %r' % file_regex)
                config.setdefault(file_regex, []).append(rule)

        file_regexes = []
        rulesets = []
        for file_regex, rules in iteritems(config):
            file_regexes.append('(%s)' % file_regex)
            rulesets.append(rules)
        matcher = re.compile('^%s$' % '|'.join(file_regexes)).match
        return matcher, rulesets

    def get_rules(self, filename):
        match = self.matcher(filename)
        if match:
            return self.rulesets[match.lastindex - 1]

    def watch(self, sleep=0.1):
        """
        Starts a daemon thread that watches the `static` directory for changes
        and calls :meth:`compile` if any occur.

        Returns a :class:`ThreadedWatcher`, if you want to turn of watching you
        can call :meth:`ThreadedWatcher.stop`.

        :param sleep: The amount of time in seconds that should be slept
                      between checks for changes, may be ignored.
        """
        watcher = ThreadedWatcher()
        for signal in [watcher.file_added, watcher.file_modified]:
            signal.connect(self.compile_asset)
        watcher.add_directory(self.app.static_folder)
        watcher.watch(sleep=sleep)
        self.compile() # initial compile
        return watcher

    def compile(self):
        """
        Compiles all assets to static files in one go.

        This works only when done within an application context of an
        initialized application.

        Emits a :class:`RuleMissing` warning for each file in `assets` for
        which no rule exists.
        """
        for root, _, files in os.walk(self.assets_folder):
            for file in files:
                self.compile_asset(os.path.join(root, file))

    def compile_asset(self, filename):
        relative_filename = os.path.relpath(filename, self.assets_folder)
        rules = self.get_rules(relative_filename)
        if rules is None:
            warnings.warn(
                'cannot find a rule for %s' % relative_filename,
                RuleMissing,
            )
        else:
            for rule in rules:
                subprocess.check_call(
                    rule.format(
                        asset=filename,
                        static=os.path.join(self.app.static_folder,
                                            relative_filename),
                        static_dir=self.app.static_folder
                    ),
                    shell=True
                )

__all__ = ['MakeStatic']
<|endoftext|>


<commit_before>"""Record `Nest API <https://developer.nest.com/documentation/api-reference>`_ data to InfluxDB.

The ``record`` API is designed to persist Nest thermostat data in an `InfluxDB <http://influxdb.com/>`_ database.

.. note::

   The environment variable ``DEN_ACCESS_TOKEN`` is required to authenticate with the Nest API.  Please refer to the
   Nest API `access token
   <https://developer.nest.com/documentation/cloud/how-to-auth#exchange-your-authorization-code-for-an-access-token>`_
   documentation for instructions on how to generate this value.

:raises: :py:exc:`~exceptions.KeyError` if the environment variable ``DEN_ACCESS_TOKEN`` is missing.

"""

from contextlib import closing
from urllib import urlencode
from urlparse import SplitResult, urlunsplit

import json
import logging
import os

from influxdb import client as influxdb

import requests

try:
    NEST_API_ACCESS_TOKEN = os.environ["DEN_ACCESS_TOKEN"]
    """The Nest API `access token
    <https://developer.nest.com/documentation/cloud/how-to-auth#exchange-your-authorization-code-for-an-access-token>`_
    value.

    This value is retrieved from the required environment variable ``DEN_ACCESS_TOKEN``.

    """
except KeyError:
    raise KeyError("Please set the environment variable 'DEN_ACCESS_TOKEN'.")

NEST_API_PROTOCOL = "https"
NEST_API_LOCATION = "developer-api.nest.com"

STREAM_DELIMITER = ":"

def _get_api_url(path=""):
    """Get a Nest API URL for the given path."""
    query = urlencode({"auth": NEST_API_ACCESS_TOKEN})
    split = SplitResult(scheme=NEST_API_PROTOCOL, netloc=NEST_API_LOCATION, path=path.strip("/"), query=query,
                        fragment="")
    return urlunsplit(split)

def _get_stream(path=""):
    """Make a GET request to the Nest REST stream API and return the response object."""
    url = _get_api_url(path)
    r = requests.get(url, headers={"Accept": "text/event-stream"}, stream=True)
    for h in r.history:
        logging.debug("[%d] Redirect: %s", h.status_code, h.url)
    logging.debug("[%d] URL: %s", r.status_code, r.url)
    r.raise_for_status()
    return r

def _is_event(line):
    """Determine if ``line`` represents an event."""
    return line.startswith("event" + STREAM_DELIMITER)

def _is_data(line):
    """Determine if ``line`` represents data."""
    return line.startswith("data" + STREAM_DELIMITER)

def _process_event(line):
    """Process the given event line."""
    event = None
    if STREAM_DELIMITER in line:
        _, event = line.split(STREAM_DELIMITER, 1)
        event = event.strip()
        if event:
            logging.debug(event)
            if event == "keep-alive":
                event = None
        else:
            event = None
    return event

def _process_data(line):
    """Process the given data line."""
    data = None
    if STREAM_DELIMITER in line:
        _, data_str = line.split(STREAM_DELIMITER, 1)
        data_str = data_str.strip()
        if data_str:
            try:
                data = json.loads(data_str)
            except ValueError, e:
                logging.error("Error processing data line: '%s', '%s'", line, e)
                data = None
    return data

def _process(line):
    """Process the given line."""
    if _is_event(line):
        _process_event(line)
    return _process_data(line) if _is_data(line) else None

def _get_structures(data):
    """Get structure data from the given data dict."""
    structures = []
    try:
        structures = data["data"]["structures"].values()
    except TypeError:
        logging.error("Invalid data: '%s'", data)
    except KeyError:
        logging.error("No structures found in data: '%s'", data)
    return structures

def _get_structure_data(data):
    """Get structure data to write to InfluxDB."""
    name = "structures"
    structures = _get_structures(data)
    points = []
    for s in structures:
        try:
            del s["thermostats"]
        except KeyError:  # pylint: disable=pointless-except
            pass
        points.append(s.values())
    columns = structures[0].keys() if structures else []
    return [{"name": name, "columns": columns, "points": points}]

def _get_thermostats(data):
    """Get thermostat data from the given data dict."""
    thermostats = []
    try:
        thermostats = data["data"]["devices"]["thermostats"].values()
    except TypeError:
        logging.error("Invalid data: '%s'", data)
    except KeyError:
        logging.error("No thermostats found in data: '%s'", data)
    return thermostats

def _get_thermostat_data(data):
    """Get thermostat data to write to InfluxDB."""
    name = "thermostats"
    thermostats = _get_thermostats(data)
    columns = []
    points = []
    if thermostats:
        columns = thermostats[0].keys()
        points = [t.values() for t in thermostats]
    return [{"name": name, "columns": columns, "points": points}]

def configure_logging(filename="den.log", level=logging.DEBUG):
    """Configure basic logging.

    :param str filename: (optional) The filename to write logging statements to.
    :param logging.Logger.debug level: (optional) The level at which to log.

    """
    logging.basicConfig(filename=filename, level=level, format="%(asctime)s %(levelname)s %(message)s")

def record(database, port, ssl):
    """Stream results from the Nest API and record them in the database.

    :param str database: The name of the database.
    :param int port: The port number the database is listening on.
    :param bool ssl: Whether or not to use SSL to communicate with the database.
    :rtype: :py:const:`None`
    :return: When the stream opened to the Nest API has been consumed.
    :raises: :exc:`requests.exceptions.StreamConsumedError`: if the stream has been consumed.
    :raises: :exc:`requests.exceptions.ConnectionError`: if the Nest API cannot be reached.
    :raises: :exc:`requests.exceptions.HTTPError`: if an invalid response is returned from the Nest API.
    :raises: :exc:`requests.exceptions.Timeout`: if the request to the Nest API takes too long to respond.

    """
    db = influxdb.InfluxDBClient(database=database, port=port, ssl=ssl)
    with closing(_get_stream()) as stream:
        logging.debug("[%d] Streaming %s", stream.status_code, stream.url)
        for l in stream.iter_lines():
            if l:
                value = _process(l)
                if value:
                    logging.info(value)
                    db.write_points(_get_structure_data(value))
                    db.write_points(_get_thermostat_data(value))
        logging.debug("[%d] Streaming complete %s", stream.status_code, stream.url)
<commit_msg>record - Fixup whitespace.<commit_after>"""Record `Nest API <https://developer.nest.com/documentation/api-reference>`_ data to InfluxDB.

The ``record`` API is designed to persist Nest thermostat data in an `InfluxDB <http://influxdb.com/>`_ database.

.. note::

   The environment variable ``DEN_ACCESS_TOKEN`` is required to authenticate with the Nest API.  Please refer to the
   Nest API `access token
   <https://developer.nest.com/documentation/cloud/how-to-auth#exchange-your-authorization-code-for-an-access-token>`_
   documentation for instructions on how to generate this value.

:raises: :py:exc:`~exceptions.KeyError` if the environment variable ``DEN_ACCESS_TOKEN`` is missing.

"""

from contextlib import closing
from urllib import urlencode
from urlparse import SplitResult, urlunsplit

import json
import logging
import os

from influxdb import client as influxdb

import requests

try:
    NEST_API_ACCESS_TOKEN = os.environ["DEN_ACCESS_TOKEN"]
    """The Nest API `access token
    <https://developer.nest.com/documentation/cloud/how-to-auth#exchange-your-authorization-code-for-an-access-token>`_
    value.

    This value is retrieved from the required environment variable ``DEN_ACCESS_TOKEN``.

    """
except KeyError:
    raise KeyError("Please set the environment variable 'DEN_ACCESS_TOKEN'.")

NEST_API_PROTOCOL = "https"
NEST_API_LOCATION = "developer-api.nest.com"

STREAM_DELIMITER = ":"

def _get_api_url(path=""):
    """Get a Nest API URL for the given path."""
    query = urlencode({"auth": NEST_API_ACCESS_TOKEN})
    split = SplitResult(scheme=NEST_API_PROTOCOL, netloc=NEST_API_LOCATION, path=path.strip("/"), query=query,
                        fragment="")
    return urlunsplit(split)

def _get_stream(path=""):
    """Make a GET request to the Nest REST stream API and return the response object."""
    url = _get_api_url(path)
    r = requests.get(url, headers={"Accept": "text/event-stream"}, stream=True)
    for h in r.history:
        logging.debug("[%d] Redirect: %s", h.status_code, h.url)
    logging.debug("[%d] URL: %s", r.status_code, r.url)
    r.raise_for_status()
    return r

def _is_event(line):
    """Determine if ``line`` represents an event."""
    return line.startswith("event" + STREAM_DELIMITER)

def _is_data(line):
    """Determine if ``line`` represents data."""
    return line.startswith("data" + STREAM_DELIMITER)

def _process_event(line):
    """Process the given event line."""
    event = None
    if STREAM_DELIMITER in line:
        _, event = line.split(STREAM_DELIMITER, 1)
        event = event.strip()
        if event:
            logging.debug(event)
            if event == "keep-alive":
                event = None
        else:
            event = None
    return event

def _process_data(line):
    """Process the given data line."""
    data = None
    if STREAM_DELIMITER in line:
        _, data_str = line.split(STREAM_DELIMITER, 1)
        data_str = data_str.strip()
        if data_str:
            try:
                data = json.loads(data_str)
            except ValueError, e:
                logging.error("Error processing data line: '%s', '%s'", line, e)
                data = None
    return data

def _process(line):
    """Process the given line."""
    if _is_event(line):
        _process_event(line)
    return _process_data(line) if _is_data(line) else None

def _get_structures(data):
    """Get structure data from the given data dict."""
    structures = []
    try:
        structures = data["data"]["structures"].values()
    except TypeError:
        logging.error("Invalid data: '%s'", data)
    except KeyError:
        logging.error("No structures found in data: '%s'", data)
    return structures

def _get_structure_data(data):
    """Get structure data to write to InfluxDB."""
    name = "structures"
    structures = _get_structures(data)
    points = []
    for s in structures:
        try:
            del s["thermostats"]
        except KeyError:  # pylint: disable=pointless-except
            pass
        points.append(s.values())
    columns = structures[0].keys() if structures else []
    return [{"name": name, "columns": columns, "points": points}]

def _get_thermostats(data):
    """Get thermostat data from the given data dict."""
    thermostats = []
    try:
        thermostats = data["data"]["devices"]["thermostats"].values()
    except TypeError:
        logging.error("Invalid data: '%s'", data)
    except KeyError:
        logging.error("No thermostats found in data: '%s'", data)
    return thermostats

def _get_thermostat_data(data):
    """Get thermostat data to write to InfluxDB."""
    name = "thermostats"
    thermostats = _get_thermostats(data)
    columns = []
    points = []
    if thermostats:
        columns = thermostats[0].keys()
        points = [t.values() for t in thermostats]
    return [{"name": name, "columns": columns, "points": points}]

def configure_logging(filename="den.log", level=logging.DEBUG):
    """Configure basic logging.

    :param str filename: (optional) The filename to write logging statements to.
    :param logging.Logger.debug level: (optional) The level at which to log.

    """
    logging.basicConfig(filename=filename, level=level, format="%(asctime)s %(levelname)s %(message)s")

def record(database, port, ssl):
    """Stream results from the Nest API and record them in the database.

    :param str database: The name of the database.
    :param int port: The port number the database is listening on.
    :param bool ssl: Whether or not to use SSL to communicate with the database.
    :rtype: :py:const:`None`
    :return: When the stream opened to the Nest API has been consumed.
    :raises: :exc:`requests.exceptions.StreamConsumedError`: if the stream has been consumed.
    :raises: :exc:`requests.exceptions.ConnectionError`: if the Nest API cannot be reached.
    :raises: :exc:`requests.exceptions.HTTPError`: if an invalid response is returned from the Nest API.
    :raises: :exc:`requests.exceptions.Timeout`: if the request to the Nest API takes too long to respond.

    """
    db = influxdb.InfluxDBClient(database=database, port=port, ssl=ssl)
    with closing(_get_stream()) as stream:
        logging.debug("[%d] Streaming %s", stream.status_code, stream.url)
        for l in stream.iter_lines():
            if l:
                value = _process(l)
                if value:
                    logging.info(value)
                    db.write_points(_get_structure_data(value))
                    db.write_points(_get_thermostat_data(value))
        logging.debug("[%d] Streaming complete %s", stream.status_code, stream.url)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <http://github.com/calamares> ===
#
#   Copyright 2015, Teo Mrnjavac <teo@kde.org>
#
#   Calamares is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Calamares is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.

import argparse
import os
import sys
import shutil

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

#following from Python cookbook, #475186
def has_colours(stream):
    if not hasattr(stream, "isatty"):
        return False
    if not stream.isatty():
        return False # auto color only on TTYs
    try:
        import curses
        curses.setupterm()
        return curses.tigetnum("colors") > 2
    except:
        # guess false in case of error
        return False

has_colours = has_colours(sys.stdout)

def printout(text, colour=WHITE):
    if has_colours:
        seq = "\x1b[1;%dm" % (30+colour) + text + "\x1b[0m"
        return seq
    else:
        return text

def message(msg):
    sys.stdout.write(printout("==> ", GREEN) + printout(msg, WHITE) + "\n")

def bail(msg):
    sys.stdout.write(printout("==> ", RED) + printout("Fatal error: " + msg + "\n", WHITE))
    sys.exit(1)

def update_self():
    message("Updating deployment script...")
    thisfile = os.path.realpath(__file__)
    os.system("curl -o " + thisfile + " -L https://calamares.io/deploycala.py")
    os.system("chmod +x " + thisfile)

    myargs = sys.argv[:]
    message('Update complete, restarting %s' % ' '.join(myargs))

    myargs.insert(0, sys.executable)
    myargs.append('--noupdate')

    os.execv(sys.executable, myargs)

def yaourt_update(noupgrade):
    packages = ["cmake", "extra-cmake-modules", "boost", "qt5-tools", "kiconthemes", "kservice", "kio", "kparts", "qtcreator", "ack"]
    if shutil.which( "pacman-mirrors" ):
        os.system("sudo pacman-mirrors -c Germany")
    if noupgrade:
        os.system("yaourt -Sy --noconfirm --needed --force " + " ".join(packages))
    else:
        os.system("yaourt -Syu --noconfirm --needed --force " + " ".join(packages))

def pacman_update(noupgrade):
    packages = ["cmake", "extra-cmake-modules", "boost", "qt5-tools", "kiconthemes", "kservice", "kio", "kparts", "qtcreator", "ack"]
    if shutil.which( "pacman-mirrors" ):
        os.system("sudo pacman-mirrors -c Germany")
    if noupgrade:
        os.system("sudo pacman -Sy --noconfirm --needed --force " + " ".join(packages))
    else:
        os.system("sudo pacman -Syu --noconfirm --needed --force " + " ".join(packages))

def setup_sudo_gdb():
    os.system("sudo touch /usr/bin/sudo-gdb")
    os.system("sudo chmod a+rwx /usr/bin/sudo-gdb")
    file = open('/usr/bin/sudo-gdb','w+')
    file.write('#!/bin/bash\nsudo gdb $@\n')
    file.close()

def inplace_change(filename, old_string, new_string):
    s=open(filename).read()
    if old_string in s:
            s=s.replace(old_string, new_string)
            f=open(filename, 'w')
            f.write(s)
            f.flush()
            f.close()

def get_file_if_not_exists(source, target):
    if not os.path.exists(target):
        os.system("curl -o " + target + " -L " + source)

def setup_qtcreator():
    prefix = 'https://calamares.io/deploycala.d/'
    getfiles = dict([
    (prefix + 'CMakeLists.txt.user', '~/calamares/CMakeLists.txt.user'),
    (prefix + 'debuggers.xml', '~/.config/QtProject/qtcreator/debuggers.xml'),
    (prefix + 'QtCreator.ini', '~/.config/QtProject/QtCreator.ini')])

    for src, dest in getfiles:
        get_file_if_not_exists(src, dest)
        inplace_change(dest,"/home/netrunner", os.path.expanduser('~'))

# Courtesy of phihag on Stack Overflow,
# http://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python
def available_cpu_count():
    # Python 3.4+
    try:
        import multiprocessing
        return multiprocessing.cpu_count()
    except (ImportError, NotImplementedError):
        pass

    # POSIX
    try:
        res = int(os.sysconf('SC_NPROCESSORS_ONLN'))

        if res > 0:
            return res
    except (AttributeError, ValueError):
        pass

    # Linux
    try:
        res = open('/proc/cpuinfo').read().count('processor\t:')

        if res > 0:
            return res
    except IOError:
        pass

    return 0

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("branch", nargs="?", default="master",
                        help="the branch to checkout and build")
    parser.add_argument("-n", "--noupgrade", action="store_true", dest="noupgrade",
                        help="do not upgrade all the packages on the system before building")
    parser.add_argument("-i", "--incremental", action="store_true", dest="incremental",
                        help="do incremental builds, i.e. don't clear the build directory before building, if found")
    parser.add_argument("--noupdate", action="store_true", dest="noupdate", help=argparse.SUPPRESS)
    args = parser.parse_args()

    if not args.noupdate:
        update_self()

    cwd = os.getcwd()

    message("Backing up Calamares configuration and resources...")
    if os.path.isdir("/usr/share/calamares.backup"):
        os.system("sudo rm -rf /usr/share/calamares.backup")
    os.system("sudo cp -R /usr/share/calamares /usr/share/calamares.backup")
    if os.path.isdir("/etc/calamares.backup"):
        os.system("sudo rm -rf /etc/calamares.backup")
    os.system("sudo cp -R /etc/calamares /etc/calamares.backup")

    message("Updating build dependencies...")
    if shutil.which("yaourt"):
        yaourt_update(args.noupgrade)
    elif shutil.which("pacman"):
        pacman_update(args.noupgrade)
    else:
        bail("no package manager found.")

    branch = args.branch;
    if not branch:
        branch = "master"

    if os.path.isdir("calamares"):
        message("Clone found, checking out " + branch + " branch...")
        os.chdir("calamares")
        os.system("git checkout --track origin/" + branch + " -b " + branch)
        os.system("git pull --rebase")
        os.system("git submodule update")
        os.system("git submodule sync")
        if os.path.isdir("build") and not args.incremental:
            shutil.rmtree("build", ignore_errors=True)
            os.mkdir("build")

    else:
        message("Cloning and checking out " + branch + " branch...")
        os.system("git clone https://github.com/calamares/calamares.git")
        os.chdir("calamares")
        os.system("git checkout --track origin/"+ branch +" -b " + branch)
        os.system("git submodule init")
        os.system("git submodule update")
        os.system("git submodule sync")
        os.mkdir("build")

    os.chdir("build")
    cpu_count = available_cpu_count()
    if not cpu_count > 0:
        cpu_count = 4

    message("Found " + str(cpu_count) + " CPU cores, building...")
    os.system("cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr -DWITH_PARTITIONMANAGER=1 .. && " +
              "make -j" + str(cpu_count) + " && " +
              "sudo make install")
    os.chdir(cwd)

    message("Restoring Calamares configuration and resources...")
    os.system("sudo cp -R /usr/share/calamares.backup/* /usr/share/calamares/")
    os.system("sudo cp -R /etc/calamares.backup/* /etc/calamares/")

    message("Setting up sudo-gdb...")
    setup_sudo_gdb()

    message("Setting up QtCreator configuration...")
    setup_qtcreator()

    message("All done.")

if __name__ == "__main__":
    sys.exit(main())
<commit_msg>Iterate over k,v pairs.<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <http://github.com/calamares> ===
#
#   Copyright 2015, Teo Mrnjavac <teo@kde.org>
#
#   Calamares is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Calamares is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.

import argparse
import os
import sys
import shutil

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

#following from Python cookbook, #475186
def has_colours(stream):
    if not hasattr(stream, "isatty"):
        return False
    if not stream.isatty():
        return False # auto color only on TTYs
    try:
        import curses
        curses.setupterm()
        return curses.tigetnum("colors") > 2
    except:
        # guess false in case of error
        return False

has_colours = has_colours(sys.stdout)

def printout(text, colour=WHITE):
    if has_colours:
        seq = "\x1b[1;%dm" % (30+colour) + text + "\x1b[0m"
        return seq
    else:
        return text

def message(msg):
    sys.stdout.write(printout("==> ", GREEN) + printout(msg, WHITE) + "\n")

def bail(msg):
    sys.stdout.write(printout("==> ", RED) + printout("Fatal error: " + msg + "\n", WHITE))
    sys.exit(1)

def update_self():
    message("Updating deployment script...")
    thisfile = os.path.realpath(__file__)
    os.system("curl -o " + thisfile + " -L https://calamares.io/deploycala.py")
    os.system("chmod +x " + thisfile)

    myargs = sys.argv[:]
    message('Update complete, restarting %s' % ' '.join(myargs))

    myargs.insert(0, sys.executable)
    myargs.append('--noupdate')

    os.execv(sys.executable, myargs)

def yaourt_update(noupgrade):
    packages = ["cmake", "extra-cmake-modules", "boost", "qt5-tools", "kiconthemes", "kservice", "kio", "kparts", "qtcreator", "ack"]
    if shutil.which( "pacman-mirrors" ):
        os.system("sudo pacman-mirrors -c Germany")
    if noupgrade:
        os.system("yaourt -Sy --noconfirm --needed --force " + " ".join(packages))
    else:
        os.system("yaourt -Syu --noconfirm --needed --force " + " ".join(packages))

def pacman_update(noupgrade):
    packages = ["cmake", "extra-cmake-modules", "boost", "qt5-tools", "kiconthemes", "kservice", "kio", "kparts", "qtcreator", "ack"]
    if shutil.which( "pacman-mirrors" ):
        os.system("sudo pacman-mirrors -c Germany")
    if noupgrade:
        os.system("sudo pacman -Sy --noconfirm --needed --force " + " ".join(packages))
    else:
        os.system("sudo pacman -Syu --noconfirm --needed --force " + " ".join(packages))

def setup_sudo_gdb():
    os.system("sudo touch /usr/bin/sudo-gdb")
    os.system("sudo chmod a+rwx /usr/bin/sudo-gdb")
    file = open('/usr/bin/sudo-gdb','w+')
    file.write('#!/bin/bash\nsudo gdb $@\n')
    file.close()

def inplace_change(filename, old_string, new_string):
    s=open(filename).read()
    if old_string in s:
            s=s.replace(old_string, new_string)
            f=open(filename, 'w')
            f.write(s)
            f.flush()
            f.close()

def get_file_if_not_exists(source, target):
    if not os.path.exists(target):
        os.system("curl -o " + target + " -L " + source)

def setup_qtcreator():
    prefix = 'https://calamares.io/deploycala.d/'
    getfiles = dict([
    (prefix + 'CMakeLists.txt.user', '~/calamares/CMakeLists.txt.user'),
    (prefix + 'debuggers.xml', '~/.config/QtProject/qtcreator/debuggers.xml'),
    (prefix + 'QtCreator.ini', '~/.config/QtProject/QtCreator.ini')])

    for src, dest in getfiles.items():
        get_file_if_not_exists(src, dest)
        inplace_change(dest,"/home/netrunner", os.path.expanduser('~'))

# Courtesy of phihag on Stack Overflow,
# http://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python
def available_cpu_count():
    # Python 3.4+
    try:
        import multiprocessing
        return multiprocessing.cpu_count()
    except (ImportError, NotImplementedError):
        pass

    # POSIX
    try:
        res = int(os.sysconf('SC_NPROCESSORS_ONLN'))

        if res > 0:
            return res
    except (AttributeError, ValueError):
        pass

    # Linux
    try:
        res = open('/proc/cpuinfo').read().count('processor\t:')

        if res > 0:
            return res
    except IOError:
        pass

    return 0

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("branch", nargs="?", default="master",
                        help="the branch to checkout and build")
    parser.add_argument("-n", "--noupgrade", action="store_true", dest="noupgrade",
                        help="do not upgrade all the packages on the system before building")
    parser.add_argument("-i", "--incremental", action="store_true", dest="incremental",
                        help="do incremental builds, i.e. don't clear the build directory before building, if found")
    parser.add_argument("--noupdate", action="store_true", dest="noupdate", help=argparse.SUPPRESS)
    args = parser.parse_args()

    if not args.noupdate:
        update_self()

    cwd = os.getcwd()

    message("Backing up Calamares configuration and resources...")
    if os.path.isdir("/usr/share/calamares.backup"):
        os.system("sudo rm -rf /usr/share/calamares.backup")
    os.system("sudo cp -R /usr/share/calamares /usr/share/calamares.backup")
    if os.path.isdir("/etc/calamares.backup"):
        os.system("sudo rm -rf /etc/calamares.backup")
    os.system("sudo cp -R /etc/calamares /etc/calamares.backup")

    message("Updating build dependencies...")
    if shutil.which("yaourt"):
        yaourt_update(args.noupgrade)
    elif shutil.which("pacman"):
        pacman_update(args.noupgrade)
    else:
        bail("no package manager found.")

    branch = args.branch;
    if not branch:
        branch = "master"

    if os.path.isdir("calamares"):
        message("Clone found, checking out " + branch + " branch...")
        os.chdir("calamares")
        os.system("git checkout --track origin/" + branch + " -b " + branch)
        os.system("git pull --rebase")
        os.system("git submodule update")
        os.system("git submodule sync")
        if os.path.isdir("build") and not args.incremental:
            shutil.rmtree("build", ignore_errors=True)
            os.mkdir("build")

    else:
        message("Cloning and checking out " + branch + " branch...")
        os.system("git clone https://github.com/calamares/calamares.git")
        os.chdir("calamares")
        os.system("git checkout --track origin/"+ branch +" -b " + branch)
        os.system("git submodule init")
        os.system("git submodule update")
        os.system("git submodule sync")
        os.mkdir("build")

    os.chdir("build")
    cpu_count = available_cpu_count()
    if not cpu_count > 0:
        cpu_count = 4

    message("Found " + str(cpu_count) + " CPU cores, building...")
    os.system("cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr -DWITH_PARTITIONMANAGER=1 .. && " +
              "make -j" + str(cpu_count) + " && " +
              "sudo make install")
    os.chdir(cwd)

    message("Restoring Calamares configuration and resources...")
    os.system("sudo cp -R /usr/share/calamares.backup/* /usr/share/calamares/")
    os.system("sudo cp -R /etc/calamares.backup/* /etc/calamares/")

    message("Setting up sudo-gdb...")
    setup_sudo_gdb()

    message("Setting up QtCreator configuration...")
    setup_qtcreator()

    message("All done.")

if __name__ == "__main__":
    sys.exit(main())
<|endoftext|>


<commit_before>#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
"""
nr_openSMILE.py

Run selected openSMILE scripts on files with noises replaced by various methods
(see `openSMILE_preprocessing/noise_replacement` for noise replacement
details).

Author:
    – Jon Clucas, 2016–2017 (jon.clucas@childmind.org)

© 2017, Child Mind Institute, Apache v2.0 License

This script uses functions from mhealthx
( http://sage-bionetworks.github.io/mhealthx/ ).

Authors:
    - Arno Klein, 2015–2017  (arno@sagebase.org)  http://binarybottle.com

Copyright 2015–2017,  Sage Bionetworks (http://sagebase.org) & Child Mind
Institute, Apache v2.0 License

"""

from ... import openSMILE_runSM.mhealthx.mhealthx.extract as ex
from ... import iterate_ursis as iu
import os

# change this variable to your openSMILE installation location
openSMILE = '/home/jclucas/opensmile-2.3.0/inst/bin/SMILExtract'

def run_openSMILE(config_file, sound_file):
    """
    Function to run the openSMILE with a specified config_file on a specified
    sound file and save the results.

    Parameters
    ----------
    config_file : string
        the basename of the openSMILE config file that lives in `config`

    sound_file : string
        the absolute path to the soundfile

    Returns
    -------
    None.

    Outputs
    -------
    *soundfile*.csv : comma separated value file
        openSMILE output in arff–csv format with the original filename with the
        extension replaced, and in a subdirectory with a name matching the
        original location within an `openSMILE_output` directory at the
        relative level of original location
    """
    sub_dir = os.path.split(os.path.dirname(sound_file))
    basename = ''.join([os.path.basename(sound_file).strip('.wav').strip(
               '.WAV'), '.csv'])
    out_path = os.path.join(sub_dir[0], 'openSMILE_output', sub_dir[1])
    if not os.path.exists(out_path):
        os.makedirs(out_path, 0755)
    row = None
    r_oS_args = sound_file, openSMILE, '-I', '-C', '-O', ''.join(['config/',
                config_file]), '', row, out_dir, True
    print(' '.join(r_oS_args))
    # process the file
    try:
        row, table_path = ex.run_openSMILE(r_oS_args)
    # if necessary, specify csvoutput
    except cComponentException:
        row, table_path = ex.run_openSMILE(sound_file, openSMILE, '-I', '-C',
                          '-csvoutput', ''.join(['config/', config_file]), '',
                          row, out_dir, True)

def main():
    # replace `top_dir`'s value with your top-level directory
    top_dir = '/home/jclucas/opensmile-2.3.0/test/noise_replacement_efficacy'
    # replace `sub_dirs` with your conditions
    sub_dirs = ['ambient_clip', 'clone_fill', 'no_beeps', 'sample_silenced',
                'timeshifted']
    # replace `configs` with your openSMILE config files
    configs = ['emobase.conf', 'ComParE_2016.conf']
    # initialize file_list
    file_list = []
    # get files
    for sub_dir in sub_dirs:
        file_list.extend(iu.i_ursi(top_dir, sub_dir))
    for sound_file in file_list:
        for config in configs:
            run_openSMILE(config, sound_file)

# ============================================================================
if __name__ == '__main__':
    main()<commit_msg>relative import<commit_after>#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
"""
nr_openSMILE.py

Run selected openSMILE scripts on files with noises replaced by various methods
(see `openSMILE_preprocessing/noise_replacement` for noise replacement
details).

Author:
    – Jon Clucas, 2016–2017 (jon.clucas@childmind.org)

© 2017, Child Mind Institute, Apache v2.0 License

This script uses functions from mhealthx
( http://sage-bionetworks.github.io/mhealthx/ ).

Authors:
    - Arno Klein, 2015–2017  (arno@sagebase.org)  http://binarybottle.com

Copyright 2015–2017,  Sage Bionetworks (http://sagebase.org) & Child Mind
Institute, Apache v2.0 License

"""

from ...openSMILE_runSM import mhealthx.mhealthx.extract as ex
from ... import iterate_ursis as iu
import os

# change this variable to your openSMILE installation location
openSMILE = '/home/jclucas/opensmile-2.3.0/inst/bin/SMILExtract'

def run_openSMILE(config_file, sound_file):
    """
    Function to run the openSMILE with a specified config_file on a specified
    sound file and save the results.

    Parameters
    ----------
    config_file : string
        the basename of the openSMILE config file that lives in `config`

    sound_file : string
        the absolute path to the soundfile

    Returns
    -------
    None.

    Outputs
    -------
    *soundfile*.csv : comma separated value file
        openSMILE output in arff–csv format with the original filename with the
        extension replaced, and in a subdirectory with a name matching the
        original location within an `openSMILE_output` directory at the
        relative level of original location
    """
    sub_dir = os.path.split(os.path.dirname(sound_file))
    basename = ''.join([os.path.basename(sound_file).strip('.wav').strip(
               '.WAV'), '.csv'])
    out_path = os.path.join(sub_dir[0], 'openSMILE_output', sub_dir[1])
    if not os.path.exists(out_path):
        os.makedirs(out_path, 0755)
    row = None
    r_oS_args = sound_file, openSMILE, '-I', '-C', '-O', ''.join(['config/',
                config_file]), '', row, out_dir, True
    print(' '.join(r_oS_args))
    # process the file
    try:
        row, table_path = ex.run_openSMILE(r_oS_args)
    # if necessary, specify csvoutput
    except cComponentException:
        row, table_path = ex.run_openSMILE(sound_file, openSMILE, '-I', '-C',
                          '-csvoutput', ''.join(['config/', config_file]), '',
                          row, out_dir, True)

def main():
    # replace `top_dir`'s value with your top-level directory
    top_dir = '/home/jclucas/opensmile-2.3.0/test/noise_replacement_efficacy'
    # replace `sub_dirs` with your conditions
    sub_dirs = ['ambient_clip', 'clone_fill', 'no_beeps', 'sample_silenced',
                'timeshifted']
    # replace `configs` with your openSMILE config files
    configs = ['emobase.conf', 'ComParE_2016.conf']
    # initialize file_list
    file_list = []
    # get files
    for sub_dir in sub_dirs:
        file_list.extend(iu.i_ursi(top_dir, sub_dir))
    for sound_file in file_list:
        for config in configs:
            run_openSMILE(config, sound_file)

# ============================================================================
if __name__ == '__main__':
    main()<|endoftext|>


<commit_before>"""This file provides a function to load json example situations."""

import json
import os

DIR_PATH = os.path.dirname(os.path.abspath(__file__))

def parse(file_name):
    """Load json example situations."""
    file_path = os.path.join(DIR_PATH, file_name)
    with open(file_path, "r") as file:
        return json.loads(file.read())

single = parse("single.json")
couple = parse("couple.json")
<commit_msg>Add encoding to open file<commit_after>"""This file provides a function to load json example situations."""

import json
import os

DIR_PATH = os.path.dirname(os.path.abspath(__file__))

def parse(file_name):
    """Load json example situations."""
    file_path = os.path.join(DIR_PATH, file_name)
    with open(file_path, "r", encoding="utf8") as file:
        return json.loads(file.read())

single = parse("single.json")
couple = parse("couple.json")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from copy import deepcopy

# TenderSwitchPreQualificationResourceTest

def pre_qual_switch_to_auction(self):
    response = self.set_status('active.pre-qualification', {'status': self.initial_status})
    self.app.authorization = ('Basic', ('chronograph', ''))
    response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
    self.assertEqual(response.status, '200 OK')
    self.assertEqual(response.content_type, 'application/json')
    self.assertEqual(response.json['data']["status"], "active.pre-qualification")

# TenderSwitchAuctionResourceTest

def switch_to_auction(self):
        response = self.app.get('/tenders/{}/qualifications?acc_token={}'.format(self.tender_id, self.tender_token))
        self.assertEqual(response.content_type, 'application/json')
        qualifications = response.json['data']
        for qualification in qualifications:
            response = self.app.patch_json('/tenders/{}/qualifications/{}?acc_token={}'.format(self.tender_id, qualification['id'], self.tender_token),
                                          {"data": {'status': 'active'}})

        response = self.set_status('active.auction', {'status': 'active.pre-qualification.stand-still'})

        self.app.authorization = ('Basic', ('chronograph', ''))
        response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
        self.assertEqual(response.status, '200 OK')
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.json['data']["status"], "active.auction")

# TenderComplaintSwitchResourceTest

def switch_to_complaint(self):
        user_data = deepcopy(self.author_data)
        for status in ['invalid', 'resolved', 'declined']:
            response = self.app.post_json('/tenders/{}/complaints'.format(self.tender_id), {'data': {
                'title': 'complaint title',
                'description': 'complaint description',
                'author': user_data,
                'status': 'claim'
            }})
            self.assertEqual(response.status, '201 Created')
            self.assertEqual(response.json['data']['status'], 'claim')
            complaint = response.json['data']

            response = self.app.patch_json('/tenders/{}/complaints/{}?acc_token={}'.format(self.tender_id, complaint['id'], self.tender_token), {"data": {
                "status": "answered",
                "resolution": status * 4,
                "resolutionType": status
            }})
            self.assertEqual(response.status, '200 OK')
            self.assertEqual(response.content_type, 'application/json')
            self.assertEqual(response.json['data']["status"], "answered")
            self.assertEqual(response.json['data']["resolutionType"], status)

        response = self.set_status('active.pre-qualification', {'status': self.initial_status})
        self.app.authorization = ('Basic', ('chronograph', ''))
        response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
        self.assertEqual(response.status, '200 OK')
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.json['data']["status"], "active.pre-qualification")
        self.assertEqual(response.json['data']["complaints"][-1]['status'], status)
<commit_msg>fix indents in chronograph blanks<commit_after># -*- coding: utf-8 -*-
from copy import deepcopy

# TenderSwitchPreQualificationResourceTest

def active_tendering_to_pre_qual(self):
    response = self.set_status('active.pre-qualification', {'status': self.initial_status})
    self.app.authorization = ('Basic', ('chronograph', ''))
    response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
    self.assertEqual(response.status, '200 OK')
    self.assertEqual(response.content_type, 'application/json')
    self.assertEqual(response.json['data']["status"], "active.pre-qualification")

def pre_qual_switch_to_auction(self):
    response = self.set_status('active.auction', {'status': 'active.pre-qualification'})
    self.app.authorization = ('Basic', ('chronograph', ''))
    response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
    self.assertEqual(response.status, '200 OK')
    self.assertEqual(response.content_type, 'application/json')
    self.assertEqual(response.json['data']["status"], "active.pre-qualification")

def pre_qual_switch_to_stand_still(self):

    response = self.set_status('active.pre-qualification.stand-still', {'status': 'active.pre-qualification'})
    self.app.authorization = ('Basic', ('chronograph', ''))
    response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
    self.assertEqual(response.status, '200 OK')
    self.assertEqual(response.content_type, 'application/json')
    self.assertEqual(response.json['data']["status"], "active.pre-qualification")

# TenderSwitchAuctionResourceTest

def switch_to_auction(self):
    response = self.app.get('/tenders/{}/qualifications?acc_token={}'.format(self.tender_id, self.tender_token))
    self.assertEqual(response.content_type, 'application/json')
    qualifications = response.json['data']
    for qualification in qualifications:
        response = self.app.patch_json('/tenders/{}/qualifications/{}?acc_token={}'.format(self.tender_id, qualification['id'], self.tender_token),
                                      {"data": {'status': 'active'}})

    response = self.set_status('active.auction', {'status': 'active.pre-qualification.stand-still'})

    self.app.authorization = ('Basic', ('chronograph', ''))
    response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
    self.assertEqual(response.status, '200 OK')
    self.assertEqual(response.content_type, 'application/json')
    self.assertEqual(response.json['data']["status"], "active.auction")

# TenderComplaintSwitchResourceTest

def switch_to_complaint(self):
    user_data = deepcopy(self.author_data)
    for status in ['invalid', 'resolved', 'declined']:
        response = self.app.post_json('/tenders/{}/complaints'.format(self.tender_id), {'data': {
            'title': 'complaint title',
            'description': 'complaint description',
            'author': user_data,
            'status': 'claim'
        }})
        self.assertEqual(response.status, '201 Created')
        self.assertEqual(response.json['data']['status'], 'claim')
        complaint = response.json['data']

        response = self.app.patch_json('/tenders/{}/complaints/{}?acc_token={}'.format(self.tender_id, complaint['id'], self.tender_token), {"data": {
            "status": "answered",
            "resolution": status * 4,
            "resolutionType": status
        }})
        self.assertEqual(response.status, '200 OK')
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.json['data']["status"], "answered")
        self.assertEqual(response.json['data']["resolutionType"], status)

    response = self.set_status('active.pre-qualification', {'status': self.initial_status})
    self.app.authorization = ('Basic', ('chronograph', ''))
    response = self.app.patch_json('/tenders/{}'.format(self.tender_id), {'data': {'id': self.tender_id}})
    self.assertEqual(response.status, '200 OK')
    self.assertEqual(response.content_type, 'application/json')
    self.assertEqual(response.json['data']["status"], "active.pre-qualification")
    self.assertEqual(response.json['data']["complaints"][-1]['status'], status)
<|endoftext|>