"""
swag-api
========

Web interface for SWAG data.

:copyright: (c) 2017 by Netflix, see AUTHORS for more
:license: Apache, see LICENSE for more details.
"""
import os

from setuptools import find_packages, setup

try:  # for pip >= 10
    from pip._internal.req import parse_requirements
except ImportError:  # for pip <= 9.0.3
    from pip.req import parse_requirements
try:  # for pip >= 10
    from pip._internal.download import PipSession
except ImportError:  # for pip <= 9.0.3
    from pip.download import PipSession

ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__)))

about = {}
with open(os.path.join(ROOT, 'swag_api', '__about__.py')) as f:
    exec(f.read(), about)  # nosec: about file is benign

# Gather install requirements from requirements.txt
install_reqs = parse_requirements('requirements.txt', session=PipSession())
install_requires = [str(ir.req) for ir in install_reqs]

# Gather test requirements from requirements-test.txt
test_reqs = parse_requirements('requirements-test.txt', session=PipSession())
tests_requires = [str(tr.req) for tr in test_reqs]

setup(
    name=about["__title__"],
    version=about["__version__"],
    author=about["__author__"],
    author_email=about["__email__"],
    url=about["__uri__"],
    description=about["__summary__"],
    packages=find_packages(),
    include_package_data=True,
    install_requires=install_requires,
    extras_require={
        'tests': tests_requires
    },
    setup_requires=[
        'pytest-runner',
    ],
    entry_points={
        'console_scripts': [
            'swag-api=swag_api:cli.main',
        ]
    }
)


from xai.brain.wordbase.nouns._oral import _ORAL

#calss header
class _ORALS(_ORAL, ):
	def __init__(self,):
		_ORAL.__init__(self)
		self.name = "ORALS"
		self.specie = 'nouns'
		self.basic = "oral"
		self.jsondata = {}


from sklearn.preprocessing import Imputer, LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd

class data_helper:
    def __init__(self):
        pass

    def pre_scale_and_export(self, X_train, X_test, y_train, y_test, data_set_name):
        '''
        This is helpful for Assignment 2 to scale and export data for use in ABAGAIL
        '''

        y_test.shape = (y_test.shape[0], 1)
        X_test = StandardScaler().fit_transform(X_test)
        xy_test = np.hstack((X_test, y_test))
        fn = './output/' + data_set_name + '_test.txt'
        np.savetxt(fn, xy_test, delimiter=',')

        y_train.shape = (y_train.shape[0], 1)
        X_train = StandardScaler().fit_transform(X_train)
        xy_train = np.hstack((X_train, y_train))
        fn = './output/' + data_set_name + '_train.txt'
        np.savetxt(fn, xy_train, delimiter=',')

    def load_preprocess_and_split_wine_data(self):

        '''
        CORR
                               quality
        fixed acidity        0.124052
        volatile acidity     -0.390558
        citric acid          0.226373
        residual sugar       0.013732
        chlorides            -0.128907
        free sulfur dioxide  -0.050656
        total sulfur dioxide -0.185100
        density              -0.174919
        pH                    -0.057731
        sulphates             0.251397
        alcohol               0.476166
        quality               1.000000
        '''

        df = pd.read_csv('./data/winequality-red.csv', sep=';')

        split = df['quality'].median()
        df['quality_2'] = df['quality']

        # group the quality into binary good or bad
        df.loc[(df['quality'] >= 0) & (df['quality'] < split), 'quality_2'] = 0
        df.loc[(df['quality'] >= split), 'quality_2'] = 1

        df['volatile_acidity_ph_ratio'] = df['volatile acidity'] / df['pH']
        df['fixed_acidity_ph_ratio'] = df['fixed acidity'] / df['pH']
        df['sulphates_residual_sugar_ratio'] = df['sulphates'] / df['residual sugar']
        df['alcohol_residual_sugar_ratio'] = df['alcohol'] / df['residual sugar']
        df['volatile_acidity_ph_ratio'] = df['volatile acidity'] / df['pH']

        #x_col_names = ['volatile acidity', 'alcohol', 'volatile_acidity_ph_ratio']
        x_col_names = ['alcohol', 'volatile acidity', 'sulphates', 'pH']

        x, y = df.loc[:,x_col_names].values, df.loc[:,'quality_2'].values

        # split the data into training and test data
        # for the wine data using 30% of the data for testing
        X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

        return X_train, X_test, y_train, y_test

    def load_preprocess_and_split_titanic_data(self, pct_testing=0.3):

        '''
        Load Titanic data, preprocess with imputing, one hot encoding, and split

        Example row:

        PassengerId                          1
        Survived                             0
        Pclass                               3
        Name           Braund, Mr. Owen Harris
        Sex                               male
        Age                                 22
        SibSp                                1
        Parch                                0
        Ticket                       A/5 21171
        Fare                              7.25
        Cabin                              NaN
        Embarked                             S
        '''

        df = pd.read_csv('./data/titanic_train.csv', sep=',')

        '''
        We need to encode sex. Using the sklearn label encoder is one way. However one
        consideration is that the learning algorithm may make assumptions about the
        magnitude of the labels. For example, male is greater than female. Use one hot
        encoding to get around this.
        '''
        # Sklearn OHE
        #ohe = OneHotEncoder(categorical_features=[0])
        #ohe.fit_transform(x).toarray()

        # Even better pandas has a one hot encoding built in!
        dfx = pd.get_dummies(df[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Cabin', 'Embarked']])

        '''
        This data set is missing some values like ages. We could impute a value like
        the average or median or remove the rows having missing data. The disadvantage
        of removing values is we may be taking away valuable information that the
        learning algorithm needs.
        '''

        # Impute most frequent to any missing value, except the y value
        imr = Imputer(strategy='most_frequent')
        imr.fit(dfx)

        # Separate x and y
        x = imr.transform(dfx)
        y = df.loc[:,'Survived'].values

        # Manually reduce the number of features?
        #x_col_names = ['Pclass', 'Age', 'Fare', 'SibSp', 'Parch', 'Sex_female', 'Sex_male']
        #x = df.loc[:,x_col_names].values

        '''
        Split the data into training and test data for the wine data holding out 30%
        by default of the data for testing
        '''
        X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=pct_testing, random_state=0)

        return X_train, X_test, y_train, y_test

if __name__== '__main__':
    dh = data_helper()
    X_train, X_test, y_train, y_test = dh.load_preprocess_and_split_titanic_data()
    dh.pre_scale_and_export(X_train, X_test, y_train, y_test, 'titanic')


from flask import Flask, render_template, request, jsonify
import numpy as np
import sys
# from flask_bootstrap import Bootstrap

from model.classifier import models, smoother#, RepetitionCounter, RepTimer, RangeOfMotion

app = Flask(__name__)
# Bootstrap(app)
pose_class = 'shoulderpress'
# repetition_counter = RepetitionCounter(
    # class_name=pose_class + '_up',
    # enter_threshold=6,
    # exit_threshold=4)
# rep_timer = RepTimer()
# rom = RangeOfMotion(pose_class)

@app.route('/')
def physifeedback():
    return render_template('demo.html')

def updateMetricCountersIfNeeded(pose):
    global pose_class#, repetition_counter, rep_timer, rom
    if pose_class != pose:
        pose_class = pose
        # repetition_counter = RepetitionCounter(
            # class_name=pose_class + '_up',
            # enter_threshold=6,
            # exit_threshold=4)
        # rep_timer = RepTimer()
        # rom = RangeOfMotion(pose_class)

def setPose(pose):
    if pose:
        updateMetricCountersIfNeeded(pose)
        return pose
    else:
        return 'full_model'

# def getRepCount(classification):
#     global repetition_counter
#     prev_count = repetition_counter.n_repeats
#     if classification is not None:
#         return prev_count, repetition_counter(classification)
#     return prev_count, repetition_counter.n_repeats

@app.route('/getpose', methods=['POST'])
def getpose():
    # print(request)
    pose_classification = None
    pose = setPose(request.args.get('pose'))

    json = request.json
    pose_landmarks = np.array(
        [[lmk['x'] * json['width'], lmk['y'] * json['height'], lmk['z'] * json['width']]
         for lmk in json['pose_landmarks']],
        dtype=np.float32)
    pose_classification = models[pose](pose_landmarks)

    # prev_rep_count, curr_rep_count = getRepCount(pose_classification)
    # left, right = rom.rom(pose_landmarks, prev_rep_count, curr_rep_count)
    # avg_left, avg_right = rom.avgROM()
    response = jsonify({'pose': pose_classification,
                        'landmarks': pose_landmarks.tolist(),
                        # 'rep': {
                        #     'count': curr_rep_count,
                        #     'time': rep_timer(int(prev_rep_count), int(curr_rep_count)),
                        #     'avg': rep_timer.average(),
                        # },
                        # 'rom': {
                        #     'left': left,
                        #     'right': right,
                        #     'avg_left': avg_left,
                        #     'avg_right': avg_right,
                        # }
                        })
    response.status_code = 200
    return response

if __name__ == '__main__':
    app.run(debug=True)

# colors:
# #10d4dc
# #fbbc5c
# #d5f3ed
# #fccc7d


import numpy as np
import pandas as pd
import pyro
import pyro.distributions as dist
import torch
from pyro.nn import PyroModule
from scvi import _CONSTANTS
from scvi.data._anndata import get_from_registry
from scvi.nn import one_hot

# class NegativeBinomial(TorchDistributionMixin, ScVINegativeBinomial):
#    pass

class LocationModelMultiExperimentLocationBackgroundNormLevelGeneAlphaPyroModel(PyroModule):
    """
    Cell2location models the elements of :math:`D` as Negative Binomial distributed,
    given an unobserved gene expression level (rate) :math:`mu` and a gene- and batch-specific
    over-dispersion parameter :math:`\alpha_{e,g}` which accounts for unexplained variance:

    .. math::
        D_{s,g} \sim \mathtt{NB}(\mu_{s,g}, \alpha_{e,g})

    The expression level of genes :math:`\mu_{s,g}` in the mRNA count space is modelled
    as a linear function of expression signatures of reference cell types :math:`g_{f,g}`:

    .. math::
        \mu_{s,g} = (m_{g} \left (\sum_{f} {w_{s,f} \: g_{f,g}} \right) + s_{e,g}) y_{s}

    Here, :math:`w_{s,f}` denotes regression weight of each reference signature :math:`f` at location :math:`s`,
      which can be interpreted as the expected number of cells at location :math:`s`
      that express reference signature :math:`f`;
    :math:`g_{f,g}` denotes the reference signatures of cell types :math:`f` of each gene :math:`g`,
      `cell_state_df` input ;
    :math:`m_{g}` denotes a gene-specific scaling parameter which adjusts for global differences in sensitivity
      between technologies (platform effect);
    :math:`y_{s}` denotes a location/observation-specific scaling parameter which adjusts for differences in sensitivity
      between observations and batches;
    :math:`s_{e,g}` is additive component that account for gene- and location-specific shift,
      such as due to contaminating or free-floating RNA.

    To account for the similarity of location patterns across cell types, :math:`w_{s,f}` is modelled using
    another layer  of decomposition (factorization) using :math:`r={1, .., R}` groups of cell types,
    that can be interpreted as cellular compartments or tissue zones. Unless stated otherwise, R is set to 50.

    Corresponding graphical model can be found in supplementary methods:
    https://www.biorxiv.org/content/10.1101/2020.11.15.378125v1.supplementary-material

    Approximate Variational Inference is used to estimate the posterior distribution of all model parameters.

    Estimation of absolute cell abundance `w_{s,f}` is guided using informed prior on the number of cells
    (argument called `N_cells_per_location`). It is a tissue-level global estimate, which can be derived from histology
    images (H&E or DAPI), ideally paired to the spatial expression data or at least representing the same tissue type.
    This parameter can be estimated by manually counting nuclei in a 10-20 locations in the histology image
    (e.g. using 10X Loupe browser), and computing the average cell abundance.
    An appropriate setting of this prior is essential to inform the estimation of absolute cell type abundance values,
    however, the model is robust to a range of similar values.
    In settings where suitable histology images are not available, the size of capture regions relative to
    the expected size of cells can be used to estimate `N_cells_per_location`.

    The prior on detection efficiency per location :math:`y_s` is selected to discourage over-normalisation, such that
    unless data has evidence of strong technical effect, the effect is assumed to be small and close to
    the mean sensitivity for each batch :math:`y_e`:

    .. math::
        y_s ~ Gamma(detection_alpha, detection_alpha / y_e)

    where y_e is unknown/latent average detection efficiency in each batch/experiment:

    .. math::
        y_e ~ Gamma(10, 10 / detection_mean)

    """

    def __init__(
        self,
        n_obs,
        n_vars,
        n_factors,
        n_batch,
        cell_state_mat,
        n_groups: int = 50,
        detection_mean=1 / 2,
        detection_alpha=200.0,
        m_g_gene_level_prior={"mean": 1, "mean_var_ratio": 1.0, "alpha_mean": 3.0},
        N_cells_per_location=8.0,
        A_factors_per_location=7.0,
        N_cells_mean_var_ratio=1.0,
        alpha_g_phi_hyp_prior={"alpha": 9.0, "beta": 3.0},
        gene_add_alpha_hyp_prior={"alpha": 9.0, "beta": 3.0},
        gene_add_mean_hyp_prior={
            "alpha": 1.0,
            "beta": 100.0,
        },
        detection_hyp_prior={"mean_alpha": 10.0},
        w_sf_mean_var_ratio=5.0,
    ):

        super().__init__()

        self.n_obs = n_obs
        self.n_vars = n_vars
        self.n_factors = n_factors
        self.n_batch = n_batch
        self.n_groups = n_groups

        self.m_g_gene_level_prior = m_g_gene_level_prior

        self.alpha_g_phi_hyp_prior = alpha_g_phi_hyp_prior
        self.w_sf_mean_var_ratio = w_sf_mean_var_ratio
        self.gene_add_alpha_hyp_prior = gene_add_alpha_hyp_prior
        self.gene_add_mean_hyp_prior = gene_add_mean_hyp_prior
        detection_hyp_prior["mean"] = detection_mean
        detection_hyp_prior["alpha"] = detection_alpha
        self.detection_hyp_prior = detection_hyp_prior

        self.register_buffer(
            "detection_hyp_prior_alpha",
            torch.tensor(self.detection_hyp_prior["alpha"]),
        )
        self.register_buffer(
            "detection_mean_hyp_prior_alpha",
            torch.tensor(self.detection_hyp_prior["mean_alpha"]),
        )
        self.register_buffer(
            "detection_mean_hyp_prior_beta",
            torch.tensor(self.detection_hyp_prior["mean_alpha"] / self.detection_hyp_prior["mean"]),
        )

        # compute hyperparameters from mean and sd
        self.register_buffer("m_g_mu_hyp", torch.tensor(self.m_g_gene_level_prior["mean"]))
        self.register_buffer(
            "m_g_mu_mean_var_ratio_hyp",
            torch.tensor(self.m_g_gene_level_prior["mean_var_ratio"]),
        )

        self.register_buffer("m_g_alpha_hyp_mean", torch.tensor(self.m_g_gene_level_prior["alpha_mean"]))

        self.cell_state_mat = cell_state_mat
        self.register_buffer("cell_state", torch.tensor(cell_state_mat.T))

        self.register_buffer("N_cells_per_location", torch.tensor(N_cells_per_location))
        self.register_buffer("A_factors_per_location", torch.tensor(A_factors_per_location))
        self.register_buffer("N_cells_mean_var_ratio", torch.tensor(N_cells_mean_var_ratio))

        self.register_buffer(
            "alpha_g_phi_hyp_prior_alpha",
            torch.tensor(self.alpha_g_phi_hyp_prior["alpha"]),
        )
        self.register_buffer(
            "alpha_g_phi_hyp_prior_beta",
            torch.tensor(self.alpha_g_phi_hyp_prior["beta"]),
        )
        self.register_buffer(
            "gene_add_alpha_hyp_prior_alpha",
            torch.tensor(self.gene_add_alpha_hyp_prior["alpha"]),
        )
        self.register_buffer(
            "gene_add_alpha_hyp_prior_beta",
            torch.tensor(self.gene_add_alpha_hyp_prior["beta"]),
        )
        self.register_buffer(
            "gene_add_mean_hyp_prior_alpha",
            torch.tensor(self.gene_add_mean_hyp_prior["alpha"]),
        )
        self.register_buffer(
            "gene_add_mean_hyp_prior_beta",
            torch.tensor(self.gene_add_mean_hyp_prior["beta"]),
        )

        self.register_buffer("w_sf_mean_var_ratio_tensor", torch.tensor(self.w_sf_mean_var_ratio))

        self.register_buffer("n_factors_tensor", torch.tensor(self.n_factors))
        self.register_buffer("n_groups_tensor", torch.tensor(self.n_groups))

        self.register_buffer("ones", torch.ones((1, 1)))
        self.register_buffer("ones_1_n_groups", torch.ones((1, self.n_groups)))
        self.register_buffer("ones_1_n_factors", torch.ones((1, self.n_factors)))
        self.register_buffer("ones_n_batch_1", torch.ones((self.n_batch, 1)))
        self.register_buffer("eps", torch.tensor(1e-8))

    @staticmethod
    def _get_fn_args_from_batch(tensor_dict):
        x_data = tensor_dict[_CONSTANTS.X_KEY]
        ind_x = tensor_dict["ind_x"].long().squeeze()
        batch_index = tensor_dict[_CONSTANTS.BATCH_KEY]
        return (x_data, ind_x, batch_index), {}

    def create_plates(self, x_data, idx, batch_index):
        return pyro.plate("obs_plate", size=self.n_obs, dim=-2, subsample=idx)

    def list_obs_plate_vars(self):
        """Create a dictionary with:
        1. "name" - the name of observation/minibatch plate;
        2. "input" - indexes of model args to provide to encoder network when using amortised inference;
        3. "sites" - dictionary with
            keys - names of variables that belong to the observation plate (used to recognise
             and merge posterior samples for minibatch variables)
            values - the dimensions in non-plate axis of each variable (used to construct output
             layer of encoder network when using amortised inference)
        """

        return {
            "name": "obs_plate",
            "input": [0, 2],  # expression data + (optional) batch index
            "input_transform": [
                torch.log1p,
                lambda x: x,
            ],  # how to transform input data before passing to NN
            "sites": {
                "w_sf": self.n_factors,
                "detection_y_s": 1,
            },
        }

    def forward(self, x_data, idx, batch_index):

        obs2sample = one_hot(batch_index, self.n_batch)

        obs_plate = self.create_plates(x_data, idx, batch_index)

        # =====================Gene expression level scaling m_g======================= #
        # Explains difference in sensitivity for each gene between single cell and spatial technology
        m_g_mean = pyro.sample(
            "m_g_mean",
            dist.Gamma(
                self.m_g_mu_mean_var_ratio_hyp * self.m_g_mu_hyp,
                self.m_g_mu_mean_var_ratio_hyp,
            )
            .expand([1, 1])
            .to_event(2),
        )  # (1, 1)

        m_g_alpha_e_inv = pyro.sample(
            "m_g_alpha_e_inv",
            dist.Exponential(self.m_g_alpha_hyp_mean).expand([1, 1]).to_event(2),
        )  # (1, 1)
        m_g_alpha_e = self.ones / m_g_alpha_e_inv.pow(2)

        m_g = pyro.sample(
            "m_g",
            dist.Gamma(m_g_alpha_e, m_g_alpha_e / m_g_mean).expand([1, self.n_vars]).to_event(2),  # self.m_g_mu_hyp)
        )  # (1, n_vars)

        # =====================Cell abundances w_sf======================= #
        # factorisation prior on w_sf models similarity in locations
        # across cell types f and reflects the absolute scale of w_sf
        n_cells_per_location = pyro.sample(
            "n_cells_per_location",
            dist.Gamma(
                self.N_cells_per_location * self.N_cells_mean_var_ratio,
                self.N_cells_mean_var_ratio,
            ),
        )

        a_factors_per_location = pyro.sample(
            "a_factors_per_location",
            dist.Gamma(self.A_factors_per_location, self.ones),
        )

        # cell group loadings
        shape = self.ones_1_n_factors * a_factors_per_location / self.n_factors_tensor
        rate = self.ones_1_n_factors / (n_cells_per_location / a_factors_per_location)

        with obs_plate:
            w_sf = pyro.sample(
                "w_sf",
                dist.Gamma(
                    shape,
                    rate,
                ),
            )  # (self.n_obs, self.n_factors)

        # =====================Location-specific detection efficiency ======================= #
        # y_s with hierarchical mean prior
        detection_mean_y_e = pyro.sample(
            "detection_mean_y_e",
            dist.Gamma(
                self.ones * self.detection_mean_hyp_prior_alpha,
                self.ones * self.detection_mean_hyp_prior_beta,
            )
            .expand([self.n_batch, 1])
            .to_event(2),
        )
        detection_hyp_prior_alpha = pyro.deterministic(
            "detection_hyp_prior_alpha",
            self.ones_n_batch_1 * self.detection_hyp_prior_alpha,
        )

        beta = (obs2sample @ detection_hyp_prior_alpha) / (obs2sample @ detection_mean_y_e)
        with obs_plate:
            detection_y_s = pyro.sample(
                "detection_y_s",
                dist.Gamma(obs2sample @ detection_hyp_prior_alpha, beta),
            )  # (self.n_obs, 1)

        # =====================Gene-specific additive component ======================= #
        # per gene molecule contribution that cannot be explained by
        # cell state signatures (e.g. background, free-floating RNA)
        s_g_gene_add_alpha_hyp = pyro.sample(
            "s_g_gene_add_alpha_hyp",
            dist.Gamma(self.gene_add_alpha_hyp_prior_alpha, self.gene_add_alpha_hyp_prior_beta),
        )
        s_g_gene_add_mean = pyro.sample(
            "s_g_gene_add_mean",
            dist.Gamma(
                self.gene_add_mean_hyp_prior_alpha,
                self.gene_add_mean_hyp_prior_beta,
            )
            .expand([self.n_batch, 1])
            .to_event(2),
        )  # (self.n_batch)
        s_g_gene_add_alpha_e_inv = pyro.sample(
            "s_g_gene_add_alpha_e_inv",
            dist.Exponential(s_g_gene_add_alpha_hyp).expand([self.n_batch, 1]).to_event(2),
        )  # (self.n_batch)
        s_g_gene_add_alpha_e = self.ones / s_g_gene_add_alpha_e_inv.pow(2)

        s_g_gene_add = pyro.sample(
            "s_g_gene_add",
            dist.Gamma(s_g_gene_add_alpha_e, s_g_gene_add_alpha_e / s_g_gene_add_mean)
            .expand([self.n_batch, self.n_vars])
            .to_event(2),
        )  # (self.n_batch, n_vars)

        # =====================Gene-specific overdispersion ======================= #
        alpha_g_phi_hyp = pyro.sample(
            "alpha_g_phi_hyp",
            dist.Gamma(self.alpha_g_phi_hyp_prior_alpha, self.alpha_g_phi_hyp_prior_beta),
        )
        alpha_g_inverse = pyro.sample(
            "alpha_g_inverse",
            dist.Exponential(alpha_g_phi_hyp).expand([self.n_batch, self.n_vars]).to_event(2),
        )  # (self.n_batch, self.n_vars)

        # =====================Expected expression ======================= #
        # expected expression
        mu = ((w_sf @ self.cell_state) * m_g + (obs2sample @ s_g_gene_add)) * detection_y_s
        alpha = obs2sample @ (self.ones / alpha_g_inverse.pow(2))
        # convert mean and overdispersion to total count and logits
        # total_count, logits = _convert_mean_disp_to_counts_logits(
        #    mu, alpha, eps=self.eps
        # )

        # =====================DATA likelihood ======================= #
        # Likelihood (sampling distribution) of data_target & add overdispersion via NegativeBinomial
        with obs_plate:
            pyro.sample(
                "data_target",
                dist.GammaPoisson(concentration=alpha, rate=alpha / mu),
                # dist.NegativeBinomial(total_count=total_count, logits=logits),
                obs=x_data,
            )

        # =====================Compute mRNA count from each factor in locations  ======================= #
        with obs_plate:
            mRNA = w_sf * (self.cell_state * m_g).sum(-1)
            pyro.deterministic("u_sf_mRNA_factors", mRNA)

    def compute_expected(self, samples, adata, ind_x=None):
        r"""Compute expected expression of each gene in each location. Useful for evaluating how well
        the model learned expression pattern of all genes in the data.
        """
        if ind_x is None:
            ind_x = np.arange(adata.n_obs).astype(int)
        else:
            ind_x = ind_x.astype(int)
        obs2sample = get_from_registry(adata, _CONSTANTS.BATCH_KEY)
        obs2sample = pd.get_dummies(obs2sample.flatten()).values[ind_x, :]
        mu = (
            np.dot(samples["w_sf"][ind_x, :], self.cell_state_mat.T) * samples["m_g"]
            + np.dot(obs2sample, samples["s_g_gene_add"])
        ) * samples["detection_y_s"][ind_x, :]
        alpha = np.dot(obs2sample, 1 / np.power(samples["alpha_g_inverse"], 2))

        return {"mu": mu, "alpha": alpha, "ind_x": ind_x}


import datetime

def timespan_in_days(beginning: datetime.datetime, end: datetime.datetime, result_format = 'days') -> int:

    days = (end - beginning).days

    # convert the result in days using our convert_days dictionary to calculate the result based on the selected format paramater
    return convert_days(days)[result_format]

def num_weekdays(beginning: datetime.datetime, end: datetime.datetime, result_format = 'days') -> int:

    # switch end and beginning if negative timespan
    if beginning > end:
        return -num_weekdays(end, beginning)

    # get a list of datetimes for each day between the two provided dates
    dates = [beginning + datetime.timedelta(days=x) for x in range(0, (end - beginning).days)]

    # filter out weekends from the list
    weekdays = list(filter(lambda date: 0 < date.weekday() < 6, dates))

    total_weekdays = len(weekdays)

    # convert the result in days using our convert_days dictionary to calculate the result based on the selected format paramater
    return convert_days(total_weekdays)[result_format]

def num_complete_weeks(beginning: datetime.datetime, end: datetime.datetime, result_format = 'days') -> int:

    # switch end and beginning if negative timespan
    if beginning > end:
        return -num_complete_weeks(end, beginning)

    # no full week can fit inside this timespan
    if abs((end - beginning).days) < 7:
        return 0

    extra_days_at_beginning = (6 - beginning.weekday())

    extra_days_at_end = (6 - end.weekday())

    total_days = (end - beginning).days

    # removing non complete week days from either side of timespan and dividing by 7 to get the number of weeks
    days = int((total_days - extra_days_at_beginning - extra_days_at_end) / 7)

    # convert the result in days using our convert_days dictionary to calculate the result based on the selected format paramater
    return convert_days(days)[result_format]

def convert_days(days) -> dict:
    return {
        'seconds': days * 86400,
        'minutes': days * 1440,
        'hours': days * 24,
        'days': days,
        'years': int(days / 365.25),
    }


import pyautogui as py
from data import *
from math import floor
from random import randint, random
from PIL import Image

img = Image.open(input("Set image: "))
constrain = lambda n, minn, maxn: max(min(maxn, n), minn)
class Edit:
	def __init__ (self, start, menu, r, g, b, ok):
		self.start = start
		self.menu = menu
		self.r = r
		self.g = g
		self.b = b
		self.ok = ok

class Stroke:
	def __init__ (self, maxStroke, maxOffset, maxLength, count, rand=0):
		self.maxStroke = maxStroke
		self.maxOffset = maxOffset
		self.maxLength = maxLength
		self.count = count
		self.rand = rand

	def setLength(self):
		self.rand = abs(random()*self.maxLength)

	def add(self):
		self.count += 1

class Position:
	def __init__ (self, x1, x2, y1, y2):
		self.x1 = x1
		self.x2 = x2
		self.y1 = y1
		self.y2 = y2

	def reset(self, randX1, randY1, randX2, randY2):
		self.x1 += randX1
		self.y1 += randY1
		self.x2 += randX2
		self.y2 += randY2
		self.x1 = constrain(self.x1, 0, img.size[0]-1)
		self.y1 = constrain(self.y1, 0, img.size[1]-1)
		self.x2 = constrain(self.x2, 0, img.size[0]-1)
		self.y2 = constrain(self.y2, 0, img.size[1]-1)

	def set(self, newX, newY, randX, randY):
		self.x1 = newX
		self.y1 = newY
		self.x2 = self.x1 + randX
		self.y2 = self.y1 + randY

edit = Edit((5, 144), (990, 65), (1150, 612), (1150, 634), (1150, 660), (775, 684))
stroke = Stroke(3, 5, 5, 0)
pos = Position(0, 0, 0, 0)
color = []

def calibrate():
    input("Set up the canvas size, and move to the left-hand corner.")
    edit.start = py.position()

    input("Move to 'Edit Colors'")
    edit.menu = py.position()

    input("Move to 'R'")
    edit.r = py.position()
    edit.g = (py.position()[0], py.position()[1] + 20)
    edit.b = (py.position()[0], py.position()[1] + 40)

    input("Move to 'OK'")
    edit.ok = py.position()

    input("Press ENTER!")

def draw(x1, x2, y1, y2):
	py.moveTo(edit.start[0] + x1, edit.start[1] + y1)
	py.dragTo(edit.start[0] + x2, edit.start[1] + y2)

def setColor (r, g, b):

	py.click(edit.menu)
	py.click(edit.r, clicks=2)
	py.typewrite(str(r))

	py.click(edit.g, clicks=2)
	py.typewrite(str(g))

	py.click(edit.b, clicks=2)
	py.typewrite(str(b))

	py.click(edit.ok)

def newStroke():

	color.clear()

	stroke.setLength()

	pos.set(randRange(0, img.size[0]-1), randRange(0, img.size[1]-1), randRange(-stroke.rand, stroke.rand), randRange(-stroke.rand, stroke.rand))

	for i in range(3):
		color.append(img.getpixel((pos.x1, pos.y1))[i])

def randRange(start, end):
	return floor(random() * (end-start) + start)

def test():
	for i in range(2):
		print(i)

#calibrate()

newStroke()
setColor(color[0], color[1], color[2])

while True:
	pos.reset(randRange(-stroke.maxOffset, stroke.maxOffset), randRange(-stroke.maxOffset, stroke.maxOffset), randRange(-stroke.maxOffset, stroke.maxOffset), randRange(-stroke.maxOffset, stroke.maxOffset))
	draw(pos.x1, pos.x2, pos.y1, pos.y2)

	stroke.add()
	if stroke.count % stroke.maxStroke == 0:
		newStroke()
		setColor(color[0], color[1], color[2])


# -*- coding: utf-8 -*-
from odoo import models, api, fields
import time
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT

class SignRequest(models.Model):
    _inherit = "sign.request"

    signature_subject = fields.Char("Signature Request Subject")
    signature_message = fields.Text("Signature Request Message")

    def set_signers(self, signers):
        self.request_item_ids.filtered(lambda r: not r.partner_id or not r.role_id).unlink()
        ids_to_remove = []
        for request_item in self.request_item_ids:
            for i in range(0, len(signers)):
                if signers[i]['partner_id'] == request_item.partner_id.id and signers[i]['role'] == request_item.role_id.id:
                    signers.pop(i)
                    break
            else:
                ids_to_remove.append(request_item.id)

        SignRequestItem = self.env['sign.request.item']
        SignRequestItem.browse(ids_to_remove).unlink()
        for signer in signers:
            sequence = signer.get('sequence')
            SignRequestItem.create({
                'partner_id': signer['partner_id'],
                'sequence': sequence,
                'sign_request_id': self.id,
                'role_id': signer['role']
            })

    @api.model
    def initialize_new(self, id, signers, followers, reference, subject, message, send=True, without_mail=False):
        sign_request = self.create({'template_id': id,
                                    'reference': reference,
                                    'favorited_ids': [(4, self.env.user.id)],
                                    'signature_subject':subject,
                                    'signature_message' :message})
        sign_request.message_subscribe(partner_ids=followers)
        sign_request.set_signers(signers)

        if send:
            no_sign_needed = sign_request.request_item_ids.filtered(lambda x:not x.partner_id or not x.partner_id.email)
            if no_sign_needed:
                no_sign_needed.write({'state':'sent'})
            request_items = sign_request.request_item_ids.filtered(lambda r: r.partner_id.id not in no_sign_needed.ids or r.state=='draft')
            sequences = request_items.mapped('sequence')
            if sequences:
                sequences.sort()
                request_items = request_items.filtered(lambda x:x.sequence==sequences[0])
                request_items.send_signature_accesses(subject, message)
                request_items.write({'state':'sent'})
                sign_request.write({'state': 'sent'})
            else:
                sign_request.action_sent(subject, message)

            followers = sign_request.message_follower_ids.mapped('partner_id')
            followers -= sign_request.create_uid.partner_id
            followers -= sign_request.request_item_ids.mapped('partner_id')
            if followers:
                sign_request.send_follower_accesses(followers, subject, message)
            #signature_request.action_sent(subject, message)
            #sign_request._message_post(_('Waiting for signatures.'), type='comment', subtype='mt_comment')

        sign_token = sign_request.request_item_ids.filtered(lambda r: r.partner_id == self.env.user.partner_id)
        access_token = ''
        if sign_token:
            access_token = sign_token[0].access_token
        return {
            'id': sign_request.id,
            'token': sign_request.access_token,
            'sign_token': access_token, #signature_request.request_item_ids.filtered(lambda r: r.partner_id == self.env.user.partner_id).access_token,
        }

class SignRequestItem(models.Model):
    _inherit = "sign.request.item"

    sequence = fields.Integer("Sequence")

    def action_completed(self):
        self.write({'signing_date': time.strftime(DEFAULT_SERVER_DATE_FORMAT), 'state': 'completed'})

        signature_request = self.mapped('sign_request_id')
        request_items = signature_request.request_item_ids.filtered(lambda x:x.sequence==self.sequence)
        state = list(set(request_items.mapped('state')))
        if len(state)==1 and state[0]=='completed':
            request_items = signature_request.request_item_ids.filtered(lambda r: not r.partner_id or r.state=='draft')
            sequences = request_items.mapped('sequence')
            if sequences:
                sequences.sort()
                request_items = request_items.filtered(lambda x:x.sequence==sequences[0])
                request_items.send_signature_accesses(signature_request.signature_subject, signature_request.signature_message)
                request_items.write({'state':'sent'})
                if signature_request.nb_closed == len(signature_request.request_item_ids) and len(signature_request.request_item_ids) > 0:
                    signature_request._check_after_compute()

            else:
                signature_request._check_after_compute()


import model.db as model
from alnilam import Alnilam

class Betelgeuse:

	def __init__(self):
		pass

	def get_first_bilibili(avid):
		return model.BilibiliMovie.select().where(model.BilibiliMovie.avid == avid ).limit(1).first()

	def get_first_bilibili_media(cid):
		return model.BilibiliMedia.select().where(model.BilibiliMedia.cid == cid ).limit(1).first()

	def build_bilibili_genre(bilibili, genre_name):
		genre = Alnilam.get_first_genre(genre_name)
		if genre is None:
			genre = model.Genre.create(name=genre_name)
			bilibili_genre = model.BilibiliGenre.create(bilibili=bilibili, genre=genre)
		else:
			bilibili_genre = model.BilibiliGenre.select().where(model.BilibiliGenre.bilibili == bilibili, model.BilibiliGenre.genre == genre).limit(1).first()
			if bilibili_genre is None:
				bilibili_genre = model.BilibiliGenre.create(bilibili=bilibili, genre=genre)

	def replenish_bilibili(self, avid, data, order=1):
		bilibili = Betelgeuse.get_first_bilibili(avid)
		if bilibili is not None:
			bilibili.pages = data.pages
			bilibili.save()
			bilibili_media = Betelgeuse.get_first_bilibili_media(data.cid)
			if bilibili_media is None:
				bilibili_media = model.BilibiliMedia.create(avid=avid, order=order, mid=data.mid, cid=data.cid, offsite=data.offsite,
					download=data.download, expires=data.expires)

	def build_bilibili(self, data, douban_id=None):
		bilibili = Betelgeuse.get_first_bilibili(data["aid"])
		if bilibili is None:
			bilibili = model.BilibiliMovie.create(douban_id=douban_id, avid=data["aid"], author=data["author"],
				typename=data["typename"], arcurl=data["arcurl"], description=data["description"], title=data["title"],
				play=data["play"])

			for tag in data["tag"].split(","):
				Betelgeuse.build_bilibili_genre(bilibili, tag)


def bubbleSort(arr):

    #Iterate through all the elements
    for i in range(len(arr)):

        #Iterate through the element that are NOT in place
        #The last elements will be in place
        for j in range(0, len(arr)-i-1):

            #swap if the element is greater than the next
            #element
            if arr[j] > arr[j+1] :
                arr[j], arr[j+1] = arr[j+1], arr[j]


#869

def find_derangement(n):
    if(n == 0 or n == 1):
        return 0

    _visited = []
    for i in range(0, n):
        _visited.append(False)

    _cnt = 0
    for i in range(1, n):
        _tmp_stack = [(i,0,_visited)]
        while(_tmp_stack != []):
            _curr_val, _curr_depth, _curr_visited = _tmp_stack.pop()
            if(_curr_depth == n-1):
                _cnt += 1
            _new_visited = _curr_visited[:]
            _new_visited[_curr_val] = True
            for k in range(0, n):
                if(_new_visited[k] == False and k != _curr_depth + 1):
                    _tmp_stack.append([k, _curr_depth+1, _new_visited])

    return _cnt % (10**9 + 7)

find_derangement(15)

# for i in range(2, 10):
#     print(i, find_derangement(i))


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Users/rudieshahinian/Projects/rudie/teslajsonpy2/teslajsonpy2/Exceptions.py
# Compiled at: 2018-06-14 15:41:27
# Size of source mod 2**32: 777 bytes

class TeslaException(Exception):

    def __init__(self, code, *args, **kwargs):
        self.message = ''
        (super().__init__)(*args, **kwargs)
        self.code = code
        if self.code == 401:
            self.message = 'UNAUTHORIZED'
        else:
            if self.code == 404:
                self.message = 'NOT_FOUND'
            else:
                if self.code == 405:
                    self.message = 'MOBILE_ACCESS_DISABLED'
                else:
                    if self.code == 423:
                        self.message = 'ACCOUNT_LOCKED'
                    else:
                        if self.code == 429:
                            self.message = 'TOO_MANY_REQUESTS'
                        else:
                            if self.code == 500:
                                self.message = 'SERVER_ERROR'
                            else:
                                if self.code == 503:
                                    self.message = 'SERVICE_MAINTENANCE'
                                elif self.code > 299:
                                    self.message = 'UNKNOWN_ERROR'


import json
import re
import sys
import time
import threading
import socket
import psutil
from socket import AddressFamily

def get_inter_ip():
    """
    查询本机所有ip地址
    :return: ip
    """
    local_addrs = []
    for name, info in psutil.net_if_addrs().items():
        for addr in info:
            if AddressFamily.AF_INET == addr.family:
                if addr.address == "127.0.0.1":
                    continue
                local_addrs.append(addr.address)
    return local_addrs

class MyThread(threading.Thread):
    def __init__(self, target, args=()):
        """
            因为threading类没有返回值,因此在此处重新定义MyThread类,使线程拥有返回值
        """
        super(MyThread, self).__init__()
        self.func = target
        self.args = args

    def run(self):
        # 接受返回值
        self.result = self.func(*self.args)

    def get_result(self):
        # 线程不结束,返回值为None
        try:
            return self.result
        except Exception:
            return None

def limit_decor(timeout, granularity):
    """
    timeout 最大允许执行时长, 单位:秒
    granularity 轮询间隔，间隔越短结果越精确同时cpu负载越高
    return 未超时返回被装饰函数返回值,超时则返回 None
    """

    def functions(func):
        def run(*args):
            thre_func = MyThread(target=func, args=args)
            thre_func.setDaemon(True)
            thre_func.start()
            sleep_num = int(timeout // granularity)
            for i in range(0, sleep_num):
                infor = thre_func.get_result()
                if infor:
                    return infor
                else:
                    time.sleep(granularity)
            return ""

        return run

    return functions

class WebSocket(object):
    """
    WebSocket服务端
    """

    def __init__(self):
        from utils.logger import Log
        self.logger = Log().logger
        self.tcp_server = None
        self.bind()

    def bind(self):
        try:
            self.tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.tcp_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.tcp_server.bind(("", 5201))
            self.tcp_server.listen(128)
            font_color = ["\033[1;36m", "\033[0m"]
            print(f"""{font_color[0] if sys.platform != "win32" else ""}注意事项：
1. 手机端请求IP地址为如下监听地址，请先测试是否可以访问通
2. 用手机浏览器测试访问说明1中尝试过的IP地址，如访问通代表无问题
3. 以下IP获取到的IP仅做参考，如果全部访问不通，请检查防火墙开启5201端口或使用ipconfig/ifconfig查看本地其他IP
4. 记得更改手机端的请求地址，并授权软件短信权限和验证码获取权限{font_color[1] if sys.platform != "win32" else ""}
            """)
            for idx, val in enumerate(get_inter_ip()):
                self.logger.info(f"监听地址{idx+1}:\thttp://{val}:5201/publish?smsCode=123456")
        except:
            self.logger.error("监听失败,请查看是否有同端口脚本")

    # 等待时间30 轮询时间0.5
    @limit_decor(30, 0.5)
    def listener(self, *args, **kwargs):
        """
        通过 socket 监听
        """
        while True:
            try:
                cs, ca = self.tcp_server.accept()
                recv_data = cs.recv(1024)
                try:
                    a = str(re.search(r'smsCode=(\d+)', str(recv_data)).group(1))
                    self.logger.info(f'监听到京东验证码:\t{a}')
                    return json.dumps({"sms_code": a})
                except AttributeError:
                    self.logger.warnning(f"监听到IP: \t{ca[0]}\t访问，但未获取到短信验证码")
            except:
                return ""

if __name__ == '__main__':
    while True:
        print(WebSocket().listener())


import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist

fig = plt.figure(figsize=(6,6))

ax = axisartist.Subplot(fig, 111)
fig.add_axes(ax)
ax.axis[:].set_visible(False)
ax.axis["x"] = ax.new_floating_axis(0,0)
ax.axis["x"].set_axisline_style("->", size = 1.0)
ax.axis["y"] = ax.new_floating_axis(1,0)
ax.axis["y"].set_axisline_style("-|>", size = 1.0)
ax.axis["x"].set_axis_direction("top")
ax.axis["y"].set_axis_direction("right")

n = np.arange(-10,11,1)
y = n*0*(n<0)+n*1*(n>=0)

plt.xticks([])
plt.yticks([])

plt.text(0.2,-0.3,'O',fontdict={'size':16})
plt.text(0,60,'r[n]',fontdict={'size':16})
plt.text(10,-0.05,r'$\omega_0$',fontdict={'size':16})

plt.scatter(n,np.fft.fft(y),color='black')
plt.show()

信号与系统/单位离散冲激串序列傅里叶变换.py

单位离散冲激串序列傅里叶变换.py


import subprocess
from pathlib import Path

root_folder = Path('helpers/asllvd_data')

for folder in root_folder.glob('*'):
    if not folder.is_dir():
        continue

    for mov_path in folder.glob('*.mov'):
        print(mov_path)
        output_file_path = str(mov_path)[:-4] + '.mp4'
        subprocess.call(['ffmpeg', '-ss', '1', '-i', str(mov_path), '-c:v', 'libx264', '-r', '30', output_file_path])

        mov_path.unlink()


import pandas as pd
import tensorflow as tf
import argparse

# CSV_COLUMN_NAMES = ['id', 'uid', 'gzh', 'xh', 'gz', 'middle_time', 'avg_time', 'ios', 'android', 'wifi', 'nonwifi', 'game0', 'game1', 'game2', 'game3', 'game4', 'type', 'dates']
CSV_COLUMN_NAMES = ['id', 'uid', 'gzh', 'xh', 'gz', 'deposit', 'time0', 'time1', 'time2', 'time3', 'emoney', 'ios',
                    'wifi', 'game0', 'game1', 'game2', 'game3', 'game4', 'pf', 'type']
USER_TYPE = ['NEW', 'ALT']

# def load_data(y_name='type', dropped=['id', 'uid', 'gzh', 'xh', 'gz', 'dates', 'ios', 'android', 'wifi', 'nonwifi', 'game0', 'game1', 'game2', 'game3', 'game4']):
def load_data(y_name='type', dropped=['id', 'uid']):
    data_dir = "model_data/bob/"

    """Returns the iris dataset as (train_x, train_y), (test_x, test_y)."""
    csv_path = data_dir + "wl_model_data.csv"
    dataall = pd.read_csv(csv_path, names=CSV_COLUMN_NAMES, header=0)

    # dataallNew = dataall[dataall['type'] == 1].iloc[:100000, :]
    # dataall = dataall[dataall['type'] == 0]
    # dataall = pd.concat([dataall, dataallNew])

    dataall = dataall.sample(frac=1)

    y_all = dataall.pop(y_name)
    print(y_all.shape)
    x_all = dataall.drop(dropped, axis=1, inplace=False)
    print(x_all.shape)

    train_x = x_all.iloc[:130000, :]
    train_y = y_all.iloc[:130000]

    test_x = x_all.iloc[130000:160000, :]
    test_y = y_all.iloc[130000:160000]
    print(test_y[test_y == 1].count())
    print(test_y[test_y == 0].count())

    print(test_x)

    return (train_x, train_y), (test_x, test_y)

def train_input_fn(features, labels, batch_size):
    """An input function for training"""
    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

    # Shuffle, repeat, and batch the examples.
    dataset = dataset.shuffle(1000).repeat().batch(batch_size)
    # Return the dataset.
    return dataset

def eval_input_fn(features, labels, batch_size):
    """An input function for evaluation or prediction"""
    features = dict(features)
    if labels is None:
        # No labels, use only features.
        inputs = features
    else:
        inputs = (features, labels)

    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices(inputs)

    # Batch the examples
    assert batch_size is not None, "batch_size must not be None"
    dataset = dataset.batch(batch_size)

    # Return the dataset.
    return dataset

parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=100, type=int, help='batch size')
parser.add_argument('--train_steps', default=100000, type=int, help='number of training steps')

def main(argv):
    args = parser.parse_args(argv[1:])

    # Fetch the data
    (train_x, train_y), (test_x, test_y) = load_data()

    # Feature columns describe how to use the input.
    my_feature_columns = []
    for key in train_x.keys():
        my_feature_columns.append(tf.feature_column.numeric_column(key=key))

    # Build 2 hidden layer DNN with 10, 10 units respectively.
    classifier = tf.estimator.DNNClassifier(feature_columns=my_feature_columns,
                                            hidden_units=[500, 500],
                                            n_classes=2,
                                            activation_fn=tf.nn.relu,
                                            model_dir="/tmp/bobInvalidAccPredict/",
                                            optimizer=tf.train.ProximalAdagradOptimizer(learning_rate=0.001, l2_regularization_strength=0.0))

    # Train the Model.
    classifier.train(input_fn=lambda: train_input_fn(train_x, train_y, args.batch_size), steps=args.train_steps)
    # classifier.train(input_fn=lambda: train_input_fn(train_x, train_y, args.batch_size))

    # Evaluate the model.
    eval_result = classifier.evaluate(input_fn=lambda: eval_input_fn(train_x, train_y, args.batch_size))

    print(eval_result)
    print('\nTrain set accuracy: {accuracy:0.3f}\n'.format(**eval_result))

    # Evaluate the model.
    eval_result = classifier.evaluate(input_fn=lambda: eval_input_fn(test_x, test_y, args.batch_size))

    print(eval_result)
    print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))

    predictions = classifier.predict(input_fn=lambda: eval_input_fn(test_x, labels=None, batch_size=args.batch_size))

    res = {"0-0": 0, "0-1": 0, "1-0": 0, "1-1": 0}
    for pred_dict, expect, index in zip(predictions, test_y, range(0, 10000)):
        # 预测是小号，但是实际不是
        # if pred_dict['class_ids'][0] == 0 and expect == 0 :
            # print(test_x.iloc[index:index+1, :])
        # 预测不是小号，但是实际是
        # if pred_dict['class_ids'][0] == 1 and expect == 1 :
        #     print(test_x.iloc[index:index+1, :])

        res[str(pred_dict['class_ids'][0]) + "-" + str(expect)] += 1

    print(res)

    # # Generate predictions from the model
    # expected = ['NEW', 'NEW', 'NEW', 'NEW']
    # predict_x = {
    #     'gzh': [0, 0, 1, 0],
    #     'xh': [0, 0, 0, 0],
    #     'gz': [0, 0, 0, 0],
    #     'ios': [0, 0, 0, 0],
    #     'wifi': [0, 0, 1, 1],
    #     'game0': [0, 0, 11, 2],
    #     'game1': [0, 0, 0, 0],
    #     'game2': [0, 0, 0, 0],
    #     'game3': [0, 0, 0, 0],
    #     'game4': [0, 0, 0, 0]
    # }
    #
    # predictions = classifier.predict(input_fn=lambda:eval_input_fn(predict_x, labels=None, batch_size=args.batch_size))
    #
    # template = '\nPrediction is "{}" ({:.1f}%), expected "{}"'
    #
    # for pred_dict, expec in zip(predictions, expected):
    #     class_id = pred_dict['class_ids'][0]
    #     probability = pred_dict['probabilities'][class_id]
    #
    #     print(template.format(USER_TYPE[class_id], 100 * probability, expec))

if __name__ == '__main__':
    tf.logging.set_verbosity(tf.logging.INFO)
    tf.app.run(main)


# pyplugin - A python plugin finder
# Copyright (C) 2013 Gary Kramlich <grim@reaperworld.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
This module contains the setuptools bootstrap for packaging
"""

from setuptools import setup

from pyscovery import __version__

DESC = """
A Python plugin loader that will search for classes based from a list of
modules and optionally use the file system to recurse into packages.
"""

def main():
    """ Main function to create our package """

    setup(
        name='pyscovery',
        version=__version__,
        description=DESC,
        py_modules=['pyscovery'],
        zip_safe=True,
        test_suite='tests',
        author='Gary Kramlich',
        author_email='grim@reaperworld.com',
        url='http://www.bitbucket.org/rw_grim/pyscovery',
    )

if __name__ == '__main__':
    main()


import airflowlib.emr_lib as emr
import os

from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.operators.postgres_operator import PostgresOperator
from airflow.contrib.hooks.aws_hook import AwsHook
from airflow.hooks.postgres_hook import PostgresHook
from datetime import datetime, timedelta
import sql_queries
default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2020, 4, 9),
    'retries': 0,
    'retry_delay': timedelta(minutes=2),
    'provide_context': True
}

# Initialize the DAG
# Concurrency --> Number of tasks allowed to run concurrently
dag = DAG('human_activity', concurrency=1, schedule_interval=None, default_args=default_args)
region = emr.get_region()
emr.client(region_name=region)

# Creates an EMR cluster
def create_emr(**kwargs):
    cluster_id = emr.create_cluster(region_name=region, cluster_name='udacity_cluster', num_core_nodes=2)
    return cluster_id

# Waits for the EMR cluster to be ready to accept jobs
def wait_for_completion(**kwargs):
    ti = kwargs['ti']
    cluster_id = ti.xcom_pull(task_ids='create_cluster')
    emr.wait_for_cluster_creation(cluster_id)

# Terminates the EMR cluster
def terminate_emr(**kwargs):
    ti = kwargs['ti']
    cluster_id = ti.xcom_pull(task_ids='create_cluster')
    emr.terminate_cluster(cluster_id)

# Converts each of the movielens datafile to parquet
def transform_humanactivity_to_parquet(**kwargs):
    # ti is the Task Instance
    ti = kwargs['ti']
    cluster_id = ti.xcom_pull(task_ids='create_cluster')
    cluster_dns = emr.get_cluster_dns(cluster_id)
    headers = emr.create_spark_session(cluster_dns, 'pyspark')
    session_url = emr.wait_for_idle_session(cluster_dns, headers)
    statement_response = emr.submit_statement(session_url,
                                              '/root/airflow/dags/transform/humanactivity.py')
    emr.track_statement_progress(cluster_dns, statement_response.headers)
    emr.kill_spark_session(session_url)

def load_data_to_redshift(*args, **kwargs):
    tables = sql_queries.tables
    source_path = "s3://s3emrdendudacity/humanactivity/csv/"
    iam = "arn:aws:iam::811800901732:role/myRedshiftRole"
    redshift_hook = PostgresHook("redshift")
    for table in tables:
        src = source_path + table + '.csv'
        redshift_hook.run("""COPY public.{}
            FROM '{}'
            IAM_ROLE '{}'
            CSV;
            """.format(table, src, iam))

# Define the individual tasks using Python Operators
create_cluster = PythonOperator(
    task_id='create_cluster',
    python_callable=create_emr,
    dag=dag)

wait_for_cluster_completion = PythonOperator(
    task_id='wait_for_cluster_completion',
    python_callable=wait_for_completion,
    dag=dag)

transform_humanactivity = PythonOperator(
    task_id='transform_humanactivity',
    python_callable=transform_humanactivity_to_parquet,
    dag=dag)

terminate_cluster = PythonOperator(
    task_id='terminate_cluster',
    python_callable=terminate_emr,
    trigger_rule='all_done',
    dag=dag)

create_people_table = PostgresOperator(
    task_id="create_people_table",
    dag=dag,
    postgres_conn_id="redshift",
    sql=sql_queries.create_tables['people']
)
create_sources_table = PostgresOperator(
    task_id="create_sources_table",
    dag=dag,
    postgres_conn_id="redshift",
    sql=sql_queries.create_tables['sources']
)
create_collectibles_table = PostgresOperator(
    task_id="create_collectibles_table",
    dag=dag,
    postgres_conn_id="redshift",
    sql=sql_queries.create_tables['collectibles']
)
create_time_table = PostgresOperator(
    task_id="create_time_table",
    dag=dag,
    postgres_conn_id="redshift",
    sql=sql_queries.create_tables['time']
)
create_activities_table = PostgresOperator(
    task_id="create_activities_table",
    dag=dag,
    postgres_conn_id="redshift",
    sql=sql_queries.create_tables['activities']
)

load_tables_from_s3 = PythonOperator(
    task_id='load_tables',
    python_callable=load_data_to_redshift,
    dag=dag
)

# construct the DAG by setting the dependencies
create_cluster >> wait_for_cluster_completion >> transform_humanactivity >> terminate_cluster
terminate_cluster >> create_people_table >> load_tables_from_s3
terminate_cluster >> create_sources_table >> load_tables_from_s3
terminate_cluster >> create_collectibles_table >> load_tables_from_s3
terminate_cluster >> create_time_table >> load_tables_from_s3
terminate_cluster >> create_activities_table >> load_tables_from_s3


#母牛的故事
import time

list = [0]*55
list[0] = 1
list[1] = 2
list[2] = 3
list[3] = 4

while(True):
    Year = eval(input())
    if(Year<=0):
        break
    else:
        if(Year<=4):
            CowNum = Year
            print(CowNum)
        else:
            for i in range(4,Year,1):
                start = time.perf_counter()
                list[i] = list[i-1]+list[i-3]

            end = time.perf_counter()
            print(end-start)

            print(list[Year-1])


import datetime
import logging
import os
import re

import pandas as pd
import numpy as np
from Bio import SeqIO
from Bio.Alphabet import generic_dna
from Bio.Seq import Seq
from pastml import numeric2datetime, datetime2numeric

EXTERNAL = 'External'

MEDIUM = 'Medium'

LOW = 'Low'

HIGH = 'High'

NAME = 'sequencename'
DATE = 'sampledate'
LOCATION = 'urbanrural'
PREVALENCE = 'prevalence'
HLE = 'highlow_prevalence'
HLME = 'highmedlow_prevalence'

def format_id(id):
    dot_year = '\.\d\d\d\d$'
    underscore_date = '_\d\d\d\d-\d\d-\d\d$'
    date = re.findall(dot_year, id)
    if date:
        id = re.sub(dot_year, '', id)
        date = int(date[0][1:])
    else:
        date = re.findall(underscore_date, id)
        if date:
            date = datetime.datetime.strptime(date[0][1:], '%Y-%m-%d')
            id = re.sub(underscore_date, '', id)
        else:
            date = None
    return id.replace('.', '_'), date

if '__main__' == __name__:
    logging.basicConfig(level=logging.INFO, format='%(asctime)s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S",
                        filename=None)

    import argparse

    parser = argparse.ArgumentParser(description="Processes data files.")

    parser.add_argument('--data_in', required=True, type=str, help="the annotation file.")
    parser.add_argument('--sequences_in', required=True, type=str, help="the fasta file.")
    parser.add_argument('--sequences_out', required=True, type=str, help="the out fasta file.")
    parser.add_argument('--to_remove', required=True, type=str, help="the sequences to skip.")
    parser.add_argument('--data_out', required=True, type=str, help="the sequence annotation file.")
    parser.add_argument('--dates', required=True, type=str, help="the date annotation file.")
    params = parser.parse_args()

    # Read and fix metadata
    df = pd.io.stata.read_stata(params.data_in)
    df.index = df['id'].apply(lambda _: format_id(_)[0])
    df.drop(labels=['id'], axis=1, inplace=True)
    df[DATE] = pd.to_datetime(df[DATE], yearfirst=False, format="%d/%m/%Y")
    for cat in (HLE, HLME):
        df[cat].replace('LastVisit', EXTERNAL, inplace=True)
    df[HLME] = df[HLME].apply(lambda _: '' if _ not in {HIGH, LOW, MEDIUM, EXTERNAL} else _)
    df[HLE] = df[HLE].apply(lambda _: '' if _ not in {HIGH, LOW, EXTERNAL} else _)
    df[LOCATION].replace('.', '', inplace=True)

    df[DATE] = pd.to_datetime(df[DATE], dayfirst=True, yearfirst=False, infer_datetime_format=True)
    logging.info(df.head())

    to_remove = set()
    if params.to_remove:
        with open(params.to_remove, 'r') as f:
            to_remove = set(f.read().strip().strip('\n').split('\n'))

    ids = []
    with open(params.sequences_out, 'w+') as f:
        for rec in SeqIO.parse(params.sequences_in, 'fasta', alphabet=generic_dna):
            rec.id, date = format_id(rec.id)
            if rec.id in to_remove:
                continue
            if rec.id not in df.index:
                df.loc[rec.id, [DATE, HLE, HLME]] = date, EXTERNAL, EXTERNAL
            ids.append(rec.id)
            f.write('>{}\n{}\n'.format(rec.id, str(rec.seq).replace('_', '-')))

    df = df.loc[ids, :]
    df['lsdate'] = df[DATE].apply(lambda _: datetime2numeric(_) if isinstance(_, datetime.datetime)
        else ('b({},{})'.format(_, _ + 1) if isinstance(_, int) else 'u(2016)'))
    with open(params.dates, 'w+') as f:
        f.write('{}\n'.format(len(df)))
    df['lsdate'].to_csv(params.dates, sep='\t', header=False, mode='a')
    df[DATE] = df[DATE].apply(lambda _: _.strftime('%Y-%m-%d') if isinstance(_, datetime.datetime) else _)
    df.to_csv(params.data_out, sep='\t', index_label='id')


# -*- coding: utf-8 -*-
from socket import *

#Cria o nome do Host
meuHost = ''

#utiliza este numero de porta
minhaPort = 50007

'''Cria um obj socket, os parametros referem-se a;
Familia do endereço padrao (socket.AF_INET)
Se é Stream (socket.SOCK_STREAM, o padrão) ou datagram(Socket.SOCK_DGRAM)
E o protocolo padrao é 0
dessa maneira:
AF_INET -- protocolo de endereço IP
Sock_STREAM -- protocolo de transferencia TCP
Combinando os dois temos o Server TCP/IP
'''
sockobj = socket(AF_INET,SOCK_STREAM)

#Vincula o servidor ao numero da porta
sockobj.bind((meuHost,minhaPort))

#o server começa a esperar por clientes limitado a 5 conexoes por vez
sockobj.listen(5)

while True:
    #aceita uma conexao quando encontrada e devolve
    #um novo socket conexao e o endereço do cliente conectado
    conexao,endereco = sockobj.accept()
    print("Server conectado por",endereco)

    while True:
        #recebe data enviada pelo cliente
        data = conexao.recv(1024)
        #time.sleep(3)

        #se não receber nada aramos o loop
        if not data: break

        #O servidor manda e volta uma resposta
        conexao.send(b'Eco=>' + data)

    #fecha a conexao criada depois de responder o cliente
    conexao.close()


import os

import numpy as np
import cv2 as cv

haar_cascade = cv.CascadeClassifier('face_detection.xml')

people = []
DIR = 'Resources/val'
for person in os.listdir(DIR):
    people.append(person)
face_recognizer = cv.face.LBPHFaceRecognizer_create()
face_recognizer.read('traindata.yml')

img = cv.imread('Resources/val/elton_john/3.jpg')

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('gray', gray)
face_detected = haar_cascade.detectMultiScale(gray, 1.1, 1)

for (x,y,w,h) in face_detected:
    face_cropped = gray[y:y+h, x:x+h]
    cv.imshow('cropped', face_cropped)
    cv.waitKey(0)
    label, confidence = face_recognizer.predict(face_cropped)
    print(f'Label = {people[label]} with a confidence of {confidence}')
    cv.putText(img, str(people[label]) + ' ' + str(confidence), (x,y), cv.FONT_HERSHEY_COMPLEX, 1.0, (0,255,0), thickness=2)
    cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)

cv.namedWindow('detect face', cv.WINDOW_NORMAL)
cv.resizeWindow('detect face', img.shape[1], img.shape[0])
#img = cv.resize(img, (1920,1080))
cv.imshow('detect face', img)
cv.waitKey(0)


import requests
import os
import json
from helper import format_filename
from vtt_to_srt import vtt_to_srt
TRANSCRIPTS_PATH = 'https://api.frontendmasters.com/v1/kabuki/transcripts/'
def download_subtitles(couses, self):
    if couses is None:
        return
    if not couses.has_key('slug') :
        return
    title = couses['slug']
    download_path = self.create_download_directory()
    course_path = self.create_course_directory(download_path, title)
    data_json = os.path.join(course_path, 'index.json')
    with open(data_json, 'wb') as out_file:
        json.dump(couses, out_file)
    for i, lesson_data in enumerate(couses['lessonData']):
        lesson_title = lesson_data['title']
        stats_id = lesson_data['statsId']
        if stats_id is None:
            continue
        path = os.path.join(course_path, str(i) + "-" + format_filename(lesson_title) + ".vtt")
        if not os.path.isfile(path) or os.path.getsize(path) == 0:
            print("Downloading subtitle of {0}/{1}".format(format_filename(lesson_title), path))
            lesson_transcript_api = TRANSCRIPTS_PATH + stats_id + ".vtt"
            data = requests.get(lesson_transcript_api)
            if data is None:
                continue
            with open(path, 'wb') as local_file:
                local_file.writelines(data.iter_lines())
    vtt_to_srt(course_path)


# -*- coding: utf-8 -*-

from django.http import HttpResponse
import datetime
from jinja2 import Environment, PackageLoader
from django.template.loader import get_template
from django.template import Context
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

from forum.models import *

def loadTpl(tpl_name, param):
    env = Environment(loader=PackageLoader('SimBBS', 'templates'))
    try:
        template = env.get_template(tpl_name)
        return HttpResponse( template.render( param ) )
    except:
        return HttpResponse('Template not found')

def index(request, error=False):
    list_of_topics = Topic.objects.all().order_by('-create_ts')
    topics = []
    for topic in list_of_topics:
        q_topic = Topic.objects.get(id=topic.id)
        reply_count = Reply.objects.filter(topic=q_topic).count()
        n_topic = {'id':q_topic.id, 'title':topic.title, 'reply_count':reply_count}
        topics.append(n_topic)
    return loadTpl('index.html', locals() )

def topic(request, topic_id):
    topic_id = int(topic_id)
    topic = Topic.objects.get(id=topic_id)
    if request.method == 'POST':
        cur_topic = Topic.objects.get(id=request.POST['topic_id'])
        reply = Reply.objects.create(topic=cur_topic, content=request.POST['reply_text'])
        try:
            reply.save()
            post = True
        except:
            post = False
    replys = Reply.objects.filter(topic=topic).order_by("create_ts")
    return loadTpl('topic.html', locals() )

def create_topic(request):
    if request.method == 'POST':
        #title = request.POST['title']
        #content = request.POST['content']
        #topic = Topic.objects.create(title=title.encode('utf-8'), content=content.encode('utf-8'))
        topic = Topic.objects.create(title=request.POST['title'], content=request.POST['content'])
        try:
            topic.save()
            create = True
        except:
            create = False
    return loadTpl('create_topic.html', locals() )

def topic_delete(request, topic_id):
    try:
        topic = Topic.objects.get(id=topic_id)
        Topic.objects.filter(id=topic.id).delete()
        return HttpResponseRedirect('/')
    except:
        return HttpResponseRedirect('/error/deletenotexist')

def blueprint(request):
    return loadTpl('blueprint.html', locals() )


import requests
from  bs4 import BeautifulSoup
import re
from requests.exceptions import ReadTimeout,HTTPError,RequestException
import jieba

try:
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
    proxies={'http':'http://60.211.166.42:63000',
             'https':'https://60.211.166.42:63000',}
    response=requests.get('https://baike.baidu.com/',headers=headers)
    print(response.status_code)
except ReadTimeout:
    print('超时访问')
except HTTPError:
    print('网址错误')
except RequestExceptin:
    print('错误')
soup=BeautifulSoup(response.content,'lxml')
#获取百科地址
links=soup.find_all('a', href=re.compile(r"/item/(.*)"))
pat='<a href="https://baike.baidu.com/item(.*?)" '
new_urls=re.compile(pat).findall(str(links))
try:
    for i in range(0,len(new_urls)):
        response=requests.get('https://baike.baidu.com/item'+new_urls[i],headers=headers)
        soup=BeautifulSoup(response.content,'lxml')
        links=soup.find_all('a',href=re.compile(r'/item/(.*?)'))
        pat='href="/item(.*?)"'
        links=re.compile(pat).findall(str(links))
        for link in links:
            if len(link)>0 and link!='\r\n':
                new_urls.append(link)
        fh=open('E:/测试/阿修罗.txt','w',encoding='utf-8')
        fh.write(str(new_urls))
        fh.close()
    for i in range(0,len(new_urls)):
        print('第'+str(i)+'个任务开始')
        u=new_urls[i]
        url='https://baike.baidu.com/item'+u
        hj=requests.get(url,headers=headers)
        soup=BeautifulSoup(hj.content,'lxml')
        title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title").find("h1")
        name=title_node.get_text()
        #summary_node=soup.find('div',class_='lemma-summary')
        #content=summary_node.get_text()
        neirong=soup.find_all('div',class_='para')
        #for j in range(len(new_urls)):
        fh=open('E:/测试/百度百科/'+name+'.txt','w',encoding='utf-8')
        for z in range(0,len(neirong)):
            fh.write(neirong[z].get_text())
        fh.close()
        print(name+'爬取成功')
except Exception as e:
    print(e)

'''
title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title").find("h1")
print(title_node.get_text())
'''
'''
summary_node = soup.find('div', class_="lemma-summary")
print(summary_node.get_text())
for i in soup.find_all('div',class_='para'):
    print(i.string)
'''
#print(soup.get_text())

爬虫项目/挑选百科主要内容.py


from enum import IntEnum
from typing import Dict

class Book(IntEnum):
    GENESIS = 1
    EXODUS = 2
    LEVITICUS = 3
    NUMBERS = 4
    DEUTERONOMY = 5
    JOSHUA = 6
    JUDGES = 7
    RUTH = 8
    SAMUEL_1 = 9
    SAMUEL_2 = 10
    KINGS_1 = 11
    KINGS_2 = 12
    CHRONICLES_1 = 13
    CHRONICLES_2 = 14
    EZRA = 15
    NEHEMIAH = 16
    ESTHER = 17
    JOB = 18
    PSALMS = 19
    PROVERBS = 20
    ECCLESIASTES = 21
    SONG_OF_SONGS = 22
    ISAIAH = 23
    JEREMIAH = 24
    LAMENTATIONS = 25
    EZEKIEL = 26
    DANIEL = 27
    HOSEA = 28
    JOEL = 29
    AMOS = 30
    OBADIAH = 31
    JONAH = 32
    MICAH = 33
    NAHUM = 34
    HABAKKUK = 35
    ZEPHANIAH = 36
    HAGGAI = 37
    ZECHARIAH = 38
    MALACHI = 39
    MATTHEW = 40
    MARK = 41
    LUKE = 42
    JOHN = 43
    ACTS = 44
    ROMANS = 45
    CORINTHIANS_1 = 46
    CORINTHIANS_2 = 47
    GALATIANS = 48
    EPHESIANS = 49
    PHILIPPIANS = 50
    COLOSSIANS = 51
    THESSALONIANS_1 = 52
    THESSALONIANS_2 = 53
    TIMOTHY_1 = 54
    TIMOTHY_2 = 55
    TITUS = 56
    PHILEMON = 57
    HEBREWS = 58
    JAMES = 59
    PETER_1 = 60
    PETER_2 = 61
    JOHN_1 = 62
    JOHN_2 = 63
    JOHN_3 = 64
    JUDE = 65
    REVELATION = 66
    ESDRAS_1 = 67
    TOBIT = 68
    WISDOM_OF_SOLOMON = 69
    ECCLESIASTICUS = 70
    MACCABEES_1 = 71
    MACCABEES_2 = 72

    @property
    def title(self) -> str:
        return _BOOK_TITLES.get(self, "")

_BOOK_TITLES: Dict[Book, str] = {
    Book.GENESIS: "Genesis",
    Book.EXODUS: "Exodus",
    Book.LEVITICUS: "Leviticus",
    Book.NUMBERS: "Numbers",
    Book.DEUTERONOMY: "Deuteronomy",
    Book.JOSHUA: "Joshua",
    Book.JUDGES: "Judges",
    Book.RUTH: "Ruth",
    Book.SAMUEL_1: "1 Samuel",
    Book.SAMUEL_2: "2 Samuel",
    Book.KINGS_1: "1 Kings",
    Book.KINGS_2: "2 Kings",
    Book.CHRONICLES_1: "1 Chronicles",
    Book.CHRONICLES_2: "2 Chronicles",
    Book.EZRA: "Ezra",
    Book.NEHEMIAH: "Nehemiah",
    Book.ESTHER: "Esther",
    Book.JOB: "Job",
    Book.PSALMS: "Psalms",
    Book.PROVERBS: "Proverbs",
    Book.ECCLESIASTES: "Ecclesiastes",
    Book.SONG_OF_SONGS: "Song of Songs",
    Book.ISAIAH: "Isaiah",
    Book.JEREMIAH: "Jeremiah",
    Book.LAMENTATIONS: "Lamentations",
    Book.EZEKIEL: "Ezekiel",
    Book.DANIEL: "Daniel",
    Book.HOSEA: "Hosea",
    Book.JOEL: "Joel",
    Book.AMOS: "Amos",
    Book.OBADIAH: "Obadiah",
    Book.JONAH: "Jonah",
    Book.MICAH: "Micah",
    Book.NAHUM: "Nahum",
    Book.HABAKKUK: "Habakkuk",
    Book.ZEPHANIAH: "Zephaniah",
    Book.HAGGAI: "Haggai",
    Book.ZECHARIAH: "Zechariah",
    Book.MALACHI: "Malachi",
    Book.MATTHEW: "Matthew",
    Book.MARK: "Mark",
    Book.LUKE: "Luke",
    Book.JOHN: "John",
    Book.ACTS: "Acts",
    Book.ROMANS: "Romans",
    Book.CORINTHIANS_1: "1 Corinthians",
    Book.CORINTHIANS_2: "2 Corinthians",
    Book.GALATIANS: "Galatians",
    Book.EPHESIANS: "Ephesians",
    Book.PHILIPPIANS: "Philippians",
    Book.COLOSSIANS: "Colossians",
    Book.THESSALONIANS_1: "1 Thessalonians",
    Book.THESSALONIANS_2: "2 Thessalonians",
    Book.TIMOTHY_1: "1 Timothy",
    Book.TIMOTHY_2: "2 Timothy",
    Book.TITUS: "Titus",
    Book.PHILEMON: "Philemon",
    Book.HEBREWS: "Hebrews",
    Book.JAMES: "James",
    Book.PETER_1: "1 Peter",
    Book.PETER_2: "2 Peter",
    Book.JOHN_1: "1 John",
    Book.JOHN_2: "2 John",
    Book.JOHN_3: "3 John",
    Book.JUDE: "Jude",
    Book.REVELATION: "Revelation",
    Book.ESDRAS_1: "1 Esdras",
    Book.TOBIT: "Tobit",
    Book.WISDOM_OF_SOLOMON: "Wisdom of Solomon",
    Book.ECCLESIASTICUS: "Ecclesiasticus",
    Book.MACCABEES_1: "1 Maccabees",
    Book.MACCABEES_2: "2 Maccabees",
}


"""
Cada funcion recibe la Carteta donde
empezara a buscar y el archivo a buscar

La funcion retorna True si encuentra
el archivo, e imprime la ruta

en caso contrario retorna falso

def primeroProfundidad(Carpeta,Archivo):
	pass
def primeroAnchura(Carpeta,Archivo):
	pass
"""
# Integrantes:
# Hidalgo Hernandez Juan Daniel
# Trejo Martinez Alexis Armando

import json
with open('base3.json') as file:
	data = json.load(file)
camino=[]

def busqueda(cuspide,valorbusqueda):

	if cuspide == valorbusqueda:
		return valorbusqueda

	for v in data:
		if v[0] == cuspide:
			camino.append(cuspide)
			print(v[0])
			resultado=busqueda(v[1],valorbusqueda)
			if resultado:
				return resultado
	camino.pop()

resultado=busqueda("C:","MemoriaRam.exe")
if resultado:
	print ("Archivo encontrado")
	print(camino)
else:
	print("no encontrado")


# -*- coding: utf-8 -*-

"""Tests for et_dot package."""

import random
import math

import pytest

import et_dot

def test_dot_aa():
    a = [1, 2, 3]
    expected = 14
    result = et_dot.dot(a, a)
    assert result == expected

def test_dot_commutative():
    # create two arrays of length 10 with random float numbers:
    a = []
    b = []
    for _ in range(10):
        a.append(random.random())
        b.append(random.random())
    # do the test
    ab = et_dot.dot(a,b)
    ba = et_dot.dot(b,a)
    assert ab==ba

def test_dot_commutative_2():
    # Fix the seed for the random number generator of module random.
    random.seed(0)
    # choose array size
    n = 10
    # create two arrays of length n with with zeros:
    a = n * [0]
    b = n * [0]
    # repetion loop:
    for r in range(1000):
        # fill a and b with random float numbers:
        for i in range(n):
           a[i] = random.random()
           b[i] = random.random()
        # do the test
        ab = et_dot.dot(a,b)
        ba = et_dot.dot(b,a)
        assert ab==ba

def test_dot_zero():
    # Fix the seed for the random number generator of module random.
    random.seed(0)
    # choose array size
    n = 10
    # create two arrays of length n with with zeros:
    a = n * [0]
    zero = n * [0]
    # repetion loop (the underscore is a placeholder for a variable dat we do not use):
    for _ in range(1000):
        # fill a with random float numbers:
        for i in range(n):
            a[i] = random.random()
        # do the test
        azero = et_dot.dot(a, zero)
        assert azero == 0

def test_dot_one():
    # Fix the seed for the random number generator of module random.
    random.seed(0)
    # choose array size
    n = 10
    # create two arrays of length n with with zeros:
    a = n * [0]
    one = n * [1.0]
    # repetion loop (the underscore is a placeholder for a variable dat we do not use):
    for _ in range(1000):
        # fill a with random float numbers:
        for i in range(n):
            a[i] = random.random()
        # do the test
        aone = et_dot.dot(a, one)
        expected = sum(a)
        assert aone == expected

def test_dot_one_2():
    a1 = 1.0e16
    a = [a1, 1.0, -a1]
    one = [1.0, 1.0, 1.0]
    expected = 1.0
    result = et_dot.dot(a, one)
    assert math.isclose(result, expected, abs_tol=10.0)

def test_dot_unequal_length():
    a = [1, 2]
    b = [1, 2, 3]
    with pytest.raises(ArithmeticError):
        et_dot.dot(a, b)

# ==============================================================================
# The code below is for debugging a particular test.
# (otherwise all tests are normally run with pytest).
# Make sure that you run this code with the project directory as CWD, and
# that the source directory is on the path
# ==============================================================================
if __name__ == "__main__":
    the_test_you_want_to_debug = test_dot_aa

    print("__main__ running", the_test_you_want_to_debug)
    the_test_you_want_to_debug()
    print('-*# finished #*-')

# eof


import cv2
import numpy as np
import matplotlib.pyplot as plt

def img_process(fname, mode='RGB'):
    img = cv2.cvtColor(cv2.imread(fname), cv2.COLOR_BGR2RGB)
    ### read img and plot ###
    img_inten_pyr = get_gray(img)

    if mode=='RGB':
        img_r_pyr, img_g_pyr, img_b_pyr = get_RGB(img)
        img_show(new_img, fname)
        img_r_pyr= apply_dog_to_pyr(img_r_pyr)
        img_g_pyr= apply_dog_to_pyr(img_g_pyr)
        img_b_pyr= apply_dog_to_pyr(img_b_pyr)
        img_color_pyr = zip(img_r_pyr, img_g_pyr, img_b_pyr)
    elif mode == 'RGBY':
        img_r_pyr, img_g_pyr, img_b_pyr, img_y_pyr = get_RGBY(img, img_inten_pyr)
        rg_cells, by_cells = get_cell(img_r_pyr, img_g_pyr, img_b_pyr, img_y_pyr)
        rg_cells = apply_dog_to_pyr(rg_cells)
        by_cells = apply_dog_to_pyr(by_cells)

    img_inten_pyr = central_surround(img_inten_pyr, img_inten_pyr)
    img_inten_pyr = apply_dog_to_pyr(img_inten_pyr)
    if mode is 'gray' :
        img_show(img_inten_pyr, fname, 'gray')
    elif mode is 'RGB':
        img_show(img_color_pyr, fname, 'RGB')
    elif mode is 'RGBY':
        cell_show(rg_cells, by_cells, img_inten_pyr, fname)

def cell_show(rg_cells, by_cells, img_inten_pyr, fname):
    n = 0
    for rg_cell in rg_cells:
        n +=1
        plt.subplot(3,6,n)
        plt.imshow(rg_cell, cmap='gray')
        plt.axis('off')
        plt.title('rg_'+str(n), fontsize=20)
    for by_cell in by_cells:
        n +=1
        plt.subplot(3,6,n)
        plt.imshow(by_cell, cmap='gray')
        plt.axis('off')
        plt.title('by_'+str(n-6), fontsize=20)
    for img_inten in img_inten_pyr:
        n +=1
        plt.subplot(3,6,n)
        plt.imshow(img_inten, cmap='gray')
        plt.axis('off')
        plt.title('inten_'+str(n-12), fontsize=20)
    plt.show()

def img_show(img_list, fname, mode = 'gray'):
    n = 1
    for new_img in img_list:
        if mode is "RGB":
            new_img = cv2.merge(tuple(new_img))
            plt.subplot(330+n)
            plt.imshow(new_img)
        elif mode is 'gray':
            plt.subplot(330+n)
            plt.imshow(new_img, cmap='gray')
        plt.axis('on')
        plt.title('pyr_'+str(n), fontsize=20)
        n +=1
    plt.subplots_adjust(wspace = 0.5, hspace= 0.5)
    plt.savefig("new_"+mode+'_'+fname)
    plt.show()

def central_surround(central, surround):
    new_img_list = []
    for i in range(2,5):
        img_sub_3 = surround[i+3]
        for j in range(3):
            img_sub_3 = cv2.pyrUp(img_sub_3)
        new_img_list.append(central[i] - img_sub_3)
        img_sub_4 = surround[i+4]
        for j in range(4):
            img_sub_4 = cv2.pyrUp(img_sub_4)
        new_img_list.append(central[i] - img_sub_4)
    return new_img_list

def apply_dog_to_pyr(img_inten_list):
    for inten in range(len(img_inten_list)):
        img_inten_list[inten] = process(img_inten_list[inten])
    return img_inten_list

def get_gray(img):
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    img_inten_pyr = get_pyr(img)
    return img_inten_pyr

def get_RGB(img):
    img_pyr = get_pyr(img)
    img_r_pyr = []
    img_g_pyr = []
    img_b_pyr = []
    for img in img_pyr:
        r, g, b = cv2.split(img)
        img_r_pyr.append(r)
        img_g_pyr.append(g)
        img_b_pyr.append(b)
    return np.asarray(img_r_pyr), np.asarray(img_g_pyr), np.asarray(img_b_pyr)

def get_pyr(img):
    pyr = []
    pyr.append(img)
    for i in range(8):
        pyr.append(cv2.pyrDown(pyr[i]))
    return pyr

def get_RGBY(img, img_inten_pyr):
    img_r_pyr, img_g_pyr, img_b_pyr = get_RGB(img)
    new_img_r_pyr = (img_r_pyr - (img_g_pyr+img_b_pyr)/2) / img_inten_pyr
    new_img_g_pyr = (img_g_pyr - (img_r_pyr+img_b_pyr)/2) / img_inten_pyr
    new_img_b_pyr = (img_b_pyr - (img_r_pyr+img_g_pyr)/2) / img_inten_pyr
    img_y_pyr = (img_r_pyr + img_g_pyr - 2*(np.abs(img_r_pyr-img_g_pyr)+img_b_pyr)) / img_inten_pyr
    return new_img_r_pyr, new_img_g_pyr, new_img_b_pyr, img_y_pyr

def get_cell(img_r_pyr, img_g_pyr, img_b_pyr, img_y_pyr):
    rg_cells = central_surround((img_r_pyr-img_g_pyr), (img_g_pyr-img_r_pyr))
    #by_cells = central_surround((img_b_pyr-img_y_pyr), (img_y_pyr-img_b_pyr))
    by_cells = central_surround_2(img_b_pyr, img_y_pyr)
    return rg_cells, by_cells

def central_surround_2(central, surround):
    img_list = []
    n = 0
    for c in range(0, 3):
        for i in range(0,3):
            new_img = surround[c+i]
            for j in range(i):
                new_img = cv2.pyrUp(new_img)
            new_img = central[c] - new_img
            img_cen_3 = central[c+3]
            for j in range(3):
                img_cen_3 = cv2.pyrUp(img_cen_3)
            img_sub_3 = surround[c+3+i]
            for j in range(3+i):
                img_sub_3 = cv2.pyrUp(img_sub_3)
            img_3 = process(new_img - img_sub_3 + img_cen_3)
            img_list.append(img_3)
            n+=1
            plt.subplot(3,6,n)
            plt.imshow(img_3, cmap='gray')
            plt.axis('off')
            plt.title('img'+str(c)+str(c+i)+str(3), fontsize=20)
            img_cen_4 = central[c+4]
            for j in range(4):
                img_cen_4 = cv2.pyrUp(img_cen_4)
            img_sub_4 = surround[c+4+i]
            for j in range(4+i):
                img_sub_4 = cv2.pyrUp(img_sub_4)
            img_4 = process(new_img - img_sub_4 + img_cen_4)
            img_list.append(img_4)
            n+=1
            plt.subplot(3,6,n)
            plt.imshow(img_4, cmap='gray')
            plt.axis('off')
            plt.title('img'+str(c)+str(c+i)+str(4), fontsize=20)
    plt.show()

def process(img):
    img = np.asarray(img, 'double')
    img_max = img.max()
    img_min = img.min()
    img = (img-img_min)/(img_max- img_min)
    sig_ex = 0.2
    sig_inh = 0.25
    c_ex = 0.2
    c_inh = 0.65
    new_img = neural_processing(img, 12, sig_ex, sig_inh, c_ex, c_inh)
    return new_img

def dog(img, sig_ex, sig_inh, c_ex, c_inh):
    img_ex = cv2.GaussianBlur(img, (5,5), sig_ex)
    img_inh = cv2.GaussianBlur(img, (5,5), sig_inh)
    return ((c_ex * img_ex) - (c_inh *img_inh))

def neural_processing(img, iter, sig_ex, sig_inh, c_ex, c_inh, c=0.02):
    for i in range(iter) :
        dog_img = dog(img, sig_ex, sig_inh, c_ex, c_inh)
        img = img + img*dog_img - c
        img_shape = np.shape(img)
        for row in range(img_shape[0]):
            for col in range(img_shape[1]):
                if img[row,col] < 0:
                    img[row, col] = 0
    return img

def new_blur(fname):
    img = cv2.cvtColor(cv2.imread(fname), cv2.COLOR_BGR2RGB)
    img_r, img_g, img_b = cv2.split(img)
    np.savetxt("img.txt", img_r)
    img_y = (img_r + img_g)/2
    height = np.shape(img_r)[1]
    width = np.shape(img_r)[0]
    n = 0
    for i in range(6,11, 2):
        for j in range(6,11, 2):
            new_img_b = by_cell(img_b, img_y, height, width,i)
            new_img_y = yb_cell(img_b, img_y, height, width, j)
            img_b = img_b + new_img_b
            img_r = img_r + new_img_y
            img_g = img_g + new_img_y
            img_y = img_y + new_img_y
            img = cv2.merge(tuple((img_r, img_g, img_b)))
            n +=1
            plt.subplot(3,6,n)
            plt.title("img_b"+str(i)+", "+str(j), fontsize=10)
            plt.axis("off")
            plt.imshow(img_b, cmap='gray')
            n+=1
            plt.subplot(3,6,n)
            plt.title("imb_y"+str(i)+", "+str(j), fontsize=10)
            plt.imshow(img_y, cmap='gray')
            plt.axis("off")

    plt.show()

def by_cell(img_b, img_y, height, width, n):
    new_img = np.zeros((height, width), dtype=np.float)
    for i in range(1, height-1):
        for j in range(1, width-1):
            new_val = (n*img_b[i][j] - img_y[i-1][j-1] - img_y[i-1][j] - img_y[i-1][j+1]  - img_y[i][j-1]\
            - img_y[i][j+1] - img_y[i+1][j-1] - img_y[i+1][j] - img_y[i+1][j+1])/8
            new_img[i][j] = 0 if new_val<0 else new_val
    return new_img

def yb_cell(img_b, img_y, height, width, n):
    new_img = np.zeros((height, width), dtype=np.float)
    for i in range(1, height-1):
        for j in range(1, width-1):
            new_val = (n*img_y[i][j] - img_b[i-1][j-1] - img_b[i-1][j] - img_b[i-1][j+1]  - img_b[i][j-1]\
            - img_b[i][j+1] - img_b[i+1][j-1] - img_b[i+1][j] - img_b[i+1][j+1])/8
            new_img[i][j] = 0 if new_val<0 else new_val
    return new_img

#img_process('512X512.png', 'RGBY')
new_blur('512X512.png')


from openerp import models,fields,api,_
from openerp.exceptions import Warning

class sale_order(models.Model):
    _inherit = 'sale.order'

    @api.multi
    def action_button_confirm(self):
        zero_price = [x.product_id.name for x in self.order_line if not x.price_unit]
        if zero_price:
            message= _("Please specify unit price for the following products:") + '\n'
            message += '\n'.join(map(str,zero_price))
            raise Warning(message.rstrip())
        return super(sale_order,self).action_button_confirm()


class JSONMiddleware(object):
    """
    Process application/json requests data from GET and POST requests.
    """
    def process_request(self, request):
        if 'application/json' in request.META['CONTENT_TYPE']:
            # load the json data
            data = json.loads(request.body)
            # for consistency sake, we want to return
            # a Django QueryDict and not a plain Dict.
            # The primary difference is that the QueryDict stores
            # every value in a list and is, by default, immutable.
            # The primary issue is making sure that list values are
            # properly inserted into the QueryDict.  If we simply
            # do a q_data.update(data), any list values will be wrapped
            # in another list. By iterating through the list and updating
            # for each value, we get the expected result of a single list.
            q_data = QueryDict('', mutable=True)
            for key, value in data.iteritems():
                if isinstance(value, list):
                    # need to iterate through the list and upate
                    # so that the list does not get wrapped in an
                    # additional list.
                    for x in value:
                        q_data.update({key: x})
                else:
                    q_data.update({key: value})

            if request.method == 'GET':
                request.GET = q_data

            if request.method == 'POST':
                request.POST = q_data

        return None