text:
from paddle.fluid import core
import numpy as np
import math
import os
from paddle.fluid.executor import global_scope
from paddle.fluid import io

__all__ = ['Calibrator']

class Calibrator(object):
    '''
    The calibrator class transforms the program and updates the calculated scale into it.
    This is INT8 v1 calibration tool, mainly for the support of ResNet-50 and MobileNet.
    '''
    # TODO(guomingz): Below op list will be updated once more INT8 op kernels are supported.
    non_conv_int8_op_type = ("pool2d")
    supported_int8_op_type = ("conv2d", "pool2d")
    const_sign_op_type = ('pool2d', 'reshape', 'concat', 'transpose')
    u8_max = 255
    s8_max = 127

    def __init__(self, *args, **kwargs):
        self.program = kwargs['program']
        self.pretrained_model = kwargs['pretrained_model']
        self.debug = kwargs['debug'] if 'debug' in kwargs else False
        self.algo = kwargs['algo']
        self.output = kwargs['output']
        self.feed_var_names = kwargs['feed_var_names']
        self.fetch_list = kwargs['fetch_list']
        self.exe = kwargs['exe']

        self._conv_input_var_name = []
        self._conv_output_var_name = []
        self._pool2d_output_var_name = []
        self._weights_var_name = []
        self._residual_input_var_name = []
        self._int8_output_var_op_index_dict = {}
        self._conv_op_index = [
            index for index, value in enumerate(self.program.global_block().ops)
            if value.type == 'conv2d'
        ]

        self._var_max_value_map = {}
        self._var_max_range = {}
        self._weights_scaling_factor = {}
        self._u8_output_var = []
        self._s8_output_var = []
        self._persistable_vars = []
        self._sampling_data = {}

        self.__init_analysis()
        self.__generate_output_program()

    def save_int8_model(self):
        self.__sampling(self._sampling_data)
        self.__save_scale()
        self.__update_program()
        self.__update_output_program_attr()
        self.__display_debug()
        self.__save_offline_model()

    def sample_data(self):
        '''
        Sampling the tensor data of variable.
        '''
        for i in self.sampling_program.list_vars():
            if i.name in self.sampling_vars:
                np_data = np.array(global_scope().find_var(i.name).get_tensor())
                if i.name not in self._sampling_data:
                    self._sampling_data[i.name] = []
                self._sampling_data[i.name].append(np_data)

    def __save_offline_model(self):
        '''
        Save the quantized model to the disk.
        '''
        io.save_inference_model(self.output, self.feed_var_names,
                                self.fetch_list, self.exe,
                                self.sampling_program)

    def __display_debug(self):
        if self.debug:
            self.__dot(self._output_program)
            print(self._output_program)

    def __get_max_range_by_var_name(self, program, var_name):
        """
        Check the specified variable was generated from Relu layer or not.
        If the variable was the output of one of the pool2d/reshape/concat
        /transpose, we keep trace the ancestor of this variable;
        If the variable was the output the conv op, we check it's has_relu
        attr;
        Otherwise, we return the Calibrator.s8 as default value.
        Returns:
            Return Calibrator.u8_max if the variable was generated by Relu,
            otherwise it will returns Calibrator.s8
        """
        search_end_index = -1
        input_index_name = {}
        output_index_name = {}
        ops_type = []

        for index, op in enumerate(program.current_block().ops):
            ops_type.append(op.type)

            input_index_name[index] = op.input_arg_names

            output_index_name[index] = op.output_arg_names
            if var_name in op.output_arg_names:
                search_end_index = index

        # analysis
        while search_end_index >= 0:
            if ops_type[search_end_index] == "relu":
                return Calibrator.u8_max

            input_name = input_index_name[search_end_index][0]

            for i in output_index_name.keys():
                if input_name in output_index_name[i]:
                    search_end_index = i
                    break

            if ops_type[
                    search_end_index] not in Calibrator.const_sign_op_type and ops_type[
                        search_end_index] != 'conv2d':
                return Calibrator.s8_max

            if ops_type[search_end_index] != 'conv2d':
                continue

            if program.current_block().ops[search_end_index].has_attr(
                    'fuse_relu') and program.current_block().ops[
                        search_end_index].attr('fuse_relu'):
                return Calibrator.u8_max
            else:
                return Calibrator.s8_max

        return Calibrator.s8_max

    def __check_op_type_with_specified_var_as_input(self,
                                                    program,
                                                    var_name,
                                                    start_index=0):
        '''
        Check whether all the type of ops that use the specified variable as the
        input.If one of those op is not int8-enabled, return False.
        '''
        op_type_list = [
            op.type for op in program.current_block().ops[start_index:]
            if var_name in op.input_arg_names
        ]
        for i in op_type_list:
            if not i in Calibrator.supported_int8_op_type:
                return False
        return True

    def __check_var_source_dt(self, var_name):
        '''
        Check whether the specified variable is the output of int8 conv op or not.
        If true, return the original op index.
        If false, return -1
        '''
        return self._int8_output_var_op_index_dict[
            var_name] if var_name in self._int8_output_var_op_index_dict else -1

    def __update_int8_output_var_op_index_dict(self, index, var_name=None):
        '''
        Update the int8_output_variable/op_index dictionary
        '''
        for k, v in self._int8_output_var_op_index_dict.items():
            if v >= index:
                self._int8_output_var_op_index_dict[k] = v + 1
        if var_name:
            self._int8_output_var_op_index_dict[var_name] = index

    def __update_program(self):
        '''
        Update the program with the quantize/dequantize op insertion.
        '''
        quantize_index, dequantize_index = self.__get_quantize_dequantize_combination(
            self._output_program)
        inserted_op_length = 0
        calc_max_func = self.__get_optimal_scaling_factor if self.algo == "KL" else np.max
        insert_op_collection = sorted(quantize_index + dequantize_index)

        for index in insert_op_collection:
            if index in quantize_index:
                quantize_tmp = self._output_program.current_block().create_var(
                    name="quantize_{}_tmp".format(index),
                    dtype=core.VarDesc.VarType.UINT8)
                original_out_name = self._output_program.current_block().ops[
                    index + inserted_op_length - 1].output_names[0]
                original_out = self._output_program.current_block().ops[
                    index + inserted_op_length - 1].output(original_out_name)[0]

                op = self._output_program.current_block()._insert_op(
                    index=index + inserted_op_length,
                    type="quantize",
                    inputs={"Input": original_out},
                    outputs={"Output": quantize_tmp}, )

                op._set_attr("data_format", "MKLDNNLAYOUT")
                op._set_attr("use_mkldnn", 1)
                op._set_attr(
                    "Scale", self._var_max_range[original_out] /
                    calc_max_func(self._var_max_value_map[original_out]))

                if self.__get_max_range_by_var_name(
                        self._output_program,
                        original_out) == Calibrator.s8_max:
                    op._set_attr("is_negative_input", 1)

                self.__update_int8_output_var_op_index_dict(
                    index + inserted_op_length, "quantize_{}_tmp".format(index))

                inserted_op_length += 1
                for op in self._output_program.current_block().ops[
                        index + inserted_op_length:]:
                    for j in op.input_names:
                        if op.input(j) and op.input(
                                j
                        )[0] == original_out and op.type in Calibrator.supported_int8_op_type:
                            op.desc.set_input(j,
                                              ["{}".format(quantize_tmp.name)])
            else:
                start_index = index + inserted_op_length
                dequantize_tmp_var = self._output_program.current_block(
                ).create_var(
                    name="dequantize_{}_tmp".format(index + 1),
                    dtype="float32", )
                original_out_var = None

                for original_input in self._output_program.current_block().ops[
                        start_index].input_arg_names:
                    index_res = self.__get_op_index_by_output_var(
                        self._output_program, original_input)
                    if index_res != -1:
                        original_out_var = original_input
                        break

                if original_out_var:
                    op = self._output_program.current_block()._insert_op(
                        index=start_index,
                        type="dequantize",
                        inputs={"Input": original_out_var},
                        outputs={"Output": dequantize_tmp_var})
                    op._set_attr("data_format", "MKLDNNLAYOUT")
                    op._set_attr("use_mkldnn", 1)
                    op._set_attr("Scale", self._var_max_range[original_out_var]
                                 / calc_max_func(self._var_max_value_map[
                                     original_out_var]))

                    for op_index in range(
                            start_index + 1,
                            len(self._output_program.current_block().ops)):
                        if self._output_program.current_block(
                        ).ops[op_index].type == "conv2d" and self._output_program.current_block(
                        ).ops[op_index].attr("force_fp32_output"):
                            continue
                        else:
                            for j in self._output_program.current_block().ops[
                                    op_index].input_names:
                                if len(self._output_program.current_block().ops[
                                        op_index].input(j)
                                       ) and self._output_program.current_block(
                                       ).ops[op_index].input(j)[
                                           0] == original_out_var:
                                    self._output_program.current_block(
                                    ).ops[op_index].desc.set_input(
                                        j,
                                        ["{}".format(dequantize_tmp_var.name)])

                    inserted_op_length += 1

                    op._set_attr("data_format", "MKLDNNLAYOUT")
                    op._set_attr("use_mkldnn", 1)

    def __update_output_program_attr(self):
        for i in self._output_program.list_vars():
            if i.name in self._persistable_vars:
                i.persistable = False
                os.system("rm -rf {}/{}".format(self.pretrained_model, i.name))

        for i in self._u8_output_var:
            self._output_program.current_block().var(i).desc.set_dtype(
                core.VarDesc.VarType.UINT8)

        for i in self._s8_output_var:
            self._output_program.current_block().var(i).desc.set_dtype(
                core.VarDesc.VarType.INT8)

    @property
    def sampling_program(self):
        return self._output_program

    @property
    def sampling_vars(self):
        return self._weights_var_name + self._conv_input_var_name + self._conv_output_var_name + self._residual_input_var_name + self._pool2d_output_var_name

    def _is_close(self, a, b, rel_tol=1e-09, abs_tol=0.0):
        return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

    def __generate_output_program(self):
        for i in self.program.list_vars():
            if not i.persistable and i.name in self.sampling_vars:
                i.persistable = True
                self._persistable_vars.append(i.name)

        self._output_program = self.program.clone()

    def __save_scale(self):
        '''
        Update the convolution scale information.
        '''
        func = self.__get_optimal_scaling_factor if self.algo == 'KL' else np.max
        for i in self._conv_op_index[1:]:
            weights_var_name = self.program.current_block().ops[i].input(
                'Filter')[0]
            input_var_name = self.program.current_block().ops[i].input('Input')[
                0]
            output_var_name = self.program.current_block().ops[i].output(
                'Output')[0]
            self._output_program.current_block().ops[i]._set_attr(
                "Scale_weights", self._weights_scaling_factor[weights_var_name])

            self._output_program.current_block().ops[i]._set_attr(
                "Scale_in", self._var_max_range[input_var_name] /
                func(self._var_max_value_map[input_var_name]))
            self._output_program.current_block().ops[i]._set_attr(
                "Scale_out", self._var_max_range[output_var_name] /
                func(self._var_max_value_map[output_var_name]))
            if self._output_program.current_block().ops[i].desc.input(
                    "ResidualData"):
                residual_var_name = self._output_program.current_block().ops[
                    i].desc.input("ResidualData")[0]
                self._output_program.current_block().ops[i]._set_attr(
                    "Scale_in_eltwise", self._var_max_range[residual_var_name] /
                    func(self._var_max_value_map[residual_var_name]))

    def __sampling(self, sampling_data):
        '''
        Sampling the variables data range.
        '''
        for i in self.program.list_vars():
            if i.name not in self.sampling_vars:
                continue

            if i.name in self._weights_var_name:
                scaling_factor_per_channel = []
                data = sampling_data[i.name][0]
                for j in range(data.shape[0]):
                    var_value = float(np.max(np.abs(data[j])))
                    if not self._is_close(var_value, 0.0):
                        scaling_factor_per_channel.append(Calibrator.s8_max /
                                                          var_value)
                    else:
                        scaling_factor_per_channel.append(0.0)
                self._weights_scaling_factor[
                    i.name] = scaling_factor_per_channel
            else:
                if i.name in self._conv_output_var_name:
                    op_pos = self.__get_op_index_by_output_var(self.program,
                                                               i.name)
                    cur_op = self.program.current_block().ops[op_pos]

                    if cur_op.has_attr('fuse_relu') and cur_op.attr(
                            'fuse_relu'):
                        max_range = Calibrator.u8_max
                        self._u8_output_var.append(i.name)
                    else:
                        max_range = Calibrator.s8_max
                        self._s8_output_var.append(i.name)
                else:
                    max_range = self.__get_max_range_by_var_name(self.program,
                                                                 i.name)
                max_value = [[np.abs(np_data)]
                             for np_data in sampling_data[i.name]]

                self._var_max_range[i.name] = max_range
                self._var_max_value_map[i.name] = max_value

    def __check_force_fp32_attr_by_output_var(self, program, var_name):
        for op in program.current_block().ops:
            if op.type == "conv2d" and var_name in op.output_arg_names:
                return op.attr("force_fp32_output")
        return False

    def __get_op_index_by_output_var(self, program, var_name, start_index=0):
        '''
        Check whether the specified input variable is the output of the
        conv/pool2d op's output or not.

        Returns:
            The index if the variable is the output of any conv/pool2d op's
            output.
            -1 when the variable is not the output of any conv/pool2d op's
            output.
        '''
        for index, op in enumerate(program.current_block().ops[start_index:]):
            if var_name in op.output_arg_names and op.type in Calibrator.supported_int8_op_type:
                return index
        return -1

    def __get_op_index_by_input_var(self, program, var_name, start_index=0):
        '''
        Get the op index by specified input variable.
        Returns:
            The op index if the variable is the input of this op or -1 if the
            variable is not the input of any op.
        '''
        for index, op in enumerate(program.current_block().ops[start_index:]):
            if var_name in op.input_arg_names:
                return index

        return -1

    def __get_quantize_dequantize_combination(self, program):
        """
        Get the quantize/dequantize op index for further inserting.
        Args:
            The program desc.
        Returns:
            Two lists contains the quantize op and dequantize op index information.
        """
        quantize_op_index = []
        dequantize_op_index = []
        minimal_conv_count = 2  # there must be two conv ops if not enable the first conv int8.
        if len(self._conv_op_index) < minimal_conv_count:
            return [], []

        for index, value in enumerate(self._conv_op_index):
            if index == 0:
                quantize_op_index.append(self._conv_op_index[index + 1])
            elif index == len(self._conv_op_index) - 1:
                output_var = program.current_block().ops[value].output(
                    "Output")[0]
                if self.__check_op_type_with_specified_var_as_input(
                        program, output_var, index):
                    dequantize_op_index.append(self._conv_op_index[index] + 2)
                else:
                    program.current_block().ops[value]._set_attr(
                        "force_fp32_output", True)

            elif self._conv_op_index[index] + 1 < self._conv_op_index[index +
                                                                      1]:

                program.current_block().ops[self._conv_op_index[
                    index]]._set_attr("force_fp32_output", True)

                for op_index in range(self._conv_op_index[index + 1],
                                      self._conv_op_index[index], -1):
                    op_type = program.current_block().ops[op_index].type
                    op_has_int8_input = False
                    input_var_name = None
                    input_length = len(program.current_block().ops[op_index]
                                       .input_arg_names)

                    for var_name in program.current_block().ops[
                            op_index].input_arg_names:
                        if self.__check_var_source_dt(var_name) != -1:
                            op_has_int8_input = True
                            input_var_name = var_name
                            break

                    if op_has_int8_input:
                        if op_type == "conv2d":
                            if program.current_block().ops[op_index +
                                                           1].type == "conv2d":
                                continue
                            elif program.current_block(
                            ).ops[op_index +
                                  1].type in Calibrator.non_conv_int8_op_type:
                                dequantize_op_index.append(op_index + 2)
                                break
                            else:
                                program.current_block().ops[op_index]._set_attr(
                                    "force_fp32_output", True)
                                continue
                        elif not self.__check_force_fp32_attr_by_output_var(
                                program, input_var_name
                        ) and op_index not in dequantize_op_index:
                            share_input_flag = True
                            for input_attr_name in program.current_block().ops[
                                    op_index].input_names:
                                input_var_name = program.current_block().ops[
                                    op_index].input(input_attr_name)[0]
                                cousin_op_index = self.__get_op_index_by_input_var(
                                    program, input_var_name)
                                if cousin_op_index != -1 and cousin_op_index in dequantize_op_index:
                                    share_input_flag = False
                                    break
                            if share_input_flag:
                                dequantize_op_index.append(op_index)

                    elif input_length:
                        output_is_to_int8_op = False
                        share_input_flag = True
                        for var_name in program.current_block().ops[
                                op_index].input_arg_names:
                            if not self.__check_op_type_with_specified_var_as_input(
                                    program, var_name):
                                share_input_flag = False
                                break

                        for var_name in program.current_block().ops[
                                op_index].output_arg_names:
                            if self.__get_op_index_by_output_var(
                                    program, var_name, op_index) != -1:
                                output_is_to_int8_op = True
                                break

                        if share_input_flag or output_is_to_int8_op:
                            quantize_op_index.append(op_index)

        return quantize_op_index, dequantize_op_index

    def __init_analysis(self):
        '''
        Collect the variable names for sampling.
        '''
        start_index = 1  #analysis the conv op detail from second conv op.

        for i in self._conv_op_index[start_index:]:
            self._weights_var_name.append(self.program.current_block().ops[i]
                                          .input('Filter')[0])
            self._conv_input_var_name.append(self.program.current_block().ops[i]
                                             .input('Input')[0])
            self._conv_output_var_name.append(self.program.current_block().ops[
                i].output('Output')[0])
            self._int8_output_var_op_index_dict[self.program.current_block()
                                                .ops[i].output('Output')[0]] = i
            if self.program.current_block().ops[i].desc.input("ResidualData"):
                self._residual_input_var_name.append(self.program.current_block(
                ).ops[i].desc.input("ResidualData")[0])

            if self.program.current_block().ops[i + 1].type == "pool2d":
                self._pool2d_output_var_name.append(self.program.current_block(
                ).ops[i + 1].output('Out')[0])

    def __expand_quantized_bins(self, quantized_bins, reference_bins):
        expanded_quantized_bins = [0] * len(reference_bins)
        num_merged_bins = len(reference_bins) / len(quantized_bins)
        j_start = 0
        j_end = num_merged_bins
        for idx in xrange(len(quantized_bins)):
            zero_count = reference_bins[j_start:j_end].count(0)
            num_merged_bins = j_end - j_start
            if zero_count == num_merged_bins:
                avg_bin_ele = 0
            else:
                avg_bin_ele = quantized_bins[idx] / (
                    num_merged_bins - zero_count + 0.0)
            for idx1 in xrange(j_start, j_end):
                expanded_quantized_bins[idx1] = (0 if reference_bins[idx1] == 0
                                                 else avg_bin_ele)
            j_start += num_merged_bins
            j_end += num_merged_bins
            if (idx + 1) == len(quantized_bins) - 1:
                j_end = len(reference_bins)
        return expanded_quantized_bins

    def __safe_entropy(self, reference_distr_P, P_sum, candidate_distr_Q,
                       Q_sum):
        '''
        Calculate the entropy.
        '''
        assert len(reference_distr_P) == len(candidate_distr_Q)
        tmp_sum1 = 0
        tmp_sum2 = 0
        for idx in range(len(reference_distr_P)):
            p_idx = reference_distr_P[idx]
            q_idx = candidate_distr_Q[idx]
            if p_idx == 0:
                tmp_sum1 += 0
                tmp_sum2 += 0
            else:
                if q_idx == 0:
                    print("Fatal error!, idx = " + str(idx) +
                          " qindex = 0! p_idx = " + str(p_idx))
                tmp_sum1 += p_idx * (math.log(Q_sum * p_idx))
                tmp_sum2 += p_idx * (math.log(P_sum * q_idx))
        return (tmp_sum1 - tmp_sum2) / P_sum

    # Reference: http://on-demand.gputechconf.com/gtc/2017/presentation/s7310-8-bit-inference-with-tensorrt.pdf
    def __get_optimal_scaling_factor(self,
                                     activation_blob,
                                     num_quantized_bins=255):
        '''
        Using the KL-divergenc method to get the more precise scaling factor.
        '''
        max_val = np.max(activation_blob)
        min_val = np.min(activation_blob)
        if min_val >= 0:
            hist, hist_edeges = np.histogram(
                activation_blob, bins=2048, range=(min_val, max_val))
            ending_iter = 2047
            starting_iter = int(ending_iter * 0.7)
        else:
            th = max(abs(max_val), abs(min_val))
            hist, hist_edeges = np.histogram(
                activation_blob, bins=2048, range=(-th, th))
            starting_iter = 0
            ending_iter = 2047
            if abs(max_val) > abs(min_val):
                while starting_iter < ending_iter:
                    if hist[starting_iter] == 0:
                        starting_iter += 1
                        continue
                    else:
                        break
                starting_iter += int((ending_iter - starting_iter) * 0.6)
            else:
                while ending_iter > 0:
                    if hist[ending_iter] == 0:
                        ending_iter -= 1
                        continue
                    else:
                        break
                starting_iter = int(0.6 * ending_iter)
        bin_width = hist_edeges[1] - hist_edeges[0]
        P_sum = len(activation_blob)
        min_kl_divergence = 0
        min_kl_index = 0
        kl_inited = False
        for i in range(starting_iter, ending_iter + 1):
            reference_distr_P = hist[0:i].tolist()
            outliers_count = sum(hist[i:2048])
            if reference_distr_P[i - 1] == 0:
                continue
            reference_distr_P[i - 1] += outliers_count
            reference_distr_bins = reference_distr_P[:]
            candidate_distr_Q = hist[0:i].tolist()
            num_merged_bins = i / num_quantized_bins
            candidate_distr_Q_quantized = [0] * num_quantized_bins
            j_start = 0
            j_end = num_merged_bins
            for idx in xrange(num_quantized_bins):
                candidate_distr_Q_quantized[idx] = sum(candidate_distr_Q[
                    j_start:j_end])
                j_start += num_merged_bins
                j_end += num_merged_bins
                if (idx + 1) == num_quantized_bins - 1:
                    j_end = i
            candidate_distr_Q = self.__expand_quantized_bins(
                candidate_distr_Q_quantized, reference_distr_bins)
            Q_sum = sum(candidate_distr_Q)
            kl_divergence = self.__safe_entropy(reference_distr_P, P_sum,
                                                candidate_distr_Q, Q_sum)
            if not kl_inited:
                min_kl_divergence = kl_divergence
                min_kl_index = i
                kl_inited = True
            elif kl_divergence < min_kl_divergence:
                min_kl_divergence = kl_divergence
                min_kl_index = i
            else:
                pass
        if min_kl_index == 0:
            while starting_iter > 0:
                if hist[starting_iter] == 0:
                    starting_iter -= 1
                    continue
                else:
                    break
            min_kl_index = starting_iter
        return (min_kl_index + 0.5) * bin_width

    @staticmethod
    def __dot(program, output_name="model.dot"):
        '''
        Generate the graphiz dot file for debugging.
        '''
        dot_graph = ""
        dot_nodes = []
        dot_edges = []
        dot_graph += "digraph pm {\n"
        for block in program.blocks:
            ops = list(block.ops)
            for index, op in enumerate(ops):
                op_type = op.type
                op_name = op_type + "_" + op.output_arg_names[0].replace(
                    ".", "_") + "___" + str(index)
                for name in op.input_arg_names:
                    name = name.replace(".", "_")
                    dot_edge = name + " -> " + op_name
                    if dot_edge not in dot_edges:
                        dot_edges.append(dot_edge)
                    dot_node = name + " [shape=oval, style=filled, fillcolor=yellow]"
                    if dot_node not in dot_nodes:
                        dot_nodes.append(dot_node)

                for name in op.output_arg_names:
                    name = name.replace(".", "_")
                    dot_edge = op_name + " -> " + name
                    if dot_edge not in dot_edges:
                        dot_edges.append(dot_edge)
                if op_type in Calibrator.supported_int8_op_type:
                    if op_type == "conv2d" and op.has_attr(
                            'force_fp32_output') and op.attr(
                                "force_fp32_output"):
                        dot_node = op_name + " [shape=box, style=filled, color=deeppink]"
                    else:
                        dot_node = op_name + " [shape=box, style=filled, color=greenyellow]"
                elif op_type in ["quantize", "dequantize"]:
                    dot_node = op_name + " [shape=box, style=filled, color=gold]"
                else:
                    dot_node = op_name + " [shape=box, style=filled, fillcolor=red]"

                if dot_node not in dot_nodes:
                    dot_nodes.append(dot_node)

        for dot_edge in dot_edges:
            dot_graph += dot_edge + "\n"
        for dot_node in dot_nodes:
            dot_graph += dot_node + "\n"
        dot_graph += "}"

        with open(output_name, 'w') as f:
            f.write(dot_graph)


text:
% For LaTeX-Box: root = stat105_F15_exam1B.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  File Name: stat105_F15_exam1B.tex
%  Purpose:
%
%  Creation Date: 24-09-2015
%  Last Modified: Wed Sep 30 15:58:16 2015
%  Created By:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{examsetup}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\usepackage{etoolbox}
\usepackage{tikz,pgfplots}
\usepackage{amsmath}

%\input{stat105_exam1_info.tex}
\newcommand{\course}[1]{\ifstrempty{#1}{STAT 105}{STAT 105, Section #1}}
\newcommand{\sectionNumber}{B}
\newcommand{\examDate}{October 1, 2015}
\newcommand{\semester}{FALL 2015}
\newcommand{\examNumber}{I}

\printanswers

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

%-- : R code (Code in Document)

\examCoverPage

\begin{questions}

\question[2]

Circle the \textbf{bold face} term that makes the following statement true: \\

A measurement device that reports the true measurement of the item on which the device is being used is (\textbf{precise} or \textbf{accurate}).

\begin{solution}
   Accurate.
\end{solution}

\question

%-- : R code (Code in Document)

A sample of size 5 was drawn from a population and the resulting observations are reported below.
\begin{center}
12, 15, 18, 19, 26
\end{center}
Using these observed values, report the following:
\vspace{1cm}

\begin{parts}

   \part[2] the mean
   \begin{solution}
      \begin{align*}
      \bar{x} = \frac{1}{n} \sum_{i=1}^n x_i &= \frac{1}{5} (x_1 + x_2 + x_3 + x_4 + x_5)  \\
                                             &= \frac{1}{5} (12 + 15 + 18 + 19 + 26) \\
                                             &= \frac{1}{5} (90) \\
                                             &= 18
      \end{align*}
   \end{solution}

   \part[2] the median
   \begin{solution}
      The median is $Q(0.5)$. \\

      Since $n p + 0.5 = 5 \cdot 0.5 + 0.5 = 3$
      then $Q(.5) = x_{3} = 18$ is the median. \\

      Alternatively, since $n = 5$ is odd, we know that the median is the middle of the ordered values.

   \end{solution}

   \part[2] the variance
   \begin{solution}
      Since this is a sample, we must $s^2$:\\

      \begin{align*}
         s^2 &= \frac{1}{n-1} \sum_{i = 1}^{n} (x_i - \bar{x})^2 \\
             &= \frac{1}{5-1} \sum_{i = 1}^{5} (x_i - \bar{x})^2 \\
             &= \frac{1}{4} \left( (x_1 - \bar{x})^2+ (x_2 - \bar{x})^2 + (x_3 - \bar{x})^2 + (x_4 - \bar{x})^2 + (x_5 - \bar{x})^2 \right) \\
             &= \frac{1}{4} \left( (12 - 18)^2+ (15 - 18)^2 + (18 - 18)^2 + (19 - 18)^2 + (26 - 18)^2 \right) \\
             &= \frac{1}{4} \left( (\ensuremath{-6})^2 + (\ensuremath{-3})^2 + (0)^2 + (1)^2 + (8)^2 \right) \\
             &= \frac{1}{4} \left( 36 + 9 + 0 + 1 + 64 \right) \\
             &= \frac{1}{4} \left( 110 \right) \\
             &= 27.5 \\
      \end{align*}

   \end{solution}

   \part[2] the standard deviation
   \begin{solution}
      We must use the sample standard deviation, $s$:\\

      \begin{align*}
         s &= \sqrt{s^2} = \sqrt{27.5} = 5.2440442 \\
      \end{align*}

   \end{solution}

   \part[2] the value of $Q(.25)$

%-- : R code (Code in Document)

   \begin{solution}
      We will need to use the quantile function.\\

      In this case, $i = \lfloor n p + 0.5 \rfloor = \lfloor 5 \cdot 0.25 + 0.5 \rfloor = \lfloor 1.75 \rfloor = 1$.
      Since $i \ne n p + 0.5$, we must use the second form of the function:

      \begin{align*}
         Q(.25) &= x_i + (n p - i + 0.5) \cdot (x_{i+1} - x_i) \\
                &= x_{1} + (5 \cdot .25 - 1 + 0.5) \cdot (x_{2} - x_{1}) \\
                &= 12 + (0.75) \cdot (15 - 12) \\
                &= 12 + (0.75) \cdot (3) \\
                &= 12 + 2.25 \\
                &= 14.25 \\
      \end{align*}

   \end{solution}

%-- : R code (Code in Document)

   \part[2] the Interquartile Range
   \begin{solution}
      Since $\text{IQR} = Q(.75) - Q(.25)$, we will need to use the quantile function to find $Q(.75)$.\\

      In this case, $i = \lfloor n p + 0.5 \rfloor = \lfloor 5 \cdot 0.75 + 0.5 \rfloor = \lfloor 4.25 \rfloor = 4$.
      Since $i \ne n p + 0.5$, we must use the second form of the function:

      \begin{align*}
         Q(.75) &= x_i + (n p - i + 0.5) \cdot (x_{i+1} - x_i) \\
                &= x_{4} + (5 \cdot .75 - 4 + 0.5) \cdot (x_{5} - x_{4}) \\
                &= 19 + (0.25) \cdot (26 - 19) \\
                &= 19 + (0.25) \cdot (7) \\
                &= 19 + 1.75 \\
                &= 20.75 \\
      \end{align*}

      So we get $\text{IQR} = Q(.75) - Q(.25) = 20.75 - 14.25 = 6.5$
   \end{solution}

\end{parts}

\question

An environmental engineer is testing four methods for reducing the concentration of a certain lake pollutant found in Iowa lakes.
To do this he first randomly selected 20 Iowa lakes from which he took water samples,
then split each of the 20 samples into 4 portions,
and randomly labeled the four portions 1, 2, 3, and 4.
Finally, he attempted to reduce the concentration of each
of the portions labeled 1 using the the first method,
of each of the portions labeled 2 using the second method,
of each of the portions labeled 3 using the third method,
and of each of the portions labeled portion 4 using the fourth method.
After the methods had been applied, he measured the change in concentration. \\

\begin{parts}
   \part[2] Is this an experiment or an observational study? Explain.
   \begin{solution}
      This is an experiment. The methods are being applied by the engineer and which method is used is decided by the engineer.
   \end{solution}

   \part Identify the following (if there was not one, simply put "not used").

  \vspace{1cm}

   \begin{subparts}
      \subpart[2] Response variable(s):
      \begin{solution}
         The change in concentration.
      \end{solution}

      \subpart[2] Experimental variable(s):
      \begin{solution}
         The method used.
      \end{solution}

      \subpart[2] Blocking variable(s):
      \begin{solution}
         The lake the sample was taken from is a treated as a block in this example.
      \end{solution}

   \end{subparts}

   \part[2] Was replication used in this experiment? If so, where was it applied? If not, how could we have applied it?
   \begin{solution}
      Replication was only used in the sense that each method was used more than once.
      However, inside the blocks (the sample from the lakes) each treatment is only applied once.
   \end{solution}

\end{parts}

\pagebreak

\question

Recently my teenage niece had an opportunity to upgrade her smart phone.
She narrowed her choices down to two phones (phone A and phone B) but had a hard time making her final decision.
She decided to ask people she knew who had one of the phones to rate their satisfaction from 0\% to 100\%.
She also asked them if they would prefer to have the other phone.
In order to help put their feelings in perspective, she also made note of how negative she thought they were in general,
using three descriptions: the interviewee was classified as overly critical, appropriately critical, or not critical enough.

\begin{parts}
   \part[2] Is this an experiment or an observational study?
   \begin{solution}
      This is an observational study. My niece takes no active role in change anything about the individuals she interviews or their phones.
   \end{solution}

   \part[2] Identify the response variable(s).
   \begin{solution}
   There are two responses being gathered about each interviewee: (1) whether or not they would prefer the other phone and (2) how satisfied they are with their current phone. variables
   \end{solution}

   \part For each of the following variables,

   \begin{itemize}

      \item Identify whether it is qualitative or quantitative variable, and

      \item If it is qualitative, what are the possible values it can take? If it is quantitative, is it continuous or discrete?

   \end{itemize}

   \begin{subparts}

      \subpart the individual's reported phone satisfaction percentage.

      \begin{solution}
      This is quantitative and continuous.
      \end{solution}

      \subpart my niece's appraisal of the interviewee's negativity.

      \begin{solution}
      This is qualitative. The levels are overly critical, appropriately critical, or not critical enough.
      \end{solution}

      \subpart whether or not the interviewee would prefer to have the other phone.

      \begin{solution}
      This is qualitative. The levels are ``yes`` and ``no``.
      \end{solution}

      \subpart the type of phone the interviewee currently owns.

      \begin{solution}
      This is qualitative. The levels are ``phone 1`` and ``phone 2``.
      \end{solution}

   \end{subparts}

\end{parts}
\pagebreak

\question

%-- : R code (Code in Document)

The strength of an internet connection is often described in terms of its download speed, measured in megabits per second (or Mbps).
A systems administrator is concerned that recent changes in her company's main server framework may be having a negative impact on the local network's download speed.
Every 2 minutes for an hour, she recorded the network speed at that moment and collected her results into the following stem-and-leaf plot:

%-- : R code (Code in Document)
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}

  The decimal point is at the |

   0 | 9
   1 | 8
   2 | 7
   3 | 6
   4 | 134
   5 | 7
   6 | 1145677
   7 | 01338
   8 | 2346
   9 | 79
  10 | 45
  11 |
  12 | 17
\end{verbatim}
\end{kframe}
\end{knitrout}

Note that \verb!0 | 9! represents 0.9. In this case, the first quartile is $Q(.25) = 5.7$, the median is 6.85, and the IQR is 2.7.

%-- : R code (Code in Document)

\begin{parts}
  \part[10] Complete the following frequency table: \\

  \begin{table}[h!]
     \centering
     \begin{tabular}{|l|p{3cm}|p{3cm}|p{4cm}|}
        \hline
                             & \textbf{Frequency} & \textbf{Relative} & \textbf{Cumulative}  \\
        \textbf{Value Range} &                    & \textbf{Frequency} & \textbf{Relative Frequency} \\\hline \hline
                    &  &  &  \\
      0.00 - 2.00   &  2 & 0.07 & 0.07 \\
                    &  &  &  \\ \hline
                    &  &  &  \\
      2.01 - 4.00   &  2 & 0.07 & 0.14 \\
                    &  &  &  \\ \hline
                    &  &  &  \\
      4.01 - 6.00   &  4 & 0.13 & 0.27 \\
                    &  &  &  \\ \hline
                    &  &  &  \\
      6.01 - 8.00   &  12 & 0.4 & 0.67 \\
                    &  &  &  \\ \hline
                    &  &  &  \\
      8.01 - 10.00  &  6 & 0.2 & 0.87 \\
                    &  &  &  \\ \hline
                    &  &  &  \\
      10.01 - 12.00 &  2 & 0.07 & 0.94 \\
                    &  &  &  \\ \hline
                    &  &  &  \\
      12.01 - 14.00 &  2 & 0.07 & 1.01 \\
                    &  &  &  \\  \hline
     \end{tabular}
  \end{table}

  \pagebreak

  \part[10] Create a box plot to summarize the data. Carefully label the axes.

  \begin{solution}
     The boxplot is below: \\

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\includegraphics[width=.8\linewidth]{figure/unnamed-chunk-8-1}

\end{knitrout}
  \end{solution}

  \part[4] Are there any unusually low observations? If so What were the speeds at those points?
  \begin{solution}
     Yes, there are two unusually low observations as indicated by the box plot. They are 0.9 and 1.8.
  \end{solution}

  \part[10] She also measured upload speed, obtaining the following 8 values.

%-- : R code (Code in Document)

\begin{center}
   7.45, 4.22, 7.7, 6.04, 7.68, 5.71, 4.71, 8.44
\end{center}

Create a theoretical Q-Q plot using the following quantiles from the normal distribution as the theoretical quantiles. Carefully label your axes.
What does this graph tell us about the upload speeds?

\begin{table}[h!]
   \centering
   \begin{tabular}{ccccccccc}
             & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline
      $p$    & 0.0625 & 0.1875 & 0.3125 & 0.4375 & 0.5625 & 0.6875 & 0.8125 & 0.9375 \\
      $Q(p)$ & \ensuremath{-1.53} & \ensuremath{-0.89} & \ensuremath{-0.49} & \ensuremath{-0.16} & 0.16 & 0.49 & 0.89 & 1.53 \\ \hline
   \end{tabular}
\end{table}

%-- : R plot (results in document)
\begin{solution}
   We get the QQ-plot by plotting the ordered values of our sample against the ordered quantiles from the normal distribution (as given above): \\

\begin{centering}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\includegraphics[width=.8\linewidth]{figure/unnamed-chunk-10-1}

\end{knitrout}
\end{centering}

The points do seem to somewhat linear - an argument could be made that because of this the upload speed is normally distributed.

\end{solution}

\end{parts}
\pagebreak

\question

The major cause of axel failure in freight trucks is when shippers exceed the recommended weight limits that can be handled by the axels.
Issues resulting from these failures have been becoming more frequent as shippers try to cut corners,
leading members of the state's Department of Transportation to ask one of their civil engineers
to look into the available data and better advise them on the relationship between excessive weight and axel failure.

A company manufacturing axels provides the engineer with data gathered from conducting experiments loading axels with excessive weight and simulating traveling conditions.
The data consists of two columns, \textbf{excessive weight (in tonnes)} is the amount of weight over the limit that was placed on the axel, and
\textbf{distance to failure (in tens of thousands of miles)} is the simulated distance to the axel's failure.

%-- : R code (Code in Document)

\begin{center}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\includegraphics[width=.5\linewidth]{figure/unnamed-chunk-12-1}

\end{knitrout}
\end{center}

Here are some summaries of the data:

$$
\sum_{i=1}^{50} x_i = 64 \hspace{3cm} \sum_{i=1}^{50} x_i^2 = 107 \\
$$

$$
\sum_{i=1}^{50} y_i = 2008 \hspace{3cm} \sum_{i=1}^{50} y_i^2 = 95182 \\
$$

$$
\sum_{i=1}^{50} x_i y_i =
1999
$$

\begin{parts}
   \part Using the summaries above, fit a linear relationship between \textbf{weight exceeding guidelines} (x) and \textbf{travel distance to failure} (y).
   \begin{subparts}
      \subpart[5] Write the equation of the fitted linear relationship.
      \begin{solution}
         We need to get the estimates of $b_0$ and $b_1$ to write the equation of the fitted line: \\

         \begin{align*}
            b_1 &= \frac{\sum_{i=1}^{50} x_i y_i - 50 \bar{x} \bar{y}}{\sum_{i=1}^{50} x_i^2 - 50 \bar{x}^2} \\
                &=  \frac{ 1999 - 50 \left(\frac{64}{50}\right) \left(\frac{2008}{50}\right) }{ 107- 50 \left(\frac{64}{50}\right)^2 } \\
                &=  \frac{ 1999 - 50 (1.27) (40.16) }{ 107- 50 (1.27)^2 } \\
                &=  \frac{ 1999 - 2550.16 }{ 107- 80.645 } \\
             &= \frac{ \ensuremath{-551.16} }{ 26.355 } \\
             &= \ensuremath{-20.91} \\
         \end{align*}
         and thus
         \begin{align*}
            b_0 &= \bar{y} - b_1 \bar{x} \\
                &= \left(\frac{2008}{50}\right) - (\ensuremath{-20.91} ) \left(\frac{64}{50}\right) \\
                &= \left(40.16\right) - (\ensuremath{-20.91} ) \left(1.28\right) \\
                &= 66.9248 \\
         \end{align*}
         So the equation of the fitted line is:

         $$
         \hat{y} = 66.9248 - 20.91 x
         $$

      \end{solution}
      \subpart[5] Find and interpret the value of $R^2$ for the fitted linear relationship.
      \begin{solution}
         Since this is a linear model, we can first find the correlation coeffecient and then find the value of $R^2$:
         \begin{align*}
         r
         &=
         \frac{
            \sum_{i=1}^n x_i y_i - n \bar{x} \bar{y}
         }{
            \sqrt{\left(\sum_{i=1}^n x_i^2 - \bar{x}^2 \right) \left(y_i - n \bar{y}^2 \right)}
         }\\
         &=
         \frac{
            1999 - 50 (1.27)(40.16)
         }{
            \sqrt{\left(107 - (50)( 1.27)^2 \right)
            \left(95182 - (50)( 40.16)^2 \right)}
         }\\
         &=
         \frac{
            \ensuremath{-551.16}
         }{
            \sqrt{\left(25.5 \right)
            \left(14540.5 \right)}
         }\\
         &=
         \frac{
            \ensuremath{-551.16}
         }{
            608.9193296
         }
         \\
         &= \ensuremath{-0.91}
         \end{align*}

         So $R^2 =
          (\ensuremath{-0.91})^2
            =
          0.8281
     $
     meaning that 82.8\% of the variability in failure time can be explained by the linear relationship of failure time with weight.
      \end{solution}

      \subpart[5] Using the fitted line, provide a predicted value of travel distance to failure when the weight exceeding the guidelines is 3.4 tonnes.
      \begin{solution}
        \begin{align*}
         \hat{y} &= 66.9248 - 20.91 ( 3.4 )\\
                 &= \ensuremath{-4.1692}\\
        \end{align*}
      \end{solution}
   \end{subparts}

   \part The JMP output below comes from fitting a quadratic model using $x$ and $x^2$.

   \centerline{\includegraphics[scale=.2]{FitModel}}

   \begin{subparts}
      \subpart[5] Write the equation of the fitted quadratic relationship.
      \begin{solution}
         From the JMP output: \\

         $$\hat{y} = 16.27 + 4.66 x - 10.2 x^2$$
      \end{solution}
      \subpart[5] Find and interpret the value of $R^2$ for the fitted quadratic relationship.
      \begin{solution}
         $$
         R^2 = \frac{\text{SSTO} - \text{SSE}}{\text{SSTO}} = \frac{13229.647 - 1311.073}{13229.647} = 0.9008989
         $$
         Using the quadratic relationship, approximately 90.09\% of the variability in failure time can be explained.
      \end{solution}
      \subpart[5] Using the fitted quadratic relationship, provide a predicted value of travel distance to failure when the weight exceeding the guidelines is 3.4 tonnes.
      \begin{solution}
         \begin{align*}
         \hat{y} &= 16.27 + 4.66 x - 10.2 x^2 \\
                 &= 16.27 + 4.66 (3.4) - 10.2 (3.4)^2 \\
                 &= \ensuremath{-85.798} \\
         \end{align*}
      \end{solution}
   \end{subparts}
\end{parts}

\end{questions}

\end{document}

redpajama_set_name: RedPajamaGithub


text:
<?php namespace Larapress\Interfaces;

interface MockablyInterface {

    /**
     * Return current Unix timestamp in seconds with microseconds as float
     *
     * @return float
     */
    public function microtime();

}


text:
package me.michaelkrauty.Kettle.command.permissions;

import me.michaelkrauty.Kettle.Kettle;
import me.michaelkrauty.Kettle.Objects.User;
import me.michaelkrauty.Kettle.util.AbstractCommand;
import me.michaelkrauty.Kettle.util.Groups;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 * Created on 7/24/2014.
 *
 * @author michaelkrauty
 */
public class PermissionCommand extends AbstractCommand {

	private final Kettle kettle;

	public PermissionCommand(String command, String usage, String description, Kettle instance) {
		super(command, usage, description);
		kettle = instance;
	}

	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
		if (args.length == 0) {
			sender.sendMessage(ChatColor.RED + cmd.getUsage());
			return true;
		}
		Player target;
		if ((target = kettle.getServer().getPlayer(args[0])) instanceof Player) {
			User targetUser = kettle.objects.getUser(target);
			if (args[1].equalsIgnoreCase("add")) {
				if (!targetUser.hasPermission(args[1])) {
					targetUser.addPermission(args[1]);
					sender.sendMessage(ChatColor.GRAY + "Added permission.");
					return true;
				}
				sender.sendMessage(ChatColor.GRAY + "That player already has that permission.");
				return true;
			}
			if (args[1].equalsIgnoreCase("remove")) {
				if (targetUser.hasPermission(args[1])) {
					targetUser.removePermission(args[1]);
					sender.sendMessage(ChatColor.GRAY + "Removed permission.");
					return true;
				}
				sender.sendMessage(ChatColor.GRAY + "That player doesn't have that permission.");
				return true;
			}
			if (args[1].equalsIgnoreCase("list")) {
				if (!targetUser.getPermissions().isEmpty()) {
					sender.sendMessage(ChatColor.GRAY + target.getDisplayName() + "'s permissions:");
					for (String permission : targetUser.getPermissions()) {
						sender.sendMessage(ChatColor.GRAY + permission);
					}
					return true;
				}
				sender.sendMessage(ChatColor.GRAY + "That player doesn't have any permissions.");
				return true;
			}
			sender.sendMessage(ChatColor.RED + cmd.getUsage());
			return true;
		}
		sender.sendMessage(ChatColor.RED + "Couldn't find that player.");
		return true;
	}
}


text:
using namespace std;

int main()
{
    cout << "Starting game..." << endl;

    StarFieldGame* game = new StarFieldGame();
    game->Play();

    delete game;

    cout << "Game ended." << endl;
    return 0;
}


text:
[![browser support](https://ci.testling.com/mattdesl/get-image-pixels.png)](https://ci.testling.com/mattdesl/get-image-pixels)

# get-image-pixels

[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)

Uses an intermediary canvas node to grab the RGBA pixels of the given source, with an optional clipping region. Works on HTML Canvas, Image, and Video elements.

```js
var getPixels = require('get-image-pixels')

var data = getPixels(myImage) // returns a Uint8Array
console.log( data.length === (myImage.width * myImage.height * 4) ) //true
```

Or, with clipping:

```js
var halfWidth = myImage.width/2
var data = getPixels(myImage, { x: halfWidth, width: halfWidth })
console.log( data.length === (halfWidth * myImage.height * 4) ) //true
```

## Usage

[![NPM](https://nodei.co/npm/get-image-pixels.png)](https://nodei.co/npm/get-image-pixels/)

### `getPixels(image[, opts])`

Gets the RGBA pixels from HTML Image/Video/Canvas element as a Uint8Array with some optional parameters.

- `x` the x position to start clipping, default 0
- `y` the y position to start clipping, default 0
- `width` the width of the source to copy; this will change the returned array's shape. defaults to image width
- `height` the height of the source to copy; this will change the returned array's shape. defaults to image height

### `getPixels.dispose()`

Release the shared canvas for GC. This is mainly useful if you need to minimize disruptive GC hitches, e.g. in a game loop.

## License

MIT, see [LICENSE.md](http://github.com/mattdesl/get-image-pixels/blob/master/LICENSE.md) for details.


text:
import { render, cleanup } from '@carbon/test-utils/react';
import React from 'react';
import { FileUploaderSkeleton } from '../';

describe('FileUploaderSkeleton', () => {
  afterEach(cleanup);

  describe('automated accessibility testing', () => {
    it('should have no axe violations', async () => {
      const { container } = render(<FileUploaderSkeleton />);
      await expect(container).toHaveNoAxeViolations();
    });

    it('should have no AC violations', async () => {
      const { container } = render(<FileUploaderSkeleton />);
      await expect(container).toHaveNoACViolations('FileUploaderSkeleton');
    });
  });

  it('should accept a custom className prop on the root node', () => {
    const className = 'test';
    const { container } = render(
      <FileUploaderSkeleton className={className} />
    );
    expect(container.firstChild.classList.contains(className)).toBe(true);
  });
});


text:
var childProcess = require('child_process');
var projectRoot = '.';
var command = 'tsc --out bin/app.js --module amd src/app.ts src/models.ts src/views.ts --sourcemap';

childProcess.exec(command, {
        cwd: '.'
      },function(err, stdout, stderr){
    console.log(err);
    console.log(stdout);
    console.log(stderr);
});


text:
package org.elasticsearch.painless.node;

import org.elasticsearch.painless.CompilerSettings;
import org.elasticsearch.painless.Definition;
import org.elasticsearch.painless.Variables;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.GeneratorAdapter;

import java.util.Collections;
import java.util.List;

/**
 * The root of all Painless trees.  Contains a series of statements.
 */
public final class SSource extends AStatement {

    final List<AStatement> statements;

    public SSource(final int line, final String location, final List<AStatement> statements) {
        super(line, location);

        this.statements = Collections.unmodifiableList(statements);
    }

    @Override
    public void analyze(final CompilerSettings settings, final Definition definition, final Variables variables) {
        variables.incrementScope();

        final AStatement last = statements.get(statements.size() - 1);

        for (final AStatement statement : statements) {
            if (allEscape) {
                throw new IllegalArgumentException(error("Unreachable statement."));
            }

            statement.lastSource = statement == last;
            statement.analyze(settings, definition, variables);

            methodEscape = statement.methodEscape;
            allEscape = statement.allEscape;
        }

        variables.decrementScope();
    }

    @Override
    public void write(final CompilerSettings settings, final Definition definition, final GeneratorAdapter adapter) {
        for (final AStatement statement : statements) {
            statement.write(settings, definition, adapter);
        }

        if (!methodEscape) {
            adapter.visitInsn(Opcodes.ACONST_NULL);
            adapter.returnValue();
        }
    }
}


text:
var winston = require('winston');
var logger = new winston.Logger({
    transports: [
        /*Log info to console*/
        new (winston.transports.Console)({
            timestamp: function () {
                return getTimeStamp();
            },
            formatter: function (options) {
                return '' + (undefined !== options.message ? options.message : '') +
                    (options.meta && Object.keys(options.meta).length ? '\n\t' + JSON.stringify(options.meta) : '' );
            },
            name: 'info-console',
            level: 'info',
            handleExceptions: true,
            humanReadableUnhandledException: true
        }),
        /*Log errors to console */
        new (winston.transports.Console)({
            timestamp: function () {
                return getTimeStamp();
            },
            formatter: function (options) {
                return '' + (undefined !== options.message ? options.message : '') +
                    (options.meta && Object.keys(options.meta).length ? '\n\t' + JSON.stringify(options.meta) : '' );
            },
            name: 'error-console',
            level: 'error',
            handleExceptions: true,
            humanReadableUnhandledException: true
        })
    ]
});

// Overwrite some of the build-in console functions
console.error = logger.error;
console.log = logger.info;
console.info = logger.info;
console.debug = logger.debug;
console.warn = logger.warn;

function getTimeStamp() {
    var date = new Date();
    return date.toISOString();
}


text:
/*
 * 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 wvlet.airframe.benchmark.http

/**
  */
import wvlet.airframe.http._
import wvlet.airframe.http.client.{AsyncClient, SyncClient}

import scala.language.higherKinds
import scala.collection.immutable.Map
import scala.concurrent.Future

class ServiceClient[F[_], Req, Resp](private val client: HttpClient[F, Req, Resp]) extends AutoCloseable {
  override def close(): Unit              = { client.close() }
  def getClient: HttpClient[F, Req, Resp] = client
  object Greeter {
    def hello(name: String, requestFilter: Req => Req = identity): F[String] = {
      client.postOps[Map[String, Any], String](
        resourcePath = s"/wvlet.airframe.benchmark.http.Greeter/hello",
        Map("name" -> name),
        requestFilter = requestFilter
      )
    }
  }
}

class NewServiceAsyncClient(private val client: AsyncClient) extends AutoCloseable {
  override def close(): Unit = { client.close() }
  def getClient: AsyncClient = client
  object Greeter {
    def hello(name: String): Future[String] = {
      client.call[Map[String, Any], String](
        Http.POST("/wvlet.airframe.benchmark.http.Greeter/hello"),
        Map("name" -> name)
      )
    }
  }
}


text:
package org.spongepowered.common.mixin.core.server.management;

import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketChunkData;
import net.minecraft.server.management.PlayerChunkMap;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.Chunk;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.interfaces.server.management.IMixinPlayerChunkMapEntry;

@Mixin(PlayerChunkMapEntry.class)
public abstract class MixinPlayerChunkMapEntry implements IMixinPlayerChunkMapEntry {

    @Shadow @Final private PlayerChunkMap playerChunkMap;
    @Shadow @Final private ChunkPos pos;
    @Shadow public int changes;
    @Shadow public int changedSectionFilter;
    @Shadow public abstract void sendPacket(Packet<?> packetIn);

    private boolean updateBiomes;

    @Inject(method = "update", at = @At("HEAD"), cancellable = true)
    public void resendUpdatedBiomes(CallbackInfo ci) {
        final Chunk chunk = this.playerChunkMap.getWorldServer().getChunkFromChunkCoords(this.pos.x, this.pos.z);
        if (this.updateBiomes) {
            this.sendPacket(new SPacketChunkData(chunk, 65535));
            this.changes = 0;
            this.changedSectionFilter = 0;
            this.updateBiomes = false;
            ci.cancel();
        }
    }

    @Override
    public void markBiomesForUpdate() {
        this.updateBiomes = true;
        this.playerChunkMap.dirtyEntries.add((PlayerChunkMapEntry) (Object) this);
    }
}


text:
package cn.com.bluemoon.jeesite.modules.mallvirtualproduct.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.bluemoon.jeesite.common.persistence.Page;
import cn.com.bluemoon.jeesite.common.service.CrudService;
import cn.com.bluemoon.jeesite.modules.mallvirtualproduct.entity.MallVirtualProduct;
import cn.com.bluemoon.jeesite.modules.mallvirtualproduct.dao.MallVirtualProductDao;

/**
 * 虚拟产品信息管理Service
 * @author linyihao
 * @version 2016-02-29
 */
@Service
@Transactional(readOnly = true)
public class MallVirtualProductService extends CrudService<MallVirtualProductDao, MallVirtualProduct> {

	public MallVirtualProduct get(String id) {
		return super.get(id);
	}

	public List<MallVirtualProduct> findList(MallVirtualProduct mallVirtualProduct) {
		return super.findList(mallVirtualProduct);
	}

	public Page<MallVirtualProduct> findPage(Page<MallVirtualProduct> page, MallVirtualProduct mallVirtualProduct) {
		return super.findPage(page, mallVirtualProduct);
	}

	@Transactional(readOnly = false)
	public void save(MallVirtualProduct mallVirtualProduct) {
		super.save(mallVirtualProduct);
	}

	@Transactional(readOnly = false)
	public void delete(MallVirtualProduct mallVirtualProduct) {
		super.delete(mallVirtualProduct);
	}

}


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.houjie.studyapk.studyview.custom.ActivityCustomViewAccBall">
    <com.example.houjie.studyapk.studyview.custom.CustomViewAccBall
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/viewAccBall"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnCustomAccBall"
        android:text="开启动画"
        />
</LinearLayout>


text:
"""Format a BLD file."""
import argparse
import os

import mool.shared_utils as su
import utils.bld_parser as bld_parser
import utils.file_utils as fu

def _parse_command_line(program_name, cmd_line):
  """Parse command line to generate arguments."""
  parser = argparse.ArgumentParser(prog=program_name)
  group = parser.add_mutually_exclusive_group(required=True)
  group.add_argument('--file', dest='file', help='path to a BLD file',
                     metavar='/project/module/common/BLD')
  group.add_argument('--path', dest='path', metavar='/project/module',
                     help='recursively find & format BLD files in path')
  return parser.parse_args(cmd_line)

def _format_and_write(file_path):
  """Format a given BLD file and write it back."""
  bld_list = bld_parser.bld_to_list(file_path)
  fu.write_file(file_path, bld_parser.list_to_bld_string(bld_list))

def main(program_name, cmd_line):
  """Main entry for BLD pretty formatter."""
  args = _parse_command_line(program_name, cmd_line)
  if args.path:
    count = 0
    for file_path in su.find_all_bld_files(args.path):
      try:
        _format_and_write(file_path)
        print 'Formatted {} file.'.format(file_path)
        count += 1
      except ValueError:
        print "File {} has errors".format(file_path)
  elif args.file:
    count = 1
    _format_and_write(os.path.abspath(args.file))
    print 'Formatted {} file.'.format(args.file)
  return (0, 'Successfully formatted {} BLD files.'.format(count))


text:
var fs = require('fs'),
	http = require('http'),
    httpProxy = require('./http-proxy'),
    httpsProxy = require('./https-proxy');

// Load settings
function loadSettings () {
	var settings = fs.readFileSync('./settings.json', { encoding: 'utf8' });
	try {
		global.settings = JSON.parse(settings);
	} catch (e) {
		console.log('[SERVER]\t[ERROR]\tTrouble parsing settings.json');
	}
	console.log('[SERVER]\t[INFO]\tLoaded settings:', global.settings);
}

// Load initial settings and watch for setting changes
loadSettings();
fs.watch('./settings.json', function (event, filename) {
	if (event === 'change') {
		loadSettings();
	}
});

// Create and start proxy server
var server = http.createServer();
server.on('request', httpProxy);
server.on('connect', httpsProxy);

// Export proxy server
module.exports = server;


text:
package com.arif.cleanarchitecture.network.api;

import com.arif.cleanarchitecture.network.response.Movie;

import io.reactivex.Single;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface OmdbRestApi {

    @GET("/")
    Single<Movie> getMovie(@Query("t") String title,
                           @Query("plot") String plot,
                           @Query("type") String type,
                           @Query("r") String format);

}


text:
import type {
  Selection,
  InputState,
  DialpadProps as Props,
  DialpadContact as DialpadContactType
} from '../../types';
import type { Props as Context } from '../ContextProvider/ContextProvider';
import type { ProviderContext } from '@dlghq/react-l10n';
import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';
import { LocalizationContextType } from '@dlghq/react-l10n';
import { View, FlatList, ActivityIndicator, Text } from 'react-native';
import DialpadContact from '../DialpadContact/DialpadContact';
import Pad from './Pad/PadAndroid';
import PadNumber from './PadNumber/PadNumberAndroid';
import PadFooter from './PadFooter/PadFooter';
import getStyles from './styles.android';
import { Color } from '../../styles';
import { insertText, replaceText, handleBackspace } from './inputState';

type State = {
  width: number,
  isLandscape: boolean
};

class Dialpad extends PureComponent<Props, State> {
  styles: Object;

  static defaultProps = {
    isContactsEnabled: true
  };

  static contextTypes = {
    theme: PropTypes.object,
    style: PropTypes.object,
    l10n: LocalizationContextType
  };

  constructor(props: Props, context: Context & ProviderContext) {
    super(props, context);

    this.state = {
      isLandscape: false,
      width: 0
    };

    this.styles = getStyles(context.theme, context.style.Dialpad);
  }

  handleChange = (inputState: *) => {
    if (/^[0-9 ()+\-#*]*$/.test(inputState.value)) {
      this.props.onChange(inputState);
    } else {
      // force selection
      this.forceUpdate();
    }
  };

  handleNumberPress = (number: string) => {
    this.props.onChange(insertText(this.props.inputState, number));
  };

  handleCallPress = () => {
    this.props.onCallRequest(this.props.inputState.value);
  };

  handleContactPress = (contact: DialpadContactType) => {
    this.props.onChange(replaceText(contact.phone));
  };

  handleLayoutChange = (event: *) => {
    const { width, height } = event.nativeEvent.layout;
    this.setState({
      width,
      isLandscape: width > height
    });
  };

  getItemKey = (contact: any, index: number) => contact.id;

  renderError() {
    const { contacts: { error } } = this.props;

    if (!error) {
      return null;
    }

    return (
      <View style={this.styles.fill}>
        <Text>{typeof error === 'string' ? error : error.message}</Text>
      </View>
    );
  }

  renderPending() {
    return (
      <View style={this.styles.fill}>
        <ActivityIndicator
          size="large"
          color={this.context.theme.color.primary || Color.primary}
        />
      </View>
    );
  }

  renderEmpty() {
    return (
      <View style={this.styles.empty}>
        <Text style={this.styles.emptyText}>
          {this.context.l10n.formatText('Error.nothing_found')}
        </Text>
      </View>
    );
  }

  renderContact = ({ item }: { item: DialpadContactType }) => {
    return <DialpadContact contact={item} onPress={this.handleContactPress} />;
  };

  renderContacts() {
    const { contacts, isContactsEnabled } = this.props;
    const { isLandscape } = this.state;

    if (!isContactsEnabled) {
      return (
        <View
          style={
            isLandscape
              ? this.styles.contactsDisabledLandscape
              : this.styles.contactsDisabled
          }
        />
      );
    }

    if (contacts.error) {
      return this.renderError();
    }

    if (contacts.pending) {
      return this.renderPending();
    }

    return (
      <View style={this.styles.contacts}>
        <FlatList
          data={contacts.value}
          renderItem={this.renderContact}
          keyExtractor={this.getItemKey}
          ListEmptyComponent={this.renderEmpty()}
        />
      </View>
    );
  }

  renderPad() {
    const { inputState } = this.props;
    const { isLandscape, width } = this.state;
    const isSmallWidth = isLandscape && width < 600;
    return (
      <View
        style={isLandscape ? this.styles.dialpadLandscape : this.styles.dialpad}
      >
        <PadNumber
          inputState={inputState}
          isLandscape={isLandscape}
          isSmallWidth={isSmallWidth}
          onChange={this.handleChange}
        />
        <Pad
          horizontal={this.state.isLandscape}
          onNumberPress={this.handleNumberPress}
          isSmallWidth={isSmallWidth}
        />
        <PadFooter
          horizontal={this.state.isLandscape}
          onCallPress={this.handleCallPress}
          isSmallWidth={isSmallWidth}
        />
      </View>
    );
  }

  render() {
    return (
      <View
        style={
          this.state.isLandscape
            ? this.styles.containerLandscape
            : this.styles.container
        }
        onLayout={this.handleLayoutChange}
      >
        {this.renderContacts()}
        {this.renderPad()}
      </View>
    );
  }
}

export default Dialpad;


text:
"""Utilities for marking deprecated functions."""
# Copyright 2016 Quantopian, Inc.
#
# 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.

import warnings
from zipline.utils.compat import wraps

def deprecated(msg=None, stacklevel=2):
    """
    Used to mark a function as deprecated.

    Parameters
    ----------
    msg : str
        The message to display in the deprecation warning.
    stacklevel : int
        How far up the stack the warning needs to go, before
        showing the relevant calling lines.

    Examples
    --------
    @deprecated(msg='function_a is deprecated! Use function_b instead.')
    def function_a(*args, **kwargs):
    """
    def deprecated_dec(fn):
        @wraps(fn)
        def wrapper(*args, **kwargs):
            warnings.warn(
                msg or "Function %s is deprecated." % fn.__name__,
                category=DeprecationWarning,
                stacklevel=stacklevel
            )
            return fn(*args, **kwargs)
        return wrapper
    return deprecated_dec


text:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="styles.css">
    <script src="tones.js"></script>
    <script src="script.js"></script>
    <script>

      function addFigure (shape, color, note) {
        var figure = document.createElement("div")
        figure.className = shape + " " + color
        figure.onmouseover = function () { playTone(note) }
        document.body.appendChild(figure)
        return figure
      }

    </script>
    <title>Choose Your Own Instrument</title>
  </head>
  <body>
  </body>
  <script>

    // backgrounds: "sky", "sea", "bubble-gum", "fireball", "sunset", "cool-mint", "fuji"
    // shapes: "triangle", "circle", "square", "diamond", "star"
    // colors: "red", "yellow", "blue", "orange", "green", "purple", "pink", "black"

   document.body.style.backgroundImage = "url('http://orig15.deviantart.net/fb68/f/2015/310/b/2/galaxy_texture_by_suga_drops-d9fr60u.jpg')"
s=addFigure ("circle", "pink", "a")
   s.innerText = "twinkle"
   	var a = new Audio ('twinkle.mp3')
   s.onclick = function () {
	   a.play ()
   }

	  for  ( i=0; i < 4; i++) {
		  addFigure ("circle", "pink", 13 + i * 50)

	  }

  </script>
</html>


text:
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.AvalonEdit.Document;
using Newtonsoft.Json;

namespace Chuck.Models
{
    /// <summary>
    ///     A model for testdetails
    /// </summary>
    public class TestDetailsModel
    {
        /// <summary>
        ///     The name of the script we're editing
        /// </summary>
        public string ScriptName { get; set; }

        /// <summary>
        ///     The text contained within Script  [Avalon TextDocument]
        ///     -Needed for JSON write support-
        /// </summary>
        public string ScriptText { get; set; }

        /// <summary>
        ///     The current status of our test if any.
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        ///     The tags associated with this test.
        /// </summary>
        public ICollection<string> Tags { get; set; }

        /// <summary>
        ///     The details of this test from a high glance.
        /// </summary>
        public string TestDetail { get { return ToString(); } }

        /// <summary>
        ///     The name of this test.
        /// </summary>
        public string TestName { get; set; }

        /// <summary>
        ///     The actual TextDocument containing our script
        /// </summary>
        [JsonIgnore]
        public TextDocument Script { get; set; }

        /// <summary>
        ///     Create a new instance of TestDetailsModel
        /// </summary>
        /// <param name="testName">Name of this test</param>
        /// <param name="scriptName">Name of the script</param>
        /// <param name="scriptText">Text that will be displayed within the script editor</param>
        /// <param name="tags">Tags associated with this test.</param>
        public TestDetailsModel(string testName, string scriptName, string scriptText, ICollection<string> tags)
        {
            ScriptName = scriptName;
            Script = new TextDocument {Text = scriptText};
            Tags = tags;
            TestName = testName;
            Status = "Idle";
        }

        /// <summary>
        ///     Necessary for saving the file with JSON.
        /// </summary>
        public void GetScriptTextFromAvalonDocument()
        {
            ScriptText = Script.Text;
        }

        /// <summary>
        ///     A human readable version of TestDetails
        /// </summary>
        /// <returns>A human readable version of TestDetails</returns>
        public override string ToString()
        {
            var tags = Tags.Aggregate(string.Empty, (current, tag) => current + string.Format("[{0}]  ", tag));
            return string.Format("{0} - Tags: {1}", TestName, tags.Trim());
        }
    }
}


text:
"""View implementations for log app."""

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.generic import CreateView
from django.views.generic import DeleteView
from django.views.generic import UpdateView
from django.views.generic.dates import DayArchiveView
from django.views.generic.dates import MonthArchiveView
from django.views.generic.dates import YearArchiveView
from django.views.generic import ListView

from core.viewutils import LoginRequiredMixin
from core.utils import second_to_str
from django.utils import timezone

from logs.forms import LogForm
from logs.forms import JobForm
from logs.models import Log
from logs.models import Job

from djangonumerics.api import register
from djangonumerics import LabelResponse
from djangonumerics import NumberResponse
# Create your views here.

class BaseLogArchiveMixin(LoginRequiredMixin):

    """Base for all archive views."""

    date_field = "start"
    make_object_list = True
    allow_future = True

    month_format = '%m'

    def get_queryset(self):
        """Filter all logs by user."""
        qs = Log.objects.filter(user=self.request.user)
        return qs

    def get_context_data(self, **kwargs):
        """Add summary data for current queryset."""
        context = super(BaseLogArchiveMixin, self).get_context_data(**kwargs)
        logs = context['object_list']
        context['job_summary'] = logs.summary_by_job()
        context['total_duration'] = logs.total_duration_display()
        context['date_name'] = self.date_name
        context['date_display'] = self.get_date_display(context)
        return context

class LogDayArchiveView(BaseLogArchiveMixin, DayArchiveView):

    """Day view."""

    date_name = 'Day'

    def get_date_display(self, context):
        """Return current display for current day."""
        return '{year}/{month}/{day}'.format(
            year=self.get_year(),
            month=self.get_month().zfill(2),
            day=self.get_day().zfill(2))

class LogMonthArchiveView(BaseLogArchiveMixin, MonthArchiveView):

    """Month view."""

    date_name = 'Month'

    def get_date_display(self, context):
        """Display for current month."""
        return '{year}/{month}'.format(year=self.get_year(),
                                       month=self.get_month().zfill(2))

class LogYearArchiveView(BaseLogArchiveMixin, YearArchiveView):

    """Year view."""

    date_name = 'Year'

    def get_date_display(self, context):
        """Dispaly for current year."""
        return '{year}'.format(year=self.get_year())

class DeleteLogView(DeleteView):

    """Delete log view."""

    model = Log
    template_name = 'logs/generic_delete.html'

    def get_success_url(self):
        """After delete is complete return to latest log day view."""
        dt = Log.objects.latest().start
        return reverse('logs-day',
                       kwargs={'year': dt.year,
                               'month': dt.month,
                               'day': dt.day})

class BaseLogMixin(LoginRequiredMixin):

    """Base Mixin for update/create log views."""

    model = Log
    form_class = LogForm
    template_name = 'logs/generic_form.html'

    def get_context_data(self, **kwargs):
        """Add Form title to context."""
        context = super(BaseLogMixin, self).get_context_data(**kwargs)
        context['form_title'] = self.form_title
        return context

    def form_valid(self, form):
        """If form is valid, save and return the date for current object."""
        instance = form.save()
        dt = instance.start
        # # do a redirect here
        return HttpResponseRedirect(reverse('logs-day',
                                            kwargs={'year': dt.year,
                                                    'month': dt.month,
                                                    'day': dt.day}))

class CreateLogView(BaseLogMixin, CreateView):

    """Create log view."""

    # template_name = "log_form.html"
    form_title = 'Create Log'
    template_name = 'logs/generic_form.html'

    def get_initial(self):
        """Set current user and set starts date to now."""
        initial = {'user': self.request.user,
                   'start': timezone.now()}
        try:
            latest = Log.objects.latest()
            initial['job'] = latest.job
        except Log.DoesNotExist:
            pass

        return initial

class UpdateLogView(BaseLogMixin, UpdateView):

    """Update log view."""

    form_title = 'Update Log'

    def get_initial(self):
        """Set finish date to now."""
        if self.object.finish:
            result = {}
        else:
            result = {'finish': timezone.now()}

        return result

class BaseJobMixin(object):

    """Base for create and update job views."""

    model = Job
    form_class = JobForm
    template_name = 'logs/generic_form.html'

    def get_success_url(self):
        """After operation is complete, return jobs list page."""
        return reverse('logs-jobs')

    def get_context_data(self, **kwargs):
        """Add form title to context."""
        context = super(BaseJobMixin, self).get_context_data(**kwargs)
        context['form_title'] = self.form_title
        return context

class CreateJobView(BaseJobMixin, CreateView):

    """Create Job view."""

    form_title = 'Create Job'

    def get_initial(self):
        """Add current user to initial data."""
        return {'user': self.request.user}

class UpdateJobView(BaseJobMixin, UpdateView):

    """Update Job view."""

    form_title = 'Update Job'

class DeleteJobView(LoginRequiredMixin, DeleteView):

    """Delete Job view."""

    model = Job
    template_name = 'logs/generic_delete.html'

    def get_success_url(self):
        """After operation is complete, return to jobs list view."""
        return reverse('logs-jobs')

class JobListView(LoginRequiredMixin, ListView):

    """Jobs list view."""

    def get_queryset(self):
        """Filter jobs by current user."""
        qs = Job.objects.filter(user=self.request.user)
        return qs

#####################
# NUMERIC ENDPOINTS #
#####################

def last_day_hours(user):
    """Total hours for latest day.

    Currently active job is also included to calculation.
    """
    try:
        latest_log = user.log_set.latest()
    except Log.DoesNotExist:
        return LabelResponse('No Data Available')
    latest_day = user.log_set.by_day(latest_log.start)
    duration = latest_day.total_duration()
    # check if there is any unfinished logs
    logs = user.log_set.filter(finish__isnull=True)[:1]
    # if there is an unfinished log and unfinished log has the same date
    # is latest_log add unfinished log to result
    if logs and logs[0].start.date() == latest_log.start.date():
        duration += logs[0].get_duration()
    latest_day_display = str(timezone.localtime(latest_log.start).date())
    return LabelResponse(second_to_str(duration),
                         latest_day_display)

def previous_month_hours(user):
    """Total hours for latest month.

    Currently active job is also included to calculation.
    """
    months = user.log_set.datetimes('start', 'month', order='DESC')[:2]
    if len(months) == 2:
        month = months[1]
    else:
        return LabelResponse('No Data Available')

    previous_month = user.log_set.by_month(month)
    duration = previous_month.total_duration()
    previous_log = previous_month[0]
    # check if there is any unfinished logs
    logs = user.log_set.filter(finish__isnull=True)[:1]
    # if there is an unfinished log and unfinished log has the same date
    # is latest_log add unfinished log to result
    if logs and logs[0].start.date() == previous_log.start.date():
        duration += logs[0].get_duration()

    postfix = timezone.localtime(previous_log.start).strftime('%B %Y')
    return LabelResponse(second_to_str(duration), postfix)

def last_month_hours(user):
    """Total hours for latest month.

    Currently active job is also included to calculation.
    """
    latest_log = user.log_set.latest()
    latest_month = user.log_set.by_month(latest_log.start)
    duration = latest_month.total_duration()

    # check if there is any unfinished logs
    logs = user.log_set.filter(finish__isnull=True)[:1]
    # if there is an unfinished log and unfinished log has the same date
    # is latest_log add unfinished log to result
    if logs and logs[0].start.date() == latest_log.start.date():
        duration += logs[0].get_duration()
    postfix = timezone.localtime(latest_log.start).strftime('%B %Y')
    response = LabelResponse(second_to_str(duration), postfix)
    return response

def current_job(user):
    """Currntly active job information.

    Info includes name of the job and total time spent for this job.
    If there is no active log, 'Not Working' will be returned.
    """
    logs = user.log_set.filter(finish__isnull=True)[:1]
    if logs:
        log = logs[0]
        result = LabelResponse(log.job.name,
                               log.get_duration_display())
    else:
        log = user.log_set.latest()
        result = LabelResponse('Not Working',
                               str(timezone.localtime(log.start).date()))

    return result

def total_users(user):
    """Return total number of users."""
    user_count = User.objects.filter(is_active=True).count()

    return NumberResponse(user_count, 'Total number of users')

register('total-users', total_users, NumberResponse)
register('last-day-hours', last_day_hours, LabelResponse)
register('last-month-hours', last_month_hours, LabelResponse)
register('previous-month-hours', previous_month_hours, LabelResponse,
         cache_timeout=60*60)  # one hour
register('current-job', current_job, LabelResponse)


text:
import os
import sys
import re
import threading
import time

from src.map.factory.band_shape_map_factory import BandShapeMapFactory
from src.map.factory.uni_shape_map_factory import UniShapeMapFactory
from src.obj.grid.base_grid import BaseFace
from src.obj.grid.triangle_grid import TriangleGrid
from src.obj.grid.icosahedron_grid import IcosahedronGrid
from src.obj.obj3d import Obj3d
from src.util.parse_util import parse_cla
from src.view.qt_main import main, MainWindow

def handler(kwargs):
    """

    実行ハンドラ

    """
    n_div = kwargs[MainWindow.KEY_N_DIV]
    grid_scale = kwargs[MainWindow.KEY_GRID_SCALE]
    model_root_path = kwargs[MainWindow.KEY_MODEL_PATH]
    grid_path = kwargs[MainWindow.KEY_GRID_PATH]
    cla_path = kwargs[MainWindow.KEY_CLA_PATH]
    save_root_path = kwargs[MainWindow.KEY_SAVE_PATH]

    def execute():

        cla = parse_cla(cla_path)

        for model_name in os.listdir(model_root_path):

            start = time.clock()

            print model_name, "..."
            model_id = int(re.search('\d+', model_name).group())
            model_label = [label for label, aff_ids in cla.items()
                           if model_id in aff_ids][0]
            cls = cla.keys().index(model_label)

            off_path = os.path.join(model_root_path, model_name)

            print "creator being generated..."
            obj3d = Obj3d.load(off_path)
            grid3d = IcosahedronGrid.load(grid_path)
            factory = UniShapeMapFactory(model_id, obj3d, grid3d, n_div, cls,
                                         grid_scale,
                                         BaseFace.UNI_SCAN_DIRECTION)

            for shape_map in factory.create():
                print shape_map
                shp_path = os.path.join(save_root_path, str(model_id),
                                        shape_map.traverse_direction.name,
                                        "{}.shp".format(shape_map.face_id))
                shape_map.save(shp_path)

            print (time.clock() - start), "s"

    threading.Thread(target=execute).start()

def band_map_handler(kwargs):
    """

    実行ハンドラ

    """
    n_div = kwargs[MainWindow.KEY_N_DIV]
    grid_scale = kwargs[MainWindow.KEY_GRID_SCALE]
    model_root_path = kwargs[MainWindow.KEY_MODEL_PATH]
    grid_path = kwargs[MainWindow.KEY_GRID_PATH]
    cla_path = kwargs[MainWindow.KEY_CLA_PATH]
    save_root_path = kwargs[MainWindow.KEY_SAVE_PATH]

    def execute():

        cla = parse_cla(cla_path)

        for model_name in os.listdir(model_root_path):

            start = time.clock()

            print model_name, "..."
            model_id = int(re.search('\d+', model_name).group())
            model_label = [label for label, aff_ids in cla.items()
                           if model_id in aff_ids][0]
            cls = cla.keys().index(model_label)

            off_path = os.path.join(model_root_path, model_name)

            print "creator being generated..."
            obj3d = Obj3d.load(off_path)
            grid3d = IcosahedronGrid.load(grid_path)
            factory = BandShapeMapFactory(model_id, obj3d, grid3d, n_div, cls,
                                          grid_scale, TriangleGrid.BAND_TYPE, 0)

            for shape_map in factory.create():
                print shape_map
                shp_path = os.path.join(save_root_path, str(model_id),
                                        "{}.shp".format(
                                            shape_map.band_type.name))
                shape_map.save(shp_path)

            print (time.clock() - start), "s"

    threading.Thread(target=execute).start()

if __name__ == '__main__':
    title, x, y, width, height = sys.argv[1:]
    main(title, int(x), int(y), int(width), int(height), band_map_handler)


text:
namespace scalloc {

class Arena {
 public:
  // Globally constructed, hence we use staged construction.
  always_inline Arena() {}
  always_inline ~Arena() {}

  always_inline void Init(size_t size, size_t alignment, const char* name);
  always_inline bool Contains(const void* p);
  always_inline void* Allocate(size_t size);
  always_inline void* AllocateVirtualSpan();

 private:
  const char* name_;

  // All of the following members hold pointers to their respective locations.
  // Use unsigned to make comparison against size_t easy.
  uintptr_t start_;
  uintptr_t end_;
  uintptr_t len_;

  UNUSED uint8_t pad1_[64 -
      ((sizeof(name_) +
        sizeof(start_) +
        sizeof(end_) +
        sizeof(len_)) % 64)];

  std::atomic<uintptr_t> current_;
  UNUSED uint8_t pad2_[64  -
      ((sizeof(current_)) % 64)];
};

void Arena::Init(size_t size, size_t alignment, const char* name) {
  name_ = name;
  len_ = size;
  bool needs_aligning = false;
  start_ = reinterpret_cast<uintptr_t>(
      SystemMmapGuided(reinterpret_cast<void*>(alignment), size));
  if (start_ == 0) {
    start_ = reinterpret_cast<uintptr_t>(SystemMmap(size + alignment));
    needs_aligning = true;
  }
  if (start_ == 0) {
    Fatal("initial mmap of arena failed. "
          "consult online docs for requirements (overcommit_memory)");
  }
  if (needs_aligning) {
    start_ += alignment - (start_ % alignment);
  }
  ScallocAssert((start_ % alignment) == 0);
  end_ = start_ + size;
  current_.store(start_);
#if defined(SCALLOC_STRICT_DUMP) && defined(MADV_DONTDUMP)
  madvise(reinterpret_cast<void*>(start_), len_, MADV_DONTDUMP);
#endif  // DEBUG && MADV_DONTDUMP

  LOG(kTrace, "arena at %p", this);
}

bool Arena::Contains(const void* p) {
  // Requires proper alignment of start_!
  return LIKELY((reinterpret_cast<uintptr_t>(p) ^ start_) < len_);
}

void* Arena::Allocate(size_t size) {
  LOG(kTrace, "allocate: %lu", size);
  uintptr_t obj = current_.fetch_add(size);
  if (UNLIKELY((obj + size) >= end_)) {
    Fatal("%s arena OOM; start: %p, end: %p, curr: %p",
        name_, start_, end_, current_.load());
  }
  LOG(kTrace, "%s: obj: %p", name_, obj);
#if defined(SCALLOC_STRICT_DUMP) && defined(MADV_DODUMP)
  madvise(reinterpret_cast<void*>(obj & kPageNrMask),
          ((size / kPageSize) + 1) * kPageSize,
          MADV_DODUMP);
#endif  // DEBUG && MADV_DODUMP
  return reinterpret_cast<void*>(obj);
}

void* Arena::AllocateVirtualSpan() {
  LOG(kTrace, "allocate: %lu", kVirtualSpanSize);
  uintptr_t obj = current_.fetch_add(kVirtualSpanSize);
  if (UNLIKELY((obj + kVirtualSpanSize) >= end_)) {
    Fatal("%s arena OOM; start: %p, end: %p, curr: %p",
        name_, start_, end_, current_.load());
  }
  LOG(kTrace, "%s: obj: %p", name_, obj);
#if defined(SCALLOC_STRICT_DUMP) && defined(MADV_DODUMP)
  madvise(reinterpret_cast<void*>(obj), kVirtualSpanSize, MADV_DODUMP);
#endif  // DEBUG && MADV_DODUMP
  return reinterpret_cast<void*>(obj);
}

}  // namespace scalloc

#endif  // SCALLOC_ARENA_H_


text:
import { combineReducers } from 'redux'
import session from './session'
import users from './user'
import twoots from './twoot'
import errors from './errors'

const index = combineReducers({
  session,
  users,
  twoots,
  errors
})

export default index