import pytest

pytestmark = [pytest.mark.setone, pytest.mark.working]

"""
FF-701
Multiple ontologies get imported to form each of the three ontologies that we are currently using
(and I can foresee us wanting to add additional ontology sources).  The same name can be used for
terms with different identifiers (we may not currently have such cases but I can check) so you might
have the term ‘lung’ from UBERON and ‘lung’ from EFO and in theory these 2 terms could be meaning
different things.  For the most part this shouldn’t be a problem but without pre-screening for name
uniqueness and somehow (likely through human inspection) figuring out which name should be used and
which changed during the generate_ontology processing step then names aren’t guaranteed unique.

Ideally I’m hoping that the sort of check that I’m asking for can be set up in such a way so that
there can be some additional validation built in.

For instance checking to see that if someone is entering a new tissue term then that term is not
really referring to a cell line.

This can be determined by info that will be stored with the term in the system.

So basically what I’m looking for is validation that can get the  json from the request and also get
info on existing terms that are in the system and do some checks prior to post or patch of the item
and also change the json to use uuid rather than the info included in the post (eg. preferred_name)
as an identifying property if the term validates.
"""

def test_store_ontology_term_by_uuid(testapp, oterm):
    oterm.pop('term_name')  # this will create preferred_name in _update
    oterm.pop('preferred_name')
    res = testapp.post_json('/ontology_term', oterm)
    assert res.json['@graph'][0]['uuid'] == oterm['uuid']
    assert res.json['@graph'][0]['term_id'] == oterm['term_id']
    assert res.json['@graph'][0].get('preferred_name', None) is None

def test_store_ontology_term_by_term_id(testapp, oterm):
    oterm.pop('term_name')  # this will create preferred_name in _update
    oterm.pop('preferred_name')
    oterm.pop('uuid')
    res = testapp.post_json('/ontology_term', oterm)
    assert res.json['@graph'][0]['term_id'] == oterm['term_id']
    assert res.json['@graph'][0].get('preferred_name', None) is None
    testapp.get('/ontology_term/' + oterm['term_id'])

def test_store_ontology_no_required_keys(testapp, oterm):
    oterm.pop('term_name')
    oterm.pop('uuid')
    oterm.pop('term_id')
    testapp.post_json('/ontology_term', oterm, status=422)

def test_linkto_ontology_term_by_term_id(testapp, lab, award, oterm):
    item = {
        "accession": "4DNSROOOAAQ1",
        "biosource_type": "immortalized cell line",
        'award': award['@id'],
        'lab': lab['@id'],
        'tissue': oterm['term_id']
    }

    res = testapp.post_json('/ontology_term', oterm).json['@graph'][0]
    res_biosource = testapp.post_json('/biosource', item).json['@graph'][0]
    assert res['@id'] == res_biosource['tissue']


from database import *
from sqlalchemy import and_, func, text

import pandas as pd
import datetime
import sqlalchemy

def main():

	sql =  "SELECT measure_ranges.wsid AS wsid \
			FROM measure_ranges \
			JOIN weather_stations ON measure_ranges.wsid = weather_stations.id \
			JOIN cities ON weather_stations.cities_id = cities.id \
			JOIN states ON cities.state_id = states.id \
			WHERE  measure_ranges.wsid NOT IN \
				(SELECT measure_ranges.wsid AS wsid \
				 FROM measure_ranges \
				 JOIN weather_stations ON measure_ranges.wsid = weather_stations.id \
				 JOIN cities ON weather_stations.cities_id = cities.id \
				 JOIN states ON cities.state_id = states.id \
				 JOIN \
				   (SELECT cast(wsid AS int) AS wsid , \
						   max(mdct) AS m \
					FROM measurements_hourly \
					GROUP BY wsid) t ON t.wsid = measure_ranges.wsid \
				 WHERE states.abbreviation IN ('SP','MG','ES','RJ')) \
				 ORDER BY measure_ranges.wsid"

	result = session.execute(sql)

	for r in result:

	#q = session.query(MeasureRanges)
	#q = q.join(WeatherStation, MeasureRanges.wsid == WeatherStation.id)
	#q = q.join(City, WeatherStation.cities_id == City.id)
	#q = q.join(State, City.state_id == State.id)
	#q = q.filter(State.abbreviation.in_(['MG','SP','ES','RJ']))
	#q = q.filter(MeasureRanges.wsid.notin_(sq))

		q = session.query(MeasureRanges).filter(MeasureRanges.wsid==r[0])
		#q = session.query(MeasureRanges).filter(MeasureRanges.wsid>32).order_by(MeasureRanges.wsid)
		#q = session.query(MeasureRanges).order_by(MeasureRanges.wsid)

		ranges = q.all()

		i =0

		for r in ranges:

			print ('Pivoting WS %s' %r.wsid)

			m = session.query(func.max(MeasurementHourly.mdct)).filter(MeasurementHourly.wsid==r.wsid).scalar()

			if m:
				print ('Exist min: %s' %m)
				if m <= r.min:
					m = r.min
			else:
				m = r.min

			dates = pd.date_range(m,r.max, freq="H")

			for d in dates:

				try:

					ms = session.query(Measurement).filter(and_(Measurement.mdth==d,Measurement.wsid==r.wsid)).order_by(Measurement.mdth).all()

					values = [(i.ptid,i.vlue) for i in ms]
					l = dict(values)

					#print (d, l.get(6))

					mh = MeasurementHourly()
					mh.wsid = r.wsid
					mh.mdct = d
					mh.msdt = d
					mh.uthr = d.strftime("%H:%M:%S")
					mh.prcp = l.get(1)
					mh.stp = l.get(2)
					mh.smax = l.get(3)
					mh.smin = l.get(4)
					mh.gbrd = l.get(5)
					mh.temp = l.get(6)
					mh.tmax = l.get(7)
					mh.tmin = l.get(8)
					mh.dewp = l.get(9)
					mh.dmax = l.get(10)
					mh.dmin = l.get(11)
					mh.hmdy = l.get(12)
					mh.hmax = l.get(13)
					mh.hmin = l.get(14)
					mh.wdsp = l.get(15)
					mh.wdct = l.get(16)
					mh.gust = l.get(17)

					session.add(mh)

					i += 1
					if i > 1000:
						session.commit()
						print ('+ 1000 records commited!')
						i=0

				except sqlalchemy.exc.IntegrityError:
					print ('duplicate date: %s' %d)
					session.rollback()

			session.commit()

if __name__ == "__main__":
	main()


import scanpy as sc
import numpy as np
import scipy as sp
import pandas as pd
from skmisc.loess import loess

def score_cell(
    data,
    gene_list,
    gene_weight=None,
    ctrl_match_key="mean",
    n_ctrl=500,
    n_genebin=200,
    weight_opt="vs",
    copy=False,
    return_ctrl_raw_score=False,
    return_ctrl_norm_score=False,
    random_seed=0,
    verbose=False,
    save_intermediate=None,
):

    """Score cells based on the trait gene set

    Args
    ----
    data (n_cell, n_gene) : AnnData
        data.X should contain size-normalized log1p transformed count data
    gene_list (n_trait_gene) : list
        Trait gene list
    gene_weight (n_trait_gene) : list/np.ndarray
        Gene weights for genes in the gene_list.
        If gene_weight=None, the weigts are set to be one.
    ctrl_match_key : str
        The quantity for matching control and trait genes.
        ctrl_match_key should appear in data.obs.columns
    n_ctrl : int
        Number of control genesets
    n_genebin : int
        Number of bins for dividing genes by ctrl_match_key
    weight_opt : str
        Option for computing the raw score
        'uniform': average over the genes in the gene_list
        'vs': weighted average with weights equal to 1/sqrt(technical_variance_of_logct)
        'inv_std': weighted average with weights equal to 1/std
        'od': overdispersion score
    copy : bool
        If to make copy of the AnnData object to avoid writing on the orignal data
    return_raw_ctrl_score : bool
        If to return control scores
    return_norm_ctrl_score : bool
        If to return control scores
    random_seed : int
        Random seed
    verbose : bool
        If to output messages
    save_intermediate : str
        File path prefix for saving intermediate results

    Returns
    -------
    df_res (n_cell, n_key) : pd.DataFrame (dtype=np.float32) with columns:
        - raw_score
        - norm_score: scores after cell-wise and trait-wise normalization
        - mc_pval: p-values computed using only the control scores from the same cell
        - pval
        - nlog10_pval: -log10(pval). Needed in case the single precision (np.float32) gives inaccurate p-values
        - zscore: one-side z-score converted from pval
    """

    np.random.seed(random_seed)
    adata = data.copy() if copy else data
    n_cell, n_gene = adata.shape

    # Pre-compute statistics
    var_set = {"mean", "var", "var_tech"}
    obs_set = {"mean", "var"}
    if (len(var_set - set(adata.var.columns)) > 0) | (
        len(obs_set - set(adata.obs.columns)) > 0
    ):
        if verbose:
            print("# score_cell: recompute statistics using method.compute_stats")
        compute_stats(adata)

    # Check options
    if ctrl_match_key not in adata.var.columns:
        raise ValueError("# score_cell: %s not in data.var.columns" % ctrl_match_key)
    weight_opt_list = ["uniform", "vs", "inv_std", "od"]
    if weight_opt not in weight_opt_list:
        raise ValueError(
            "# score_cell: weight_opt not in [%s]"
            % ", ".join([str(x) for x in weight_opt_list])
        )

    if verbose:
        print(
            "# score_cell: ctrl_match_key=%s, n_ctrl=%d, n_genebin=%d, weight_opt=%s"
            % (ctrl_match_key, n_ctrl, n_genebin, weight_opt)
        )

    # Gene information
    df_gene = pd.DataFrame(
        index=adata.var_names,
        data={
            "gene": adata.var_names,
            "mean": adata.var["mean"].values,
            "var": adata.var["var"].values,
        },
    )
    if ctrl_match_key not in df_gene.columns:
        df_gene[ctrl_match_key] = adata.var[ctrl_match_key].values
    df_gene.drop_duplicates(subset="gene", inplace=True)

    # Update gene_list and gene_weight
    n_gene_old = len(list(gene_list))
    if gene_weight is None:
        gene_weight = [1] * n_gene_old
    dic_weight = {x: y for x, y in zip(gene_list, gene_weight)}
    gene_list = sorted(list(set(gene_list) & set(df_gene["gene"].values)))
    gene_weight = [dic_weight[x] for x in gene_list]

    if verbose:
        print(
            "# score_cell: %-15s %-15s %-20s"
            % (
                "trait gene set,",
                "%d/%d genes," % (len(gene_list), n_gene_old),
                "mean=%0.2e" % df_gene.loc[gene_list, "mean"].mean(),
            )
        )

    # Select control gene sets
    dic_ctrl_list, dic_ctrl_weight = _select_ctrl_geneset(
        df_gene, gene_list, gene_weight, ctrl_match_key, n_ctrl, n_genebin, random_seed
    )

    # Compute raw scores
    v_raw_score, v_score_weight = _compute_raw_score(
        adata, gene_list, gene_weight, weight_opt
    )

    mat_ctrl_raw_score, mat_ctrl_weight = np.zeros([n_cell, n_ctrl]), np.zeros(
        [len(gene_list), n_ctrl]
    )
    for i_ctrl in range(n_ctrl):
        mat_ctrl_raw_score[:, i_ctrl], mat_ctrl_weight[:, i_ctrl] = _compute_raw_score(
            adata, dic_ctrl_list[i_ctrl], dic_ctrl_weight[i_ctrl], weight_opt
        )

    # Compute normalized scores
    v_var_ratio_c2t = np.ones(n_ctrl)
    if weight_opt in ["uniform", "vs", "inv_std"]:
        # For raw scores compuated as weighted average. estimate variance ratio assuming independence
        for i_ctrl in range(n_ctrl):
            v_var_ratio_c2t[i_ctrl] = (
                df_gene.loc[dic_ctrl_list[i_ctrl], "var"]
                * mat_ctrl_weight[:, i_ctrl] ** 2
            ).sum()
        v_var_ratio_c2t /= (df_gene.loc[gene_list, "var"] * v_score_weight ** 2).sum()

    v_norm_score, mat_ctrl_norm_score = _correct_background(
        v_raw_score,
        mat_ctrl_raw_score,
        v_var_ratio_c2t,
        save_intermediate=save_intermediate,
    )

    # Get p-values
    mc_p = (1 + (mat_ctrl_norm_score.T >= v_norm_score).sum(axis=0)) / (1 + n_ctrl)
    pooled_p = _get_p_from_empi_null(v_norm_score, mat_ctrl_norm_score.flatten())
    nlog10_pooled_p = -np.log10(pooled_p)
    pooled_z = -sp.stats.norm.ppf(pooled_p).clip(min=-10, max=10)

    # Return result
    dic_res = {
        "raw_score": v_raw_score,
        "norm_score": v_norm_score,
        "mc_pval": mc_p,
        "pval": pooled_p,
        "nlog10_pval": nlog10_pooled_p,
        "zscore": pooled_z,
    }
    if return_ctrl_raw_score:
        for i in range(n_ctrl):
            dic_res["ctrl_raw_score_%d" % i] = mat_ctrl_raw_score[:, i]
    if return_ctrl_norm_score:
        for i in range(n_ctrl):
            dic_res["ctrl_norm_score_%d" % i] = mat_ctrl_norm_score[:, i]
    df_res = pd.DataFrame(index=adata.obs.index, data=dic_res, dtype=np.float32)
    return df_res

def _select_ctrl_geneset(
    input_df_gene,
    gene_list,
    gene_weight,
    ctrl_match_key,
    n_ctrl,
    n_genebin,
    random_seed,
):

    """Subroutine for score_cell, select control genesets

    Args
    ----
    input_df_gene (adata.shape[1], n_statistic) : pd.DataFrame
        Gene-wise statistics
    gene_list (n_trait_gene) : list
        Trait gene list
    gene_weight (n_trait_gene) : list/np.ndarray
        Gene weights for genes in the gene_list.
    ctrl_match_key : str
        The quantity for matching control and trait genes.
        ctrl_match_key should appear in input_df_gene.columns
    n_ctrl : int
        Number of control gene sets
    n_genebin : int
        Number of bins for dividing genes by ctrl_match_key
    random_seed : int
        Random seed

    Returns
    -------
    dic_ctrl_list : dictionary of lists
        dic_ctrl_list[i]: the i-th control gene list
    dic_ctrl_weight : dictionary of lists
        dic_ctrl_weight[i]: weights for the i-th control gene list

    """

    np.random.seed(random_seed)
    df_gene = input_df_gene.copy()
    trait_gene_set = set(gene_list)
    dic_weight = {x: y for x, y in zip(gene_list, gene_weight)}

    # Divide genes into equal-sized bins based on ctrl_match_key
    if df_gene[ctrl_match_key].unique().shape[0] < df_gene.shape[0] / 10:
        df_gene_bin = df_gene.groupby(ctrl_match_key).agg({"gene": set})
    else:
        df_gene["qbin"] = pd.qcut(
            df_gene[ctrl_match_key], q=n_genebin, labels=False, duplicates="drop"
        )
        df_gene_bin = df_gene.groupby("qbin").agg({"gene": set})

    # Find ctrl_match_key matched control genes
    dic_ctrl_list = {x: [] for x in range(n_ctrl)}
    dic_ctrl_weight = {x: [] for x in range(n_ctrl)}
    for bin_ in df_gene_bin.index:
        bin_gene = sorted(df_gene_bin.loc[bin_, "gene"])
        bin_trait_gene = sorted(df_gene_bin.loc[bin_, "gene"] & trait_gene_set)
        if len(bin_trait_gene) > 0:
            for i_list in np.arange(n_ctrl):
                dic_ctrl_list[i_list].extend(
                    np.random.choice(bin_gene, size=len(bin_trait_gene), replace=False)
                )
                dic_ctrl_weight[i_list].extend([dic_weight[x] for x in bin_trait_gene])

    return dic_ctrl_list, dic_ctrl_weight

def _compute_raw_score(adata, gene_list, gene_weight, weight_opt):
    """Compute raw score
        v_score_weight = gene_weight * {uniform/vs/inv_std}

    Args
    ----
    adata (n_cell, n_gene) : AnnData
        adata.X should contain size-normalized log1p transformed count data
    gene_list (n_trait_gene) : list
        Trait gene list
    gene_weight (n_trait_gene) : list/np.ndarray
        Gene weights for genes in the gene_list
    weight_opt : str
        Option for computing the raw score
        - 'uniform': average over the genes in the gene_list
        - 'vs': weighted average with weights equal to 1/sqrt(technical_variance_of_logct)
        - 'inv_std': weighted average with weights equal to 1/std
        - 'od': overdispersion score

    Returns
    -------
    v_raw_score (n_cell,) : np.ndarray
        Raw score
    v_score_weight (n_trait_gene,) : np.ndarray
        Gene weights score
    """

    # Compute raw score (overdispersion)
    if weight_opt == "od":
        return _compute_overdispersion_score(adata, gene_list, gene_weight)

    # Compute raw score (weighted average)
    if weight_opt == "uniform":
        v_score_weight = np.ones(len(gene_list))
    if weight_opt == "vs":
        v_score_weight = 1 / np.sqrt(adata.var.loc[gene_list, "var_tech"].values + 1e-2)
    if weight_opt == "inv_std":
        v_score_weight = 1 / np.sqrt(adata.var.loc[gene_list, "var"].values + 1e-2)

    if gene_weight is not None:
        v_score_weight = v_score_weight * np.array(gene_weight)
    v_score_weight = v_score_weight / v_score_weight.sum()
    v_raw_score = adata[:, gene_list].X.dot(v_score_weight).reshape([-1])

    return v_raw_score, v_score_weight

def _compute_overdispersion_score(adata, gene_list, gene_weight):
    """Compute overdispersion score

        Let w_g_raw = gene_weight / \sigma_{tech,g}^2
        Let w_g = w_g_raw / \sum_g w_g_raw
        s_c = \sum_g w_g * [(X_cg - \mu_g)^2 - \sigma_{tech,g}^2]

    Args
    ----
    adata (n_cell, n_gene) : AnnData
        adata.X should contain size-normalized log1p transformed count data
    gene_list (n_trait_gene) : list
        Trait gene list
    gene_weight (n_trait_gene) : list/np.ndarray
        Gene weights for genes in the gene_list

    Returns
    -------
    v_raw_score (n_cell,) : np.ndarray
        Raw score
    v_score_weight (n_trait_gene,) : np.ndarray
        Gene weights score
    """

    v_mean = adata.var.loc[gene_list, "mean"].values
    v_var_tech = adata.var.loc[gene_list, "var_tech"].values

    v_w = 1 / (v_var_tech + 1e-2)
    if gene_weight is not None:
        v_w = v_w * np.array(gene_weight)
    v_w = v_w / v_w.sum()

    # Compute overdispersion score
    if sp.sparse.issparse(adata.X):
        v_raw_score = (
            adata[:, gene_list].X.power(2).dot(v_w).reshape([-1])
        )  # Quadratic term
    else:
        v_raw_score = (
            (adata[:, gene_list].X ** 2).dot(v_w).reshape([-1])
        )  # Quadratic term
    v_raw_score = v_raw_score - adata[:, gene_list].X.dot(2 * v_w * v_mean).reshape(
        [-1]
    )  # Linear term
    v_raw_score = (
        v_raw_score + (v_w * (v_mean ** 2 - v_var_tech)).sum()
    )  # Constant term

    return v_raw_score, np.ones(len(gene_list))

def _correct_background(
    v_raw_score, mat_ctrl_raw_score, v_var_ratio_c2t, save_intermediate=None
):
    """Cell-wise and gene-wise background correction

    Args
    ----
    v_raw_score (n_cell,n_ctrl) : np.ndarray
        Trait raw score
    mat_ctrl_raw_score (n_cell,n_ctrl) : np.ndarray
        Control raw scores
    v_var_ratio_c2t (n_cell) : np.ndarray
        Variance ratio between control scores and disease score
    save_intermediate : str
        File path prefix for saving intermediate results

    Returns
    -------
    v_norm_score (n_cell,n_ctrl) : np.ndarray
        Trait normalized score
    mat_ctrl_norm_score (n_cell,n_ctrl) : np.ndarray
        Control normalized scores
    """

    if save_intermediate is not None:
        np.savetxt(
            save_intermediate + ".raw_score.tsv.gz",
            v_raw_score,
            fmt="%.9e",
            delimiter="\t",
        )
        np.savetxt(
            save_intermediate + ".ctrl_raw_score.tsv.gz",
            mat_ctrl_raw_score,
            fmt="%.9e",
            delimiter="\t",
        )

    # Calibrate gene-sets (mean 0 and same ind. var)
    ind_zero_score = v_raw_score == 0
    ind_zero_ctrl_score = mat_ctrl_raw_score == 0

    v_raw_score = v_raw_score - v_raw_score.mean()
    mat_ctrl_raw_score = mat_ctrl_raw_score - mat_ctrl_raw_score.mean(axis=0)
    mat_ctrl_raw_score = mat_ctrl_raw_score / np.sqrt(v_var_ratio_c2t)
    if save_intermediate is not None:
        np.savetxt(
            save_intermediate + ".raw_score.1st_gs_alignment.tsv.gz",
            v_raw_score,
            fmt="%.9e",
            delimiter="\t",
        )
        np.savetxt(
            save_intermediate + ".ctrl_raw_score.1st_gs_alignment.tsv.gz",
            mat_ctrl_raw_score,
            fmt="%.9e",
            delimiter="\t",
        )

    # Cell-wise correction
    v_mean = mat_ctrl_raw_score.mean(axis=1)
    v_std = mat_ctrl_raw_score.std(axis=1)
    v_norm_score = v_raw_score.copy()
    v_norm_score = (v_norm_score - v_mean) / v_std
    mat_ctrl_norm_score = ((mat_ctrl_raw_score.T - v_mean) / v_std).T
    if save_intermediate is not None:
        np.savetxt(
            save_intermediate + ".raw_score.cellwise_standardization.tsv.gz",
            v_norm_score,
            fmt="%.9e",
            delimiter="\t",
        )
        np.savetxt(
            save_intermediate + ".ctrl_raw_score.cellwise_standardization.tsv.gz",
            mat_ctrl_norm_score,
            fmt="%.9e",
            delimiter="\t",
        )

    # Gene-set-wise correction
    v_norm_score = v_norm_score - v_norm_score.mean()
    mat_ctrl_norm_score = mat_ctrl_norm_score - mat_ctrl_norm_score.mean(axis=0)
    if save_intermediate is not None:
        np.savetxt(
            save_intermediate + ".raw_score.2nd_gs_alignment.tsv.gz",
            v_norm_score,
            fmt="%.9e",
            delimiter="\t",
        )
        np.savetxt(
            save_intermediate + ".ctrl_raw_score.2nd_gs_alignment.tsv.gz",
            mat_ctrl_norm_score,
            fmt="%.9e",
            delimiter="\t",
        )

    # Set cells with raw_score=0 to the minimum norm_score value
    norm_score_min = min(v_norm_score.min(), mat_ctrl_norm_score.min())
    v_norm_score[ind_zero_score] = norm_score_min - 1e-3
    mat_ctrl_norm_score[ind_zero_ctrl_score] = norm_score_min
    if save_intermediate is not None:
        np.savetxt(
            save_intermediate + ".raw_score.final.tsv.gz",
            v_norm_score,
            fmt="%.9e",
            delimiter="\t",
        )
        np.savetxt(
            save_intermediate + ".ctrl_raw_score.final.tsv.gz",
            mat_ctrl_norm_score,
            fmt="%.9e",
            delimiter="\t",
        )

    return v_norm_score, mat_ctrl_norm_score

def compute_stats(data, n_mean_bin=20, n_var_bin=20, copy=False):
    """
    Precompute mean for each gene and mean&var for each cell
    """
    # Gene-wise statistics
    adata = data.copy() if copy else data
    adata.var["mean"], adata.var["var"] = _get_sparse_var(adata.X, axis=0)

    # Get the mean and var for the size-factor-normalized counts
    # It is highly correlated to the non-size-factor-normalized counts
    if sp.sparse.issparse(adata.X):  # sp sparse matrix
        temp_X = adata.X.copy().expm1()  # exp(X)-1 to get ct matrix from logct
    else:
        temp_X = np.expm1(adata.X)  # numpy ndarray
    adata.var["ct_mean"], adata.var["ct_var"] = _get_sparse_var(temp_X, axis=0)
    del temp_X

    # Borrowed from scanpy _highly_variable_genes_seurat_v3
    not_const = adata.var["ct_var"].values > 0
    estimat_var = np.zeros(adata.shape[1], dtype=np.float64)
    y = np.log10(adata.var["ct_var"].values[not_const])
    x = np.log10(adata.var["ct_mean"].values[not_const])
    model = loess(x, y, span=0.3, degree=2)
    model.fit()
    estimat_var[not_const] = model.outputs.fitted_values
    adata.var["ct_var_tech"] = 10 ** estimat_var
    # Recipe from Frost Nucleic Acids Research 2020
    adata.var["var_tech"] = (
        adata.var["var"] * adata.var["ct_var_tech"] / adata.var["ct_var"]
    )
    adata.var.loc[adata.var["var_tech"].isna(), "var_tech"] = 0

    # Add n_mean_bin*n_var_bin mean_var bins
    n_bin_max = np.floor(np.sqrt(adata.shape[1] / 10)).astype(int)
    if (n_mean_bin > n_bin_max) | (n_var_bin > n_bin_max):
        n_mean_bin, n_var_bin = n_bin_max, n_bin_max
        print(
            "Too few genes for 20*20 bins, setting n_mean_bin=n_var_bin=%d"
            % (n_bin_max)
        )
    v_mean_bin = pd.qcut(adata.var["mean"], n_mean_bin, labels=False, duplicates="drop")
    adata.var["mean_var"] = ""
    for bin_ in set(v_mean_bin):
        ind_select = v_mean_bin == bin_
        v_var_bin = pd.qcut(
            adata.var.loc[ind_select, "var"], n_var_bin, labels=False, duplicates="drop"
        )
        adata.var.loc[ind_select, "mean_var"] = [
            "%s.%s" % (x, y) for x, y in zip(v_mean_bin[ind_select], v_var_bin)
        ]

    # Cell-wise statistics
    adata.obs["mean"], adata.obs["var"] = _get_sparse_var(adata.X, axis=1)

    return adata if copy else None

def _get_sparse_var(sparse_X, axis=0):
    """
    Compute mean and var of a sparse matrix.
    """

    if sp.sparse.issparse(sparse_X):
        v_mean = sparse_X.mean(axis=axis)
        v_mean = np.array(v_mean).reshape([-1])
        v_var = sparse_X.power(2).mean(axis=axis)
        v_var = np.array(v_var).reshape([-1])
        v_var = v_var - v_mean ** 2
    else:
        v_mean = np.mean(sparse_X, axis=axis)
        v_var = np.var(sparse_X, axis=axis)

    return v_mean, v_var

def _get_p_from_empi_null(v_t, v_t_null):
    """Compute p-value from empirical null
    For score T and a set of null score T_1,...T_N, the p-value is

        p= [1 + \Sigma_{i=1}^N 1_{ (T_i \geq T) }] / (1+N)

    If T, T1, ..., T_N are i.i.d. variables following a null distritbuion,
    then p is super-uniform.

    The naive algorithm is N^2. Here we provide an O(N log N) algorithm to
    compute the p-value for each of the N elements in v_t

    Args
    ----
    v_t (M,): np.ndarray
        The observed score.
    v_t_null (N,): np.ndarray
        The null score.

    Returns
    -------
    v_p: (M,): np.ndarray
        P-value for each element in v_t
    """

    v_t = np.array(v_t)
    v_t_null = np.array(v_t_null)

    v_t_null = np.sort(v_t_null)
    v_pos = np.searchsorted(v_t_null, v_t, side="left")
    v_p = (v_t_null.shape[0] - v_pos + 1) / (v_t_null.shape[0] + 1)
    return v_p

##############################################################################
######################### Code for comparison methods ########################
##############################################################################

def score_cell_vision(adata, gene_list):

    """Score cells based on the trait gene set

    Args
    ----
    data (n_cell, n_gene) : AnnData
        data.X should contain size-normalized log1p transformed count data
    gene_list (n_trait_gene) : list
        Trait gene list

    Returns
    -------
    df_res (n_cell, n_key) : pd.DataFrame (dtype=np.float32)
        Columns:
        1. score: Vision signature score
        2. pval: p-value computed from the Vision score
    """

    gene_list = sorted(set(gene_list) & set(adata.var_names))
    v_mean, v_var = _get_sparse_var(adata.X, axis=1)

    v_score = adata[:, gene_list].X.mean(axis=1)
    v_score = np.array(v_score).reshape([-1])
    v_score = (v_score - v_mean) / np.sqrt(v_var / len(gene_list))
    v_p = 1 - sp.stats.norm.cdf(v_score)

    v_norm_score = (v_score - v_score.mean()) / v_score.std()
    v_norm_p = 1 - sp.stats.norm.cdf(v_norm_score)

    dic_res = {
        "score": v_score,
        "pval": v_p,
        "norm_score": v_norm_score,
        "norm_pval": v_norm_p,
    }
    df_res = pd.DataFrame(index=adata.obs.index, data=dic_res, dtype=np.float32)
    return df_res

def score_cell_scanpy(adata, gene_list):

    """Score cells based on the trait gene set

    Args
    ----
        data (n_cell, n_gene) : AnnData
            data.X should contain size-normalized log1p transformed count data
        gene_list (n_trait_gene) : list
            Trait gene list

    Returns
    -------
        df_res (n_cell, n_key) : pd.DataFrame (dtype=np.float32)
            Columns:
            1. score: Vision signature score
            2. pval: p-value computed from the Vision score
    """

    gene_list = sorted(set(gene_list) & set(adata.var_names))
    sc.tl.score_genes(adata, gene_list=gene_list)

    v_score = adata.obs["score"]
    v_score_z = (v_score - v_score.mean()) / np.sqrt(v_score.var())
    v_p = 1 - sp.stats.norm.cdf(v_score_z)

    dic_res = {"score": v_score, "score_z": v_score_z, "pval": v_p}
    df_res = pd.DataFrame(index=adata.obs.index, data=dic_res, dtype=np.float32)
    return df_res

##############################################################################
######################## Code for downstream analysis ########################
##############################################################################

def reg_out(mat_Y, mat_X):
    """Regress mat_X out of mat_Y

    Args
    ----
    mat_Y (n_sample, n_response) : np.ndarray
        Response variable
    mat_X (n_sample, n_covariates) : np.ndarray
        Covariates

    Returns
    -------
    mat_Y_resid (n_sample, n_response) : np.ndarray
        Response variable residual
    """

    mat_X = np.array(mat_X)
    if len(mat_X.shape) == 1:
        mat_X = mat_X.reshape([-1, 1])
    mat_Y = np.array(mat_Y)
    if len(mat_Y.shape) == 1:
        mat_Y = mat_Y.reshape([-1, 1])

    n_sample = mat_Y.shape[0]
    mat_xtx = np.dot(mat_X.T, mat_X) / n_sample
    mat_xty = np.dot(mat_X.T, mat_Y) / n_sample
    mat_coef = np.linalg.solve(mat_xtx, mat_xty)
    mat_Y_resid = mat_Y - mat_X.dot(mat_coef)

    if mat_Y_resid.shape[1] == 1:
        mat_Y_resid = mat_Y_resid.reshape([-1])

    return mat_Y_resid

def correlate_gene(
    data, trs_name="trs_ez", suffix="", corr_opt="pearson", cov_list=None, copy=False
):

    """Compute the correlation between gene expressions and TRS

    Args
    ----
    data (n_cell, n_gene) : AnnData
        adata.X should contain size-normalized log1p transformed count data
    trs_name : str
        The variable to correlate gene expression with. Should be one column in data.obs.
    suffix : str
        The name of the added gene-wise correlation would be 'trs_corr'+suffix.
    corr_opt : str
        Option for computing the correlation
        'pearson': Pearson's correlation
        'spearman': Spearman's correlation
    cov_list : list of str
        Covariates to control for.
        The covariates are first centered and then regressed out from
            both trs_name and the gene expression before computing the correlation.
        Elements in cov_list should be present in data.obs.columns
    copy : bool
        If to make copy of the AnnData object

    Returns
    -------
    adata (AnnData):
        Add the columns 'scdrs_corr'+suffix to data.var
    """

    adata = data.copy() if copy else data

    # Check options
    corr_opt_list = ["pearson", "spearman"]
    if corr_opt not in corr_opt_list:
        raise ValueError(
            "# compute_scdrs_corr: corr_opt not in [%s]"
            % ", ".join([str(x) for x in corr_opt_list])
        )
    if trs_name not in adata.obs.columns:
        raise ValueError("# compute_scdrs_corr: %s not in data.obs.columns" % trs_name)
    if cov_list is not None:
        temp_list = list(set(cov_list) - set(adata.obs.columns))
        if len(temp_list) > 0:
            raise ValueError(
                "# compute_scdrs_corr: covariates %s not in data.obs.columns"
                % ",".join(temp_list)
            )

    # Get data
    mat_X = data.X.toarray()
    v_trs = data.obs[trs_name].values.copy()

    # Regress out covariates
    if cov_list is not None:
        mat_cov = adata.obs[cov_list].values.copy()
        mat_cov = mat_cov - mat_cov.mean(axis=0)
        v_trs = reg_out(v_trs, mat_cov)
        mat_X = reg_out(mat_X, mat_cov)

    # Compute correlation
    if corr_opt == "pearson":
        v_corr = _pearson_corr(mat_X, v_trs)

    if corr_opt == "spearman":
        v_corr = _spearman_corr(mat_X, v_trs)

    adata.var["scdrs_corr" + suffix] = v_corr

    return adata if copy else None

def _pearson_corr(mat_X, mat_Y):
    """Pearson's correlation between every columns in mat_X and mat_Y

    Args
    ----
    mat_X (N,M1): np.ndarray
    mat_Y (N,M2): np.ndarray

    Returns
    -------
    mat_corr: (M1,M2): np.ndarray
        Correlation matrix
    """

    # If sparse, use _pearson_corr_sparse
    if sp.sparse.issparse(mat_X) | sp.sparse.issparse(mat_Y):
        return _pearson_corr_sparse(mat_X, mat_Y)

    # Reshape
    if len(mat_X.shape) == 1:
        mat_X = mat_X.reshape([-1, 1])
    if len(mat_Y.shape) == 1:
        mat_Y = mat_Y.reshape([-1, 1])

    mat_X = (mat_X - mat_X.mean(axis=0)) / mat_X.std(axis=0).clip(min=1e-8)
    mat_Y = (mat_Y - mat_Y.mean(axis=0)) / mat_Y.std(axis=0).clip(min=1e-8)
    mat_corr = mat_X.T.dot(mat_Y) / mat_X.shape[0]
    mat_corr = np.array(mat_corr, dtype=np.float32)

    if (mat_X.shape[1] == 1) | (mat_Y.shape[1] == 1):
        return mat_corr.reshape([-1])
    else:
        return mat_corr

def _pearson_corr_sparse(mat_X, mat_Y):
    """Pearson's correlation between every columns in mat_X and mat_Y (sparse matrix)

    Args
    ----
    mat_X (N,M1): sp.sparse
    mat_Y (N,M2): sp.sparse

    Returns
    -------
    mat_corr: (M1,M2): np.ndarray
        Correlation matrix
    """

    # Reshape
    if len(mat_X.shape) == 1:
        mat_X = mat_X.reshape([-1, 1])
    if len(mat_Y.shape) == 1:
        mat_Y = mat_Y.reshape([-1, 1])

    # Convert to sparse matrix if not already sparse
    if sp.sparse.issparse(mat_X) is False:
        mat_X = sp.sparse.csr_matrix(mat_X)
    if sp.sparse.issparse(mat_Y) is False:
        mat_Y = sp.sparse.csr_matrix(mat_Y)

    # Compute v_mean,v_var
    v_X_mean, v_X_var = _get_sparse_var(mat_X, axis=0)
    v_X_sd = np.sqrt(v_X_var).clip(min=1e-8)
    v_Y_mean, v_Y_var = _get_sparse_var(mat_Y, axis=0)
    v_Y_sd = np.sqrt(v_Y_var).clip(min=1e-8)

    mat_corr = mat_X.T.dot(mat_Y) / mat_X.shape[0]
    mat_corr = mat_corr - v_X_mean.reshape([-1, 1]).dot(v_Y_mean.reshape([1, -1]))
    mat_corr = mat_corr / v_X_sd.reshape([-1, 1]).dot(v_Y_sd.reshape([1, -1]))
    mat_corr = np.array(mat_corr, dtype=np.float32)

    if (mat_X.shape[1] == 1) | (mat_Y.shape[1] == 1):
        return mat_corr.reshape([-1])
    else:
        return mat_corr

def _spearman_corr(mat_X, mat_Y):
    """Spearman's correlation between every columns in mat_X and mat_Y

    Args
    ----
    mat_X (N,M1): np.ndarray
    mat_Y (N,M2): np.ndarray

    Returns
    -------
    mat_corr (M1,M2): np.ndarray
        Correlation matrix
    """

    # Reshape
    if len(mat_X.shape) == 1:
        mat_X = mat_X.reshape([-1, 1])
    if len(mat_Y.shape) == 1:
        mat_Y = mat_Y.reshape([-1, 1])

    mat_X = _get_rank(mat_X, axis=0)
    mat_Y = _get_rank(mat_Y, axis=0)

    mat_X = (mat_X - mat_X.mean(axis=0)) / mat_X.std(axis=0).clip(min=1e-8)
    mat_Y = (mat_Y - mat_Y.mean(axis=0)) / mat_Y.std(axis=0).clip(min=1e-8)
    mat_corr = mat_X.T.dot(mat_Y) / mat_X.shape[0]

    if (mat_X.shape[1] == 1) | (mat_Y.shape[1] == 1):
        return mat_corr.reshape([-1])
    else:
        return mat_corr

def _get_rank(mat_X, axis=0):
    """Spearman's correlation between every columns in mat_X and mat_Y

    Args
    ----
    mat_X (N,M): np.ndarray
    axis: int
        axis=0: column-wise rank (across rows)
        axis=1: row-wise rank (across columns)
    Returns
    -------
    mat_rank  (N,M): np.ndarray
        Rank matrix
    """

    if axis == 0:
        mat_X = np.argsort(mat_X, axis=0)
        mat_rank = np.empty_like(mat_X)
        temp_v = np.arange(mat_X.shape[0])
        for i_col in range(mat_X.shape[1]):
            mat_rank[mat_X[:, i_col], i_col] = temp_v

    if axis == 1:
        mat_X = np.argsort(mat_X, axis=1)
        mat_rank = np.empty_like(mat_X)
        temp_v = np.arange(mat_X.shape[1])
        for i_row in range(mat_X.shape[0]):
            mat_rank[i_row, mat_X[i_row, :]] = temp_v

    return mat_rank

def compute_gene_contrib(data, gene_list, random_seed=0, copy=False, verbose=False):

    """Find the contribution of each gene to the scDRS score

    Args
    ----
    data (n_cell, n_gene) : AnnData
        adata.X should contain size-normalized log1p transformed count data
    gene_list (n_trait_gene) : list
        Trait gene list
    copy : bool
        If to make copy of the AnnData object

    Returns
    -------
    adata (AnnData):
        Add the columns 'trs_corr'+suffix to data.var
    """

    np.random.seed(random_seed)
    adata = data.copy() if copy else data

    # Pre-compute statistics
    if "var_tech" not in adata.var.columns:
        if verbose:
            print("# score_cell: recompute statistics using method.compute_stats")
        compute_stats(adata)

    # Compute contribution for each gene
    gene_list = sorted(set(gene_list) & set(adata.var_names))
    v_score_weight = 1 / np.sqrt(adata.var.loc[gene_list, "var_tech"].values + 1e-2)
    df_contrib = pd.DataFrame(
        index=adata.obs_names, columns=gene_list, data=adata[:, gene_list].X.toarray()
    )
    df_contrib = df_contrib * v_score_weight

    return df_contrib


from matplotlib import pyplot as plt
from matplotlib.pyplot import MultipleLocator

y_3=[11,17,16,11,12,11,12,6,6,7,8,9]
y_10=[26,26,28,19,21,17,16,19,18,20,20,19]
x_3=range(1,13)
x_10=range(21,33)

plt.figure(figsize=(200,100),dpi=60)

_x=list(x_3)+list(x_10)
_xtick_labels=["3.{}".format(i) for i in x_3]
_xtick_labels+=["10.{}".format(i-20) for i in x_10]

plt.xticks(_x,_xtick_labels,rotation=45)
plt.xlabel("TIME")
plt.ylabel("TEMPERATURE")
plt.title("Change of Temperature")

plt.scatter(x_3,y_3,label="3rd month")
plt.scatter(x_10,y_10,label="10th month")

plt.legend(loc="upper left")

plt.show()


import pygame
import random

#---------------------------Цвета----------------------
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red  = (255, 0, 0)
#свои цвета
ber = (25,170,175)# березовый
lber = ( 9,220,190)# l - светлый березовый
Dpurple = (70,2,90)# D - темный фиолетовый
purple = ( 190,40,175)# фиолетовый
lroze = (255,170,175) # легкий розовый
pygame.init ()

size = (700, 500)
screen = pygame.display.set_mode (size)
pygame.display.set_caption ("My game alpha 0.001")
# Цикл, пока пользователь не нажмет кнопку закрытия.
done = False
#------------------глобальные переменные------------------
rect_x = 50
rect_y = 50
rect_change_x = 5
rect_change_y = 5
snow_list = []
for i in range(50):
    x = random.randrange(0, 700)
    y = random.randrange(0, 400)
    snow_list.append([x, y])
# Скорость, пикселей за кадр
x_speed = 0
y_speed = 0
# Текущая позиция
x_coord = 10
y_coord = 10
star_list = []
#---------------------------------------------------
# Используется для управления скоростью обновления экрана.
clock = pygame.time.Clock ()

#----------------------Функции элементов----------------------

#функция отскока прямоугольника от краев
def Priam():
    global rect_x,rect_y,rect_change_x,rect_change_y
    pygame.draw.rect(screen, purple, [rect_x, rect_y, 50, 50])
    pygame.draw.rect(screen, red, [rect_x + 10, rect_y + 10, 30, 30])
    rect_x += rect_change_x
    rect_y += rect_change_y
    # Заставить прямоугольник отпрыгнуть, если нужно
    if rect_y > 450 or rect_y < 0:
        rect_change_y = rect_change_y * -1
    if rect_x > 650 or rect_x < 0:
        rect_change_x = rect_change_x * -1

#функция снега
def snow_up():

    for i in range(len(snow_list)):

        # рисует круг, по координате, а потом по координате -1, и так двигает
        pygame.draw.circle(screen, lroze, snow_list[i], 2)
        # меняет местоположение снежинки(скорость),направление
        snow_list[i][1] -= 1

        # если снег зашел за экран ( вверх)
        if snow_list[i][1] == 0:
            # новая позиция игрика
            y = random.randrange(401, 402)
            snow_list[i][1] = y

#функция заднего фона
def background():
    for y_offset in range(0, 140, 10):
        for x_offset in range(0,140,10):
            pygame.draw.line(screen, ber, [0, 400 + y_offset], [700, 400 + y_offset], 2)
            pygame.draw.line(screen, Dpurple, [0, 405 + y_offset], [700, 405 + y_offset], 8)
# попытка сделать градиент
    global grber
    for y_offset in range(0, 230, 5):
        grber =(255-y_offset ,170,175) #gr - градиент
        pygame.draw.line(screen, grber, [0, 400 - (1.75 *y_offset)],
                         [700, 400 - (1.75*y_offset)],9)

#функция человечка
def draw_stick_figure(screen, x, y):
    # Голова
    pygame.draw.ellipse(screen, black, [1 + x, y, 10, 10], 0)

    # Ноги
    pygame.draw.line(screen, black, [5 + x, 17 + y], [10 + x, 27 + y], 2)
    pygame.draw.line(screen, black, [5 + x, 17 + y], [x, 27 + y], 2)

    # Тело
    pygame.draw.line(screen, red, [5 + x, 17 + y], [5 + x, 7 + y], 2)

    # Руки
    pygame.draw.line(screen, red, [5 + x, 7 + y], [9 + x, 17 + y], 2)
    pygame.draw.line(screen, red, [5 + x, 7 + y], [1 + x, 17 + y], 2)
# Спрятать курсор мыши
pygame.mouse.set_visible(1)
# -------- Основной программный цикл -----------
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

# ------------- Логика игры должна идти здесь--------------
    screen.fill (lber)

# ---------- Код рисования должен идти здесь ----------------
#задний план
    background()
#снег
    snow_up()

#------------------------------------------------------------
    pygame.display.flip ()
    clock.tick (30)
pygame.quit ()


from django import forms
from django.utils.translation import ugettext as _
from django.forms.util import ValidationError

from autocomplete.widgets import AutoCompleteWidget
from geo.models import City, State, Address

class CityField(forms.ModelChoiceField):
    def clean(self, value):
        forms.Field.clean(self, value)
        if self.required and value in (None, ''):
            raise ValidationError(self.error_messages['required'])
        return value

class BaseAddressForm(forms.ModelForm):
    street = forms.CharField(label=_("Street"), max_length=255)
    state = forms.ModelChoiceField(queryset=State.objects.all(), required=True)
    city = CityField(queryset=City.objects, to_field_name="name", required=True,
                    widget=AutoCompleteWidget(
                        channel=('autocomplete', None, ('geo', 'city')),
                        options={'minChars': 1, 'width': '207px',},
                        related_fields=("state", )))
    postal_code = forms.CharField(label=_("Zip"), required=True)

    def __init__(self, *args, **kwargs):
        prefix = kwargs.get("prefix")
        super(BaseAddressForm, self).__init__(*args, **kwargs)
        if prefix:
            self.fields["city"].widget.set_prefix_suffix(prefix)
        instance = kwargs.get("instance", None)
        if not instance is None and instance.city:
            self.initial['city'] = instance.city
            self.initial['state'] = instance.city.state.id

    def clean(self):
        cd = self.cleaned_data
        state = cd.get('state', None)
        city = cd.get('city', None)
        if state:
            if city:
                city_field = self.fields['city']
                value, new = city_field.queryset.get_or_create(\
                                    **{city_field.to_field_name or 'pk': city,
                                       "state":state })
                cd['city'] = value
        else:
            cd["city"] = None
        return cd

    class Meta:
        model = Address
        fields = ('street', 'state', 'city', 'postal_code')

class AddressNotRequiredForm(BaseAddressForm):
    def __init__(self, *args, **kwargs):
        super(AddressNotRequiredForm, self).__init__(*args, **kwargs)
        self.fields["street"].required = False
        self.fields["state"].required = False
        self.fields["city"].required = False
        self.fields["postal_code"].required = False


# -*- coding: utf-8 -*-
"""
Created on Thu Nov  8 10:50:08 2018

@author: sanket
"""

import socket
import nodes
import time
import threading
import json
import copy

'''ONLY CHANGE NODE_NO FOR NEW NODES'''
NODE_NO = 2
NODE_ADDRESS = nodes.node_list[NODE_NO]
FANOUT = nodes.fanout
HEARTBEAT = nodes.heartbeat
NEIGHBORS = []
OTHER_NODES = copy.deepcopy(nodes.node_list)
OTHER_NODES.remove(NODE_ADDRESS)

#extra
members = nodes.memberlist;

#semaphore
sem = threading.Semaphore()

def hb_fun(sock, neighbors, members,):
    try:
        while True:
            time.sleep(HEARTBEAT)
            sem.acquire();
            members[NODE_ADDRESS][0]= members[NODE_ADDRESS][0]+1;
            members[NODE_ADDRESS][1] = time.time();
            d = {}
            nodes.encode_memberlist(members, d)
            msg_json = json.dumps(d)
            for destination in NEIGHBORS:
                try:
                    sock.sendto( msg_json.encode(), destination);
                except ConnectionResetError: #that node closed the connection
                    continue
            sem.release()
    except:
        print("Closed")

k=1;
i  = (NODE_NO+1)%len(nodes.node_list)

while i != NODE_NO and k <= FANOUT:
    NEIGHBORS.append(nodes.node_list[i])
    k = k+1
    i = (i+1)%len(nodes.node_list)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
sock.bind(NODE_ADDRESS);
received_data, sender_address = sock.recvfrom(1024);

#first time gossip recived
m={}
nodes.decode_memberlist( json.loads( received_data.decode() ), m)
t1 = threading.Thread(target=hb_fun, args=(sock, NEIGHBORS,members,))
t1.start()
sock.settimeout(2);
#Infected
t2 = time.time()

try:
    while True:
       try:
           received_data, sender_address = sock.recvfrom(1024); #should be power of two
       except socket.timeout:
           if abs(time.time()-t2) >= nodes.T_Failure:
               print("No one is responding");
               print("Close?")
               raise Exception('No one is responding!');
           else:
              continue;
       except ConnectionResetError:
           #Reciving data corrupted due to forcefully port close at the sender
           #do the timing trick here, and exit the program thereafter
           continue;
       t2 = time.time()
       m = {}
       nodes.decode_memberlist( json.loads( received_data.decode() ), m);
       sem.acquire();
       nodes.compare_members(members, m);
       print("Node No:", NODE_NO,"Address:", NODE_ADDRESS);
       nodes.print_nodes(members)
       print('-'*60);
       sem.release();
except KeyboardInterrupt:
    print('Server closing')
    sock.close()
    t1.join()
except:
    sock.close()
    t1.join()
    input()


import nltk
import requests

with open ("/Users/markvsteinbach/Downloads/siddh01.txt", "r") as myfile:
    data=myfile.read().replace('\r\n', '')

words = nltk.word_tokenize(data)
my_bigrams = nltk.bigrams(words)
my_trigrams = nltk.trigrams(words)
# print words, my_bigrams, my_trigrams

words_file = open("words.txt", "w")
words_file.write(raw_input(words))
words_file.close()

bigrams_file = open("bigrams.txt", "w")
bigrams_file.write(raw_input(my_bigrams))
bigrams_file.close()

trigrams_file = open("trigrams.txt", "w")
trigrams_file.write(raw_input(my_trigrams))
trigrams_file.close()


# This program is based on the color.c program written by Naofumi.

import os, sys
from GUI import Window, application
from GUI.GL import GLView, GLConfig
from OpenGL.GL import glClearColor, glClear, glBegin, glColor3f, glVertex2i, glEnd, \
	GL_COLOR_BUFFER_BIT, GL_TRIANGLES
from TestInput import TestKeyEvents, TestMouseEvents
from testing import say

class TriangleView(TestKeyEvents, TestMouseEvents, GLView):

	def init_context(self):
		glClearColor(0.0, 0.0, 0.0, 0.0)

	def render(self):
		glClear(GL_COLOR_BUFFER_BIT)
		glBegin(GL_TRIANGLES)
		glColor3f(1.0, 0.0, 0.0)
		glVertex2i(0, 1)
		glColor3f(0.0, 1.0, 0.0)
		glVertex2i(-1, -1)
		glColor3f(0.0, 0.0, 1.0)
		glVertex2i(1, -1)
		glEnd()

def make_view(db, options):
	pf = GLConfig(double_buffer = db)
	pf.alpha = "a" in options
	pf.depth_buffer = "d" in options
	pf.stencil_buffer = "s" in options
	pf.aux_buffers = "x" in options
	pf.accum_buffer = "A" in options
	view = TriangleView(pf, size = (200, 200))
	win = Window(
		title = "%s Buffered GLView" % ["Single", "Double"][db],
		size = (240, 240))
	win.place(view, left = 20, top = 20, sticky = "nsew")
	view.become_target()
	win.show()

def get_args():
	args = sys.argv[1:]
	if not 0 <= len(args) <= 2:
		badusage()
	if args:
		sd = args[0][0]
	else:
		sd = "d"
	if sd == "s":
		db = False
	elif sd == "d":
		db = True
	else:
		badusage()
	if len(args) > 1:
		options = args[1]
	else:
		options = ""
	return db, options

def badusage():
	sys.stderr.write("Usage: python %s s[ingle]|d[ouble] [adsxA]\n" % os.path.basename(sys.argv[0]))
	sys.exit(1)

instructions = """
There should be a window containing an OpenGL view, 20 pixels
from the edge of the window on all sides. There should be a
coloured triangle filling the view over a black background.

Mouse and keyboard events in the view should be reported.
The view and the triangle it contains should resize smoothly
with the window.
"""

make_view(*get_args())
say(instructions)
application().run()


#! /usr/bin/env python3

# coding: utf-8

########### load packages ############
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from bs4 import BeautifulSoup
import csv
import lxml
import requests
import re
import multiprocessing
from multiprocessing import Lock
import time
import os
########### Chrome浏览器驱动设置 ############
options = Options()
options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错
options.add_argument('window-size=1920x3000') #指定浏览器分辨率
options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
#options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
#options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
#options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使用的浏览器位置
Executable_path="C:\\Users\\17914\\Downloads\\chromedriver_win32\\chromedriver.exe"
#key caption url
class baidu_image_spider():
    def __init__(self,outfile='./out.csv'):
        self.driver=webdriver.Chrome(options=options,executable_path=Executable_path)
        self.driver.get("https://image.baidu.com/")
        time.sleep(1)
        self.driver.refresh()
        self.cookie = self.driver.get_cookies()
        #self.infile=infile
        self.outfile=outfile
    def search_oneTerm(self,keyword):
         page=0
         query_url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+ keyword +'&pn='+str(page)
         self.driver.get(query_url)
         html = self.driver.page_source
         soup = BeautifulSoup(html, 'lxml')
         nxt=True if soup.find('a',class_='n',string='下一页') else False
         #print()
         img_items=soup.find_all('li',class_='imgitem')
         img_list=[]
         caption_list=[]
         for img in img_items:
             img_list.append(img.find('img').get('src'))
             caption_list.append(img.find('div',class_='hover').get('title').replace('','').replace('',''))
         while(nxt):
             page+=20
             query_url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+ keyword +'&pn='+str(page)
             self.driver.get(query_url)
             html = self.driver.page_source
             soup = BeautifulSoup(html, 'lxml')
             nxt=True if soup.find('a',class_='n',string='下一页') else False
             img_items=soup.find_all('li',class_='imgitem')
             for img in img_items:
                img_list.append(img.find('img').get('src'))
                caption_list.append(img.find('div',class_='hover').get('title').replace('','').replace('',''))
         #print(caption_list)
         #print(img_list)
         return img_list,caption_list
    def search_all(self,key_list):
        if not os.path.exists(self.outfile):
            fout=open(self.outfile,"a",encoding='utf-8',newline = "")
            writer = csv.writer(fout)
            writer.writerow(['keyword','url','caption'])
        else:
            fout=open(self.outfile,"a",encoding='utf-8',newline = "")
            writer = csv.writer(fout)
        for key in key_list:
            print(key)
            urls,captions=self.search_oneTerm(key)
            for i in range(len(urls)):
                 writer.writerow([key,urls[i],captions[i]])

if __name__=='__main__':
    keywords_path = './aic_all_zh.txt' #'THUOCL_food_8974.txt'
    keywords=[]
    with open(keywords_path,"r",encoding='utf-8') as f:
       for line in f.readlines():
          try:
             line = line.strip('\n').split('\t')[1].replace("/"," ")
          except:
             print(line)
             continue
          keywords.append(line)

    s=baidu_image_spider('./out1.csv')
    s2=baidu_image_spider('./out2.csv')
    l=len(keywords)
    s.search_all(keywords[:l//2])
    s2.search_all(keywords[l//2:])


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('https://raw.githubusercontent.com/mGalarnyk/Python_Tutorials/master/Kaggle/BreastCancerWisconsin/data/data.csv')
sns.boxplot(x='diagnosis', y='area_mean', data=df)
malignant = df[df['diagnosis']=='M']['area_mean']
benign = df[df['diagnosis']=='B']['area_mean']
fig = plt.figure()
ax = fig.add_subplot(111)
ax.boxplot([malignant,benign], labels=['M', 'B'])
df.boxplot(column = 'area_mean', by = 'diagnosis')
plt.title('')
malignant = df[df['diagnosis']=='M']['area_mean']
benign = df[df['diagnosis']=='B']['area_mean']
fig = plt.figure()
ax = fig.add_subplot(111)
ax.boxplot([malignant,benign], notch = True, labels=['M', 'B'])
plt.show()


from django.forms import ModelForm, widgets
from django import forms
from app.models import Ficha_salud
from django.forms import ModelForm, widgets

class AddSalud(ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for i in self.visible_fields():
            i.field.widget.attrs['class'] = 'form-control'
            i.field.widget.attrs['autocomplete'] = 'off'
        self.fields['NomEnfer_fichsa'].widget.attrs['autofocus'] = True

    class Meta:
        model = Ficha_salud
        fields = '__all__'
        widgets = {
            'descripcion_fichsal' : widgets.Textarea(attrs={'class':'form-control'})}

class FormSalud(ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for i in self.visible_fields():
            i.field.widget.attrs['class'] = 'form-control'
            i.field.widget.attrs['autocomplete'] = 'off'
        # self.fields['id_est'].widget.attrs['disabled'] = True
        # self.fields['fecha_est'].widget.attrs['hidden'] = True
    class Meta:
        model = Ficha_salud
        fields = '__all__'
        exclude = ('id_est','')