import os
import re
import shutil
import zipfile
from functools import partial
from os import path as op

import pooch
import pytest

from mne import datasets, read_labels_from_annot, write_labels_to_annot
from mne.datasets import fetch_dataset, fetch_infant_template, fetch_phantom, testing
from mne.datasets._fsaverage.base import _set_montage_coreg_path
from mne.datasets._infant import base as infant_base
from mne.datasets._phantom import base as phantom_base
from mne.datasets.utils import _manifest_check_download
from mne.utils import (
    ArgvSetter,
    _pl,
    catch_logging,
    get_subjects_dir,
    hashfunc,
    requires_good_network,
    use_log_level,
)

subjects_dir = testing.data_path(download=False) / "subjects"

def test_datasets_basic(tmp_path, monkeypatch):
    """Test simple dataset functions."""
    # XXX 'hf_sef' and 'misc' do not conform to these standards
    for dname in (
        "sample",
        "somato",
        "spm_face",
        "testing",
        "opm",
        "bst_raw",
        "bst_auditory",
        "bst_resting",
        "multimodal",
        "bst_phantom_ctf",
        "bst_phantom_elekta",
        "kiloword",
        "mtrf",
        "phantom_4dbti",
        "visual_92_categories",
        "fieldtrip_cmc",
    ):
        if dname.startswith("bst"):
            dataset = getattr(datasets.brainstorm, dname)
        else:
            dataset = getattr(datasets, dname)
        if str(dataset.data_path(download=False)) != ".":
            assert isinstance(dataset.get_version(), str)
            assert datasets.has_dataset(dname)
        else:
            assert dataset.get_version() is None
            assert not datasets.has_dataset(dname)
        print("%s: %s" % (dname, datasets.has_dataset(dname)))
    tempdir = str(tmp_path)
    # Explicitly test one that isn't preset (given the config)
    monkeypatch.setenv("MNE_DATASETS_SAMPLE_PATH", tempdir)
    dataset = datasets.sample
    assert str(dataset.data_path(download=False)) == "."
    assert dataset.get_version() != ""
    assert dataset.get_version() is None
    # don't let it read from the config file to get the directory,
    # force it to look for the default
    monkeypatch.setenv("_MNE_FAKE_HOME_DIR", tempdir)
    monkeypatch.delenv("SUBJECTS_DIR", raising=False)
    assert str(datasets.utils._get_path(None, "foo", "bar")) == op.join(
        tempdir, "mne_data"
    )
    assert get_subjects_dir(None) is None
    _set_montage_coreg_path()
    sd = get_subjects_dir()
    assert sd.name.endswith("MNE-fsaverage-data")
    monkeypatch.setenv("MNE_DATA", str(tmp_path / "foo"))
    with pytest.raises(FileNotFoundError, match="as specified by MNE_DAT"):
        testing.data_path(download=False)

@requires_good_network
def test_downloads(tmp_path, monkeypatch, capsys):
    """Test dataset URL and version handling."""
    # Try actually downloading a dataset
    kwargs = dict(path=str(tmp_path), verbose=True)
    # XXX we shouldn't need to disable capsys here, but there's a pytest bug
    # that we're hitting (https://github.com/pytest-dev/pytest/issues/5997)
    # now that we use pooch
    with capsys.disabled():
        with pytest.raises(RuntimeError, match="Do not download .* in tests"):
            path = datasets._fake.data_path(update_path=False, **kwargs)
        monkeypatch.setattr(
            datasets.utils, "_MODULES_TO_ENSURE_DOWNLOAD_IS_FALSE_IN_TESTS", ()
        )
        path = datasets._fake.data_path(update_path=False, **kwargs)
    assert op.isdir(path)
    assert op.isfile(op.join(path, "bar"))
    assert not datasets.has_dataset("fake")  # not in the desired path
    assert datasets._fake.get_version() is None
    assert datasets.utils._get_version("fake") is None
    monkeypatch.setenv("_MNE_FAKE_HOME_DIR", str(tmp_path))
    with pytest.warns(RuntimeWarning, match="non-standard config"):
        new_path = datasets._fake.data_path(update_path=True, **kwargs)
    assert path == new_path
    out, _ = capsys.readouterr()
    assert "Downloading" not in out
    # No version: shown as existing but unknown version
    assert datasets.has_dataset("fake")
    # XXX logic bug, should be "unknown"
    assert datasets._fake.get_version() == "0.0"
    # With a version but no required one: shown as existing and gives version
    fname = tmp_path / "foo" / "version.txt"
    with open(fname, "w") as fid:
        fid.write("0.1")
    assert datasets.has_dataset("fake")
    assert datasets._fake.get_version() == "0.1"
    datasets._fake.data_path(download=False, **kwargs)
    out, _ = capsys.readouterr()
    assert "out of date" not in out
    # With the required version: shown as existing with the required version
    monkeypatch.setattr(datasets._fetch, "_FAKE_VERSION", "0.1")
    assert datasets.has_dataset("fake")
    assert datasets._fake.get_version() == "0.1"
    datasets._fake.data_path(download=False, **kwargs)
    out, _ = capsys.readouterr()
    assert "out of date" not in out
    monkeypatch.setattr(datasets._fetch, "_FAKE_VERSION", "0.2")
    # With an older version:
    # 1. Marked as not actually being present
    assert not datasets.has_dataset("fake")
    # 2. Will try to update when `data_path` gets called, with logged message
    want_msg = "Correctly trying to download newer version"

    def _error_download(self, fname, downloader, processor):
        url = self.get_url(fname)
        full_path = self.abspath / fname
        assert "foo.tgz" in url
        assert str(tmp_path) in str(full_path)
        raise RuntimeError(want_msg)

    monkeypatch.setattr(pooch.Pooch, "fetch", _error_download)
    with pytest.raises(RuntimeError, match=want_msg):
        datasets._fake.data_path(**kwargs)
    out, _ = capsys.readouterr()
    assert re.match(r".* 0\.1 .*out of date.* 0\.2.*", out, re.MULTILINE), out

    # Hash mismatch suggestion
    # https://mne.discourse.group/t/fsaverage-hash-value-mismatch/4663/3
    want_msg = "MD5 hash of downloaded file (MNE-sample-data-processed.tar.gz) does not match the known hash: expected md5:e8f30c4516abdc12a0c08e6bae57409c but got a9dfc7e8843fd7f8a928901e12fb3d25. Deleted download for safety. The downloaded file may have been corrupted or the known hash may be outdated."  # noqa: E501

    def _error_download_2(self, fname, downloader, processor):
        url = self.get_url(fname)
        full_path = self.abspath / fname
        assert "foo.tgz" in url
        assert str(tmp_path) in str(full_path)
        raise ValueError(want_msg)

    shutil.rmtree(tmp_path / "foo")
    monkeypatch.setattr(pooch.Pooch, "fetch", _error_download_2)
    with pytest.raises(ValueError, match=".*known hash.*force_update=True.*"):
        datasets._fake.data_path(download=True, force_update=True, **kwargs)

@pytest.mark.slowtest
@testing.requires_testing_data
@requires_good_network
def test_fetch_parcellations(tmp_path):
    """Test fetching parcellations."""
    pytest.importorskip("nibabel")
    this_subjects_dir = str(tmp_path)
    os.mkdir(op.join(this_subjects_dir, "fsaverage"))
    os.mkdir(op.join(this_subjects_dir, "fsaverage", "label"))
    os.mkdir(op.join(this_subjects_dir, "fsaverage", "surf"))
    for hemi in ("lh", "rh"):
        shutil.copyfile(
            op.join(subjects_dir, "fsaverage", "surf", "%s.white" % hemi),
            op.join(this_subjects_dir, "fsaverage", "surf", "%s.white" % hemi),
        )
    # speed up by prenteding we have one of them
    with open(
        op.join(this_subjects_dir, "fsaverage", "label", "lh.aparc_sub.annot"), "wb"
    ):
        pass
    datasets.fetch_aparc_sub_parcellation(subjects_dir=this_subjects_dir)
    with ArgvSetter(("--accept-hcpmmp-license",)):
        datasets.fetch_hcp_mmp_parcellation(subjects_dir=this_subjects_dir)
    for hemi in ("lh", "rh"):
        assert op.isfile(
            op.join(
                this_subjects_dir, "fsaverage", "label", "%s.aparc_sub.annot" % hemi
            )
        )
    # test our annot round-trips here
    kwargs = dict(
        subject="fsaverage", hemi="both", sort=False, subjects_dir=this_subjects_dir
    )
    labels = read_labels_from_annot(parc="HCPMMP1", **kwargs)
    write_labels_to_annot(
        labels,
        parc="HCPMMP1_round",
        table_name="./left.fsaverage164.label.gii",
        **kwargs,
    )
    orig = op.join(this_subjects_dir, "fsaverage", "label", "lh.HCPMMP1.annot")
    first = hashfunc(orig)
    new = orig[:-6] + "_round.annot"
    second = hashfunc(new)
    assert first == second

_zip_fnames = ["foo/foo.txt", "foo/bar.txt", "foo/baz.txt"]

def _fake_zip_fetch(url, path, fname, *args, **kwargs):
    fname = op.join(path, fname)
    with zipfile.ZipFile(fname, "w") as zipf:
        with zipf.open("foo/", "w"):
            pass
        for fname in _zip_fnames:
            with zipf.open(fname, "w"):
                pass

@pytest.mark.parametrize("n_have", range(len(_zip_fnames)))
def test_manifest_check_download(tmp_path, n_have, monkeypatch):
    """Test our manifest downloader."""
    monkeypatch.setattr(pooch, "retrieve", _fake_zip_fetch)
    destination = op.join(str(tmp_path), "empty")
    manifest_path = op.join(str(tmp_path), "manifest.txt")
    with open(manifest_path, "w") as fid:
        for fname in _zip_fnames:
            fid.write("%s\n" % fname)
    assert n_have in range(len(_zip_fnames) + 1)
    assert not op.isdir(destination)
    if n_have > 0:
        os.makedirs(op.join(destination, "foo"))
        assert op.isdir(op.join(destination, "foo"))
    for fname in _zip_fnames:
        assert not op.isfile(op.join(destination, fname))
    for fname in _zip_fnames[:n_have]:
        with open(op.join(destination, fname), "w"):
            pass
    with catch_logging() as log:
        with use_log_level(True):
            # we mock the pooch.retrieve so these are not used
            url = hash_ = ""
            _manifest_check_download(manifest_path, destination, url, hash_)
    log = log.getvalue()
    n_missing = 3 - n_have
    assert ("%d file%s missing from" % (n_missing, _pl(n_missing))) in log
    for want in ("Extracting missing", "Successfully "):
        if n_missing > 0:
            assert want in log
        else:
            assert want not in log
    assert op.isdir(destination)
    for fname in _zip_fnames:
        assert op.isfile(op.join(destination, fname))

def _fake_mcd(manifest_path, destination, url, hash_, name=None, fake_files=False):
    if name is None:
        name = url.split("/")[-1].split(".")[0]
        assert name in url
        assert name in str(destination)
    assert name in manifest_path
    assert len(hash_) == 32
    if fake_files:
        with open(manifest_path) as fid:
            for path in fid:
                path = path.strip()
                if not path:
                    continue
                fname = op.join(destination, path)
                os.makedirs(op.dirname(fname), exist_ok=True)
                with open(fname, "wb"):
                    pass

def test_infant(tmp_path, monkeypatch):
    """Test fetch_infant_template."""
    monkeypatch.setattr(infant_base, "_manifest_check_download", _fake_mcd)
    fetch_infant_template("12mo", subjects_dir=tmp_path)
    with pytest.raises(ValueError, match="Invalid value for"):
        fetch_infant_template("0mo", subjects_dir=tmp_path)

def test_phantom(tmp_path, monkeypatch):
    """Test phantom data downloading."""
    # The Otaniemi file is only ~6MB, so in principle maybe we could test
    # an actual download here. But it doesn't seem worth it given that
    # CircleCI will at least test the VectorView one, and this file should
    # not change often.
    monkeypatch.setattr(
        phantom_base,
        "_manifest_check_download",
        partial(_fake_mcd, name="phantom_otaniemi", fake_files=True),
    )
    fetch_phantom("otaniemi", subjects_dir=tmp_path)
    assert op.isfile(tmp_path / "phantom_otaniemi" / "mri" / "T1.mgz")

@requires_good_network
def test_fetch_uncompressed_file(tmp_path):
    """Test downloading an uncompressed file with our fetch function."""
    dataset_dict = dict(
        dataset_name="license",
        url=(
            "https://raw.githubusercontent.com/mne-tools/mne-python/main/" "LICENSE.txt"
        ),
        archive_name="LICENSE.foo",
        folder_name=op.join(tmp_path, "foo"),
        hash=None,
    )
    fetch_dataset(dataset_dict, path=None, force_update=True)
    assert (tmp_path / "foo" / "LICENSE.foo").is_file()


import json

from odoo.http import request, route
from odoo.addons.website_sale.controllers.main import WebsiteSale

class WebsiteSaleProductComparison(WebsiteSale):

    @route('/shop/compare', type='http', auth='public', website=True, sitemap=False)
    def product_compare(self, **post):
        values = {}
        product_ids = [int(i) for i in post.get('products', '').split(',') if i.isdigit()]
        if not product_ids:
            return request.redirect("/shop")
        # use search to check read access on each record/ids
        products = request.env['product.product'].search([('id', 'in', product_ids)])
        values['products'] = products.with_context(display_default_code=False)
        return request.render("website_sale_comparison.product_compare", values)

    @route('/shop/get_product_data', type='json', auth='public', website=True)
    def get_product_data(self, product_ids, cookies=None):
        ret = {}

        website = request.env['website'].get_current_website()
        products = request.env['product.product'].search([('id', 'in', product_ids)])

        if cookies is not None:
            ret['cookies'] = json.dumps(request.env['product.product'].search([('id', 'in', list(set(product_ids + cookies)))]).ids)

        products = products.with_context(display_default_code=False)
        for product in products:
            ret[product.id] = {
                'render': request.env['ir.ui.view']._render_template(
                    "website_sale_comparison.product_product",
                    {'product': product, 'website': website}
                ),
                'product': dict(id=product.id, name=product.name, display_name=product.display_name),
            }
        return ret


import logging
from pathlib import Path

from farm.data_handler.processor import TextClassificationProcessor
from farm.modeling.tokenization import Tokenizer
from farm.utils import set_all_seeds
import torch

def test_processor_saving_loading(caplog):
    if caplog is not None:
        caplog.set_level(logging.CRITICAL)

    set_all_seeds(seed=42)
    lang_model = "bert-base-cased"

    tokenizer = Tokenizer.load(
        pretrained_model_name_or_path=lang_model, do_lower_case=False
    )

    processor = TextClassificationProcessor(tokenizer=tokenizer,
                                            max_seq_len=128,
                                            data_dir=Path("samples/doc_class"),
                                            train_filename="train-sample.tsv",
                                            dev_filename=None,
                                            test_filename=None,
                                            label_column_name="coarse_label",
                                            dev_split=0.1,
                                            label_list=["OTHER", "OFFENSE"],
                                            metric=["f1_macro"]
                                            )
    dicts = processor.file_to_dicts(file=Path("samples/doc_class/train-sample.tsv"))
    data, tensor_names, _ = processor.dataset_from_dicts(dicts)

    save_dir = Path("testsave/processor")
    processor.save(save_dir)

    processor = processor.load_from_dir(save_dir)
    dicts = processor.file_to_dicts(file=Path("samples/doc_class/train-sample.tsv"))
    data_loaded, tensor_names_loaded, _ = processor.dataset_from_dicts(dicts)

    assert tensor_names == tensor_names_loaded
    for i in range(len(data.tensors)):
        assert torch.all(torch.eq(data.tensors[i], data_loaded.tensors[i]))

if __name__ == "__main__":
    test_processor_saving_loading(None)


from typing import Any, Callable

import wrapt

import numba as nb
import numpy as np
import pandas as pd
from numba import prange
from pandas.api.types import infer_dtype

from anndata import AnnData

from cellrank import logging as logg

jit_kwargs = {"nogil": True, "cache": True, "fastmath": True}

@nb.njit(parallel=False, **jit_kwargs)
def _np_apply_along_axis(func1d, axis: int, arr: np.ndarray) -> np.ndarray:
    """Apply a reduction function over a given axis.

    Parameters
    ----------
    func1d
        Reduction function that operates only on 1 dimension.
    axis
        Axis over which to apply the reduction.
    arr
        The array to be reduced.

    Returns
    -------
    The reduced array.
    """
    assert arr.ndim == 2
    assert axis in [0, 1]

    if axis == 0:
        result = np.empty(arr.shape[1])
        for i in range(len(result)):
            result[i] = func1d(arr[:, i])
        return result

    result = np.empty(arr.shape[0])
    for i in range(len(result)):
        result[i] = func1d(arr[i, :])

    return result

@nb.njit(**jit_kwargs)
def np_mean(array: np.ndarray, axis: int) -> np.ndarray:  # noqa
    return _np_apply_along_axis(np.mean, axis, array)

@nb.njit(**jit_kwargs)
def np_std(array: np.ndarray, axis: int) -> np.ndarray:  # noqa
    return _np_apply_along_axis(np.std, axis, array)

@nb.njit(**jit_kwargs)
def norm(array: np.ndarray, axis: int) -> np.ndarray:  # noqa
    return _np_apply_along_axis(np.linalg.norm, axis, array)

# this is faster than using flat array
@nb.njit(parallel=True)
def _random_normal(
    m: np.ndarray,
    v: np.ndarray,
    n_samples: int = 1,
) -> np.ndarray:
    """Sample number from normal distribution.

    Parameters
    ----------
    m
        Mean vector.
    v
        Variance vector.
    n_samples
        Number of samples to be generated.

    Returns
    -------
    Array of shape ``(n_samples x m.shape[0])``.
    """
    assert m.ndim == 1, "Means are not 1-dimensional."
    assert m.shape == v.shape, "Means and variances have different shape."

    if n_samples == 1:
        return np.expand_dims(np.array([np.random.normal(m[i], v[i]) for i in prange(m.shape[0])]), 0)  # noqa: NPY002

    return np.array(
        [[np.random.normal(m[i], v[i]) for _ in prange(n_samples)] for i in prange(m.shape[0])]  # noqa: NPY002
    ).T

@nb.njit(**jit_kwargs)
def _calculate_starts(indptr: np.ndarray, ixs: np.ndarray) -> np.ndarray:
    """Get the position where to put the data.

    Parameters
    ----------
    indptr
        Pointer of indices from :class:`~scipy.sparse.csr_matrix`.
    ixs
        Row indices for which to calculate the starts.

    Returns
    -------
    The starting positions.
    """
    starts = np.cumsum(indptr[ixs + 1] - indptr[ixs])
    return np.hstack((np.array([0], dtype=starts.dtype), starts))

def _get_basis(adata: AnnData, basis: str) -> np.ndarray:
    try:
        return adata.obsm[f"X_{basis}"]
    except KeyError:
        try:
            return adata.obsm[basis]  # e.g. 'spatial'
        except KeyError:
            raise KeyError(f"Unable to find a basis in `adata.obsm['X_{basis}']` or `adata.obsm[{basis!r}]`.") from None

def _ensure_numeric_ordered(adata: AnnData, key: str) -> pd.Series:
    if key not in adata.obs.keys():
        raise KeyError(f"Unable to find data in `adata.obs[{key!r}]`.")

    exp_time = adata.obs[key].copy()
    if not np.issubdtype(np.asarray(exp_time).dtype, np.number):
        try:
            exp_time = np.asarray(exp_time).astype(float)
        except Exception as e:  # noqa: BLE001/Cannot interpret
            raise TypeError(
                f"Unable to convert `adata.obs[{key!r}]` of type `{infer_dtype(adata.obs[key])}` to `float`."
            ) from e

    if not isinstance(exp_time.dtype, pd.CategoricalDtype):
        logg.debug(f"Converting `adata.obs[{key!r}]` to `categorical`")
        exp_time = np.asarray(exp_time)
        categories = sorted(set(exp_time[~np.isnan(exp_time)]))
        if len(categories) > 100:  # arbitrary threshold
            raise ValueError(
                f"Unable to convert `adata.obs[{key!r}]` to `categorical` since it "
                f"would create `{len(categories)}` categories."
            )
        exp_time = pd.Series(
            pd.Categorical(
                exp_time,
                categories=categories,
                ordered=True,
            )
        )

    if not exp_time.cat.ordered:
        logg.warning("Categories are not ordered. Using ascending order")
        exp_time.cat = exp_time.cat.as_ordered()

    exp_time = pd.Series(pd.Categorical(exp_time, ordered=True), index=adata.obs_names)
    if exp_time.isnull().any():
        raise ValueError("Series contains NaN value(s).")

    n_cats = len(exp_time.cat.categories)
    if n_cats < 2:
        raise ValueError(f"Expected to find at least `2` categories, found `{n_cats}`.")

    return exp_time

@wrapt.decorator
def require_tmat(
    wrapped: Callable[..., Any],
    instance: "KernelExpression",  # noqa: F821
    args: Any,
    kwargs: Any,
) -> Any:
    """Require that the transition matrix is computed before calling the wrapped function."""
    # this can trigger combinations, but not individual kernels
    if instance.transition_matrix is None:
        raise RuntimeError("Compute transition matrix first as `.compute_transition_matrix()`.")
    return wrapped(*args, **kwargs)


"""Add img_url column to Project table

Revision ID: 1dcb912d6836
Revises: c50fa84f7100
Create Date: 2021-11-22 15:15:27.054109

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '1dcb912d6836'
down_revision = 'c50fa84f7100'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('project', sa.Column('img_url', sa.String(length=10000), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('project', 'img_url')
    # ### end Alembic commands ###


import json

python_dict = {
    "이름":"홍길동",
    "나이": 25,
    "거주지": "서울",
    "신체 정보": {
        "키" : 175.4,
        "몸무게" : 71.2
    },
    "취미": [
        "등산",
        "자전거 타기",
        "독서"
    ]

}
print(type(python_dict), python_dict)

json_data = json.dumps(python_dict) # json 형태로변환
print(type(json_data), json_data)

json_data = json.dumps(python_dict, sort_keys = True, indent = 4, ensure_ascii=False)
print(type(json_data), json_data)

python_dict2 = json.loads(json_data)
print(type(python_dict2), python_dict2)

student = [
    {
        'no': 1,
        'name': '김승영',
        'score': {'국어': 90, '영어': 90, '수학': 90}
    },
    {
        'no': 2,
        'name': '지재삼',
        'score': {'국어': 80, '영어': 79, '수학': 69}
    }
]

print(type(student))

#write
json_student = json.dumps(student) # json 형태로 변환
print(type(json_student))

with open('json_test.txt', 'w', encoding ='utf-8') as f:
    json.dump(json_student, f)  # json 파일 작성 한굴은 unicode형태로 저장

with open('json_test.txt', 'r', encoding ='utf-8') as f:
    x = json.load(f)    #json 형태의 파일 load
    print(type(x), x)

students = json.loads(x)
type(students)
for std in students:
    print("{}{} # {}{}{}"
            .format(type(std['no']),
                    type(std['name']),
                    type(std['score']['국어']),
                    type(std['score']['영어']),
                    type(std['score']['수학'])))
    print(std['no'], std['name'], end = ' ')
    total = sum([x for x in std['score'].values()])
    #total = std['score']['국어'] + std['score']['영어'] + std['score']['수학']
    [print(score, end=' ') for score in std['score'].values()]
    print(total, total /float(3))


import io
import pytest
import random
import string

from unittest import mock

import snarl.exc
import snarl.main

@pytest.fixture(scope='function')
def snarlobj():
    return snarl.main.Snarl()

def test_include_recursive(snarlobj):
    def doc():
        data = '\n'.join(('This is a test',
                          '',
                          '<!-- include recursive.snarl.md -->'
                          ))
        return io.StringIO(data)

    with mock.patch('snarl.main.open', create=True) as mock_open:
        mock_open.side_effect = lambda x, y: doc()
        with pytest.raises(snarl.exc.RecursiveIncludeError):
            snarlobj.parse(doc())

def test_include_simple(snarlobj):
    parent = '<!-- include simple.snarl.md -->\n'
    child = io.StringIO('included document')

    with mock.patch('snarl.main.open', create=True) as mock_open:
        mock_open.side_effect = lambda x, y: child
        snarlobj.fromstring(parent)
        assert 'included document' in '\n'.join(snarlobj.output)

def test_include_escape(snarlobj):
    parent = '<!-- include simple.snarl.md --escape-html -->\n'
    child = io.StringIO('<This is a test>')

    with mock.patch('snarl.main.open', create=True) as mock_open:
        mock_open.side_effect = lambda x, y: child
        snarlobj.fromstring(parent)
        assert '<This is a test>' in '\n'.join(snarlobj.output)

def test_include_verbatim(snarlobj):
    parent = '<!-- include simple.snarl.md --verbatim -->\n'
    child = io.StringIO('<!-- include testfile -->')

    with mock.patch('snarl.main.open', create=True) as mock_open:
        mock_open.side_effect = lambda x, y: child
        snarlobj.fromstring(parent)
        assert '<!-- include testfile -->' in '\n'.join(snarlobj.output)

def test_file(snarlobj):
    doc = '\n'.join(('```=block0',
                     f'This is block0.',
                     '```',
                     '```=output.txt --file',
                     '<<block0>>',
                     '```'))

    snarlobj.fromstring(doc)
    assert 'output.txt' in snarlobj.files()
    assert 'This is block0.' in ''.join(snarlobj.generate('output.txt'))

def test_lang_block(snarlobj):
    doc = '\n'.join(('```python:block0',
                     'print("This is a test")',
                     '```'))

    snarlobj.fromstring(doc)
    assert '```python' in '\n'.join(snarlobj.output)

def test_hide_block(snarlobj):
    doc = '\n'.join(('```=block0 --hide',
                     'This should not appear in weave output',
                     '```'))

    snarlobj.fromstring(doc)
    assert 'This should not appear in weave output' not in '\n'.join(snarlobj.output)

def test_block_escape(snarlobj):
    doc = '\n'.join(('```=block0 --escape-html',
                     '<This is a test>',
                     '```'))

    snarlobj.fromstring(doc)
    assert '<This is a test>' in '\n'.join(snarlobj.output)

def test_block_verbatim(snarlobj):
    doc = '\n'.join(('```=block0 --verbatim',
                     '<<This is a test>>',
                     '```'))

    snarlobj.fromstring(doc)
    assert '<<This is a test>>' in '\n'.join(snarlobj.output)

def test_block_unknown(snarlobj):
    doc = '\n'.join(('```=block0',
                     '<<This is a test>>',
                     '```'))

    snarlobj.fromstring(doc)
    with pytest.raises(KeyError):
        '\n'.join(snarlobj.generate('block0'))

def test_append_block(snarlobj):
    doc = '\n'.join(('```=block0',
                     'this is line1',
                     '```',
                     '```+=block0',
                     'this is line2',
                     '```'))
    snarlobj.fromstring(doc)
    assert 'this is line2' in '\n'.join(snarlobj.generate('block0'))

def test_replace(snarlobj):
    doc = '\n'.join(('```=block0 --replace gadgets gizmos',
                     'An article about gadgets.',
                     '```'))

    snarlobj.fromstring(doc)
    assert 'gizmos' in '\n'.join(snarlobj.generate('block0'))

def test_unknown_block_arg(snarlobj):
    doc = '\n'.join((
        '```=foo --unknown',
    ))

    with pytest.raises(snarl.exc.BlockArgumentError):
        snarlobj.fromstring(doc)

def test_tags(snarlobj):
    doc = '\n'.join(('```=block0 -t foo',
                     'This is block0',
                     '```',
                     '',
                     '```=block1 -t bar',
                     'This is block1',
                     '```'))

    snarlobj.fromstring(doc)
    assert 'block0' in snarlobj.blocks(tags=['foo'])
    assert 'block1' not in snarlobj.blocks(tags=['foo'])
    assert 'block1' in snarlobj.blocks(tags=['bar'])
    assert 'block0' not in snarlobj.blocks(tags=['bar'])

def test_unterminated_block(snarlobj):
    doc = '\n'.join((
        '```block0',
        'This block has no end marker.',
    ))

    with pytest.raises(snarl.exc.UnexpectedEOFError):
        snarlobj.fromstring(doc)


import os
import pandas as pd
from PIL import Image
import numpy as np
import warnings
warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data", UserWarning)
Image.MAX_IMAGE_PIXELS = None

def export_csv(root_imgs_path, csv_name):
    dic_categories = {'scenery' : [], 'furniture' : [], 'animal' : [], 'plant' : []}

    for path in os.listdir(root_imgs_path):
        c, name = path.split("_")
        if c in dic_categories.keys():
            dic_categories[c].append(name)

    # Show information
    total_objects = sum([len(dic_categories[c]) for c in dic_categories])
    # Object có số lượng ảnh lớn nhất
    max_len = max([len(dic_categories[c]) for c in dic_categories])
    print(f'total_objects = {total_objects}')
    print(f'object có nhiều ảnh nhất = {max_len}')

    # generate csv with pandas
    for c in dic_categories:
        dic_categories[c] += [""]*(max_len - len(dic_categories[c]))
    df = pd.DataFrame(dic_categories)
    df.to_csv("categories.csv", index=False)

    return dic_categories

def processing_data(images_path):
    dic_categories = {'animal' : [], 'plant' : [], 'furniture' : [], 'scenery' : []}
    count = 0

    for folder in os.listdir(images_path):
        if folder.split("_")[0] in dic_categories:
            path = images_path + folder
            list_dir = [path + '/' + name for name in os.listdir(path) if name.endswith((".jpg", ".png", ".jpeg"))]
            for p in list_dir:
                try:
                    #Step1:Open image, sử dụng Image của PIL để mở file ảnh theo path(biến p)
                    #thu được biến img chứa ảnh(lưu ý: biến thu được chứa ảnh dạng PIL)
                    img = Image.open(p)

                    #Step2: Verify image, Sau khi mở ảnh ở step1, thu được biến img chứa ảnh,
                    # .verify(): phát hiện ảnh lỗi
                    img.verify()

                    #Step3: Open image, Vì sau khi verify() hình ảnh sẽ bị đóng lại, vì vậy cần mở lại hình ảnh như Step1.
                    img = Image.open(p)

                    #Step4: Check width of image, nếu hình ảnh có width<10 thì xoá ảnh
                    if img.size[0] < 10:
                        os.remove(p)

                    #Step5: Only 3 channel image (color image), convert ảnh từ PIL sang numpy
                    #nếu hình ảnh có channel khác 3 thì xóa ảnh.
                    img = np.asarray(img)
                    if img.shape[2] != 3:
                        os.remove(p)

                except Exception as e:
                    print(e)
                    count += 1
                    print("error: ", p)
                    os.remove(p) # Các trường hợp ngoại lệ, ảnh lỗi,... sẽ bị xóa

if __name__ == '__main__':

    root_imgs_path = "./images/"
    csv_name = "categories.csv"
    dic_categories = export_csv(root_imgs_path, csv_name)

    root_imgs_path = "./images/"
    processing_data(images_path=root_imgs_path)


import os

import yaml

ATTRIBUTES = [('nesbi_username', ''),
              ('nesbi_password', ''),
              ('nesbi_dry_run', False),
              ('nesbi_network_driver', ''),
              ('nsot_url', ''),
              ('nsot_email', ''),
              ('nsot_secret_key', ''),
              ('nsot_auth_header', 'X-NSoT-Email'),
              ('nsot_site_id', '1'),
              ('nsot_delete_objects', False),
              ('nesbi_snmp_version', '2c'),
              ('nesbi_snmp_community', ''),
              ('nesbi_scan_ports', [22]),
              ('nesbi_logging_level', 'info'),
              ('nesbi_logging_file', 'nesbi.log'),
              ('nesbi_logging_to_stdout', False),
              ('nesbi_thread_limit', 5)]

class Config:
    def __init__(self, config_file, **kwargs):
        with open(config_file, 'r') as f:
            data = yaml.safe_load(f.read()) or {}

        for k, v in data.items():
            setattr(self, k, v)

        for attr in ATTRIBUTES:
            self._set_self_attribute(attr[0], attr[1], **kwargs)

    def _set_self_attribute(self, attr, default, **kwargs):
        if kwargs.get(attr):
            setattr(self, attr, kwargs.get(attr))
        elif hasattr(self, attr):
            setattr(self, attr, getattr(self, attr))
        elif attr.upper() in os.environ:
            setattr(self, attr, os.environ.get(attr.upper()))
        else:
            setattr(self, attr, default)


"""
Checks to see if Makefile follows standards
"""
import os
import re

import pytest
from pytest_repo_health import health_metadata

from repo_health import get_file_content

module_dict_key = "makefile"
output_keys = {
    "upgrade": "target that upgrades our dependencies to newer released versions",
    "test": "target that runs tests",
    "quality": "target that runs code quality checks",
    "test-js": "target that runs javascript unit tests",
    "quality-js": "target that runs javascript code quality checks",
    "test-python": "target that runs python unit tests",
    "quality-python": "target that runs python code quality checks",
}

@pytest.fixture(name='makefile')
def fixture_makefile(repo_path):
    """Fixture containing the text content of Makefile"""
    full_path = os.path.join(repo_path, "Makefile")
    return get_file_content(full_path)

@health_metadata(
    [module_dict_key, "has_target"],
    output_keys
)
def check_has_make_target(makefile, all_results):
    """
    Checks make file has provided targets
    """
    for target, __ in output_keys.items():
        all_results[module_dict_key][target] = False
        regex_pattern = "".join(["^", target, ":"])
        match = re.search(regex_pattern, makefile, re.MULTILINE)
        if match:
            all_results[module_dict_key][target] = True

@health_metadata(
    [module_dict_key],
    {
        "pip-installed": "check if pip.txt was installed immediately after upgrade"
    }
)
def check_upgrade_script(makefile, all_results):
    """
    Checks if pip installed after upgrading pip.txt
    """
    upgrade_targets = re.finditer("^upgrade:", makefile, re.MULTILINE)

    for i in upgrade_targets:
        upgrade_script = makefile[i.end():]
        next_target = re.search("^[a-zA-Z_]+:", upgrade_script, re.MULTILINE)
        if next_target is not None:
            upgrade_script = upgrade_script[:next_target.start()]
        update_commands = (r"(\n\t(\$\(PIP_COMPILE\)|pip-compile)(.*?)((requirements/pip\.txt requirements/pip\.in)"
                           r"|(requirements/pip-tools\.txt requirements/pip-tools\.in))){2}")
        install_commands = r"(\n\t(pip install)(.*?)(requirements/pip.txt|requirements/pip-tools.txt)){2}"
        regex_pattern = "".join([update_commands, install_commands])
        match = re.search(regex_pattern, upgrade_script, re.MULTILINE)
        if match:
            all_results[module_dict_key]["pip-installed"] = True
            return

    all_results[module_dict_key]["pip-installed"] = False


import numpy as np
import h5py
from collections import defaultdict
from typing import List

from june.epidemiology.infection import (
    TransmissionGamma,
    Transmission,
    TransmissionConstant,
    TransmissionXNExp,
)
from june.hdf5_savers.utils import read_dataset, write_dataset

str_to_class = {
    "TransmissionXNExp": TransmissionXNExp,
    "TransmissionGamma": TransmissionGamma,
    "TransmissionConstant": TransmissionConstant,
}
attributes_to_save_dict = {
    "TransmissionXNExp": ["time_first_infectious", "norm_time", "n", "norm", "alpha"],
    "TransmissionGamma": ["shape", "shift", "scale", "norm"],
    "TransmissionConstant": ["probability"],
}

def save_transmissions_to_hdf5(
    hdf5_file_path: str, transmissions: List[Transmission], chunk_size: int = 50000
):
    """
    Saves transmissions data to hdf5. The transmission type is inferred from the first
    element of the list.

    Parameters
    ----------
    attributes_to_save
        attributes to save from each transmission
    hdf5_file_path
        hdf5 path to save transmissions
    transmissions
        list of transmission objects
    chunk_size
        number of hdf5 chunks to use while saving
    """
    with h5py.File(hdf5_file_path, "a") as f:
        if "infections" not in f:
            f.create_group("infections")
        f["infections"].create_group("transmissions")
        transmissions_group = f["infections"]["transmissions"]
        n_transsmissions = len(transmissions)
        transmissions_group.attrs["n_transsmissions"] = n_transsmissions
        transmission_type = transmissions[0].__class__.__name__
        transmissions_group.attrs["transmission_type"] = transmission_type
        n_chunks = int(np.ceil(n_transsmissions / chunk_size))
        attributes_to_save = attributes_to_save_dict[transmission_type]
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_transsmissions)
            attribute_dict = defaultdict(list)
            for index in range(idx1, idx2):
                transmission = transmissions[index]
                for attribute_name in attributes_to_save:
                    attribute = getattr(transmission, attribute_name)
                    if attribute is None:
                        attribute_dict[attribute_name].append(np.nan)
                    else:
                        attribute_dict[attribute_name].append(attribute)
            for attribute_name in attributes_to_save:
                attribute_dict[attribute_name] = np.array(
                    attribute_dict[attribute_name], dtype=np.float64
                )
            for attribute_name in attributes_to_save:
                write_dataset(
                    group=transmissions_group,
                    dataset_name=attribute_name,
                    data=attribute_dict[attribute_name],
                    index1=idx1,
                    index2=idx2,
                )

def load_transmissions_from_hdf5(hdf5_file_path: str, chunk_size=50000):
    """
    Loads transmissions data from hdf5.

    Parameters
    ----------
    hdf5_file_path
        hdf5 path to load from
    chunk_size
        number of hdf5 chunks to use while loading
    """
    transmissions = []
    with h5py.File(hdf5_file_path, "r") as f:
        transmissions_group = f["infections"]["transmissions"]
        n_transsmissions = transmissions_group.attrs["n_transsmissions"]
        transmission_type = transmissions_group.attrs["transmission_type"]
        transmission_class = str_to_class[transmission_type]
        n_chunks = int(np.ceil(n_transsmissions / chunk_size))
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_transsmissions)
            attribute_dict = {}
            for attribute_name in transmissions_group.keys():
                attribute_dict[attribute_name] = read_dataset(
                    transmissions_group[attribute_name], idx1, idx2
                )
            for index in range(idx2 - idx1):
                transmission = transmission_class()
                for attribute_name in attribute_dict:
                    attribute_value = attribute_dict[attribute_name][index]
                    if attribute_value == np.nan:
                        attribute_value = None
                    setattr(transmission, attribute_name, attribute_value)
                transmissions.append(transmission)
    return transmissions


import requests
import db_connector
import sys

user_id = sys.argv[1]

#  Creating the user with a POST
post = requests.post(f'http://127.0.0.1:5000/data/{user_id}', json={"user_name": "oriel"})
if post.ok:
    print('User Created:', post.json())
else:
        print('response code 500:', post.json())

# Checking the user exists with a GET
try:
    get = requests.get(f'http://127.0.0.1:5000/data/{user_id}')
    if get.ok:
        print('User verified and exists:', get.json())
    else:
        print('response code 500:', get.json())
except:
    print('No access to server with GET')

#  Login to the DB and pull the name of the user
# print(add_user(user_id))


import websockets
import asyncio
import base64

PORT = 5050

print(f"Server listening in Port {PORT}")
connected = set()

async def echo(webs, path):
    print('A client is connected')
    connected.add(webs)
    try:
        async for message in webs:
            for conn in connected:
                if conn != webs:
                    print('\n\nReceived msg from cliend: \n', message)
                    await conn.send(message)
    except websockets.exceptions.ConnectionClosed as e:
        print("A client just disconnected.")
    finally:
        connected.remove(webs)

start_server = websockets.serve(echo, "0.0.0.0", PORT)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


#
# Copyright (c) 2023 Thibaut Zeissloff.
#
# This file is part of Thumb2ISS
# (see https://github.com/TZe-0xff/thumb2ISS).
#
# License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
#
import logging, struct
from _testing import Core, test
logging.basicConfig(level=logging.DEBUG)
c = Core(profile=True)

 # 0 	:	e8df f000 	tbb	[pc, r0]
 # 4  	:	d5280e04 	.word	0x07060504
 # 8 	:	8461ab9f 	.word	0x8461ab9f

initial_mem = {i:struct.pack('B',i) for i in range(128)}

for add in range(8):
	test(c, [c.getExec('tbb', 'tbb [pc, r0]', 0)], initial_mem, intial_regs={0: add , 13:0x20001000, 15:0})
	assert(c.UInt(c.R[15]) == 0x4 + 2*(4+add))

initial_mem = {i:struct.pack('B',i) for i in range(128) if i%2==0}
initial_mem.update({i:b'\x00' for i in range(128) if i%2==1})

for add in range(8):
	test(c, [c.getExec('tbh', 'tbh [pc, r0, lsl #1]', 0)], initial_mem, intial_regs={0: add , 13:0x20001000, 15:0})
	assert(c.UInt(c.R[15]) == 0x4 + 2*(4+2*add))


from flask import Flask,render_template,request

app = Flask(__name__)

def calc_tax(v,rate):
    try:
        rs = str(int(float(v)*(1 + rate)))
    except:
        print('calc error')
        rs = "計算できませんでした。"
    return rs

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/health-check", methods=["GET"])
def health_check():
    return "ok"

@app.route("/std", methods=["POST"])
def stdtaxinc():
    print(request.get_data())
    return calc_tax(request.get_data(), 0.1)

@app.route("/red", methods=["POST"])
def reducedtaxinc():
    print(request.get_data())
    return calc_tax(request.get_data(), 0.08)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)


from math import ceil, cos, radians, sin
from PIL import Image, ImageDraw, ImageFont

vu_navy =   (  0,  31,  91)
vu_yellow = (255, 220,  79)
vu_orange = (248, 151,  40)
vu_green =  (193, 205,  35)
vu_blue =   ( 80, 145, 205)
vu_teal =   (  0, 146, 143)
vu_gray=    (148, 156, 166)

colors = [vu_navy, vu_blue, vu_orange, vu_yellow]

def comma(num):
    return '{:,}'.format(num)

def smart_text(draw, text, x, y, font, align="left", fillColor=(255, 255, 255), border=vu_navy):
    draw.text((x - 1, y), text, fill=border, font=font, align=align)
    draw.text((x, y - 1), text, fill=border, font=font, align=align)
    draw.text((x + 1, y), text, fill=border, font=font, align=align)
    draw.text((x, y + 1), text, fill=border, font=font, align=align)
    draw.text((x - 1, y + 1), text, fill=border, font=font, align=align)
    draw.text((x - 1, y - 1), text, fill=border, font=font, align=align)
    draw.text((x + 1, y + 1), text, fill=border, font=font, align=align)
    draw.text((x + 1, y - 1), text, fill=border, font=font, align=align)
    draw.text((x, y), text, fill=fillColor, font=font, align=align)

def bar_graph(filename, data, title=None, key=None, labels=None, numbers=True):
    bar_width = 60
    bar_gap = 15
    bar_border = 1
    padding = 20
    width = ((bar_width + bar_gap) * len(data)) - bar_gap + padding * 2
    height = 600

    im = Image.new("RGB", (width, height), (255, 255, 255))
    draw = ImageDraw.Draw(im)
    gotham_font = ImageFont.truetype("fonts/opensans-semibold-webfont.ttf", 10)

    bottom_pad = 0
    if labels != None:
        for l in labels:
            _, th = draw.textsize(l, font=gotham_font, spacing=2)
            if th > bottom_pad:
                bottom_pad = th
    bottom = height - (padding / 2) - bottom_pad

    x = padding
    max = 0
    for arr in data:
        for d in arr:
            if d > max:
                max = d
    bar = 0
    totals = []
    for i in data[0]:
        totals.append(0)
    for arr in data:
        arr.sort(reverse=True)
        index = 0
        for d in arr:
            totals[index] += d
            if d == 0:
                continue
            bar_h = (height - bottom_pad - padding * 2) * (d / max)
            if index == 0:
                box = (
                    x - bar_border,
                    bottom - bar_h - bar_border,
                    x + bar_width + bar_border,
                    bottom + bar_border
                )
                draw.rectangle(box, fill=(0, 0, 0))
                if numbers:
                    c_arr = []
                    c_colors = []
                    for i in range(len(arr)):
                        if arr[i] > 0:
                            c_arr.append(arr[i])
                            c_colors.append(colors[i])
                    stats = ", ".join([comma(d) for d in c_arr])
                    _, h = draw.textsize("0,", font=gotham_font)
                    w, _ = draw.textsize(stats, font=gotham_font)
                    for i in range(len(c_arr)):
                        stats = ", ".join([comma(d) for d in c_arr[i:]])
                        draw.text((x + bar_width + bar_border - w, bottom - bar_h - h), comma(c_arr[i]), fill=c_colors[i], spacing=2, font=gotham_font)
                        if i == len(c_arr) - 1:
                            break
                        sub_w, _ = draw.textsize(comma(c_arr[i]), font=gotham_font)
                        w -= sub_w
                        draw.text((x + bar_width + bar_border - w, bottom - bar_h - h), ", ", fill=c_colors[0], spacing=2, font=gotham_font)
                        sub_w, _ = draw.textsize(", ", font=gotham_font)
                        w -= sub_w
            box = (
                x,
                bottom - bar_h,
                x + bar_width,
                bottom
            )
            draw.rectangle(box, fill=(colors[index]))
            index += 1
        if labels != None:
            draw.text((x, bottom + 3), labels[bar], fill=(0, 0, 0), spacing=2, font=gotham_font)
        x += bar_width + bar_gap
        bar += 1

    # Titles
    if title != None:
        gotham_title = ImageFont.truetype("fonts/opensans-semibold-webfont.ttf", 24)
        title_w, title_h = draw.textsize(title, font=gotham_title)
        draw.text((width - title_w - padding, padding), title, fill=(0, 0, 0), font=gotham_title)

    # Key
    if key != None:
        y = padding + 28
        max_w = 0
        max_h = 0
        for k in key:
            w, h = draw.textsize(k, font=gotham_font)
            if w > max_w:
                max_w = w
            if h > max_h:
                max_h = h
        for i in range(len(key)):
            # Bar
            draw.rectangle((width - max_w - padding - 20, y, width - padding, y + max_h + 9), fill=(colors[i]))
            smart_text(draw, key[i], width - max_w - padding - 10, y + 5, gotham_font)
            # Totals
            total_text = comma(totals[i])
            if i > 0:
                total_text += " (%.1f%%)" % (100 * totals[i] / totals[0])
            total_w, _ = draw.textsize(total_text, font=gotham_font)
            draw.text((width - total_w - max_w - padding - 25, y + 5), total_text, fill=(colors[i]), font=gotham_font)
            y += max_h + 9 + 5

    del draw
    im.save(filename)

def pie_chart(filename, data, gap=0):
    padding = 100
    width = height = 600 + padding
    full_width = width + padding

    im = Image.new("RGB", (full_width, full_width), (255, 255, 255))
    draw = ImageDraw.Draw(im)

    gotham_title = ImageFont.truetype("fonts/opensans-semibold-webfont.ttf", 48)
    title_w, title_h = draw.textsize(data["title"], font=gotham_title)
    draw.text(((padding + width - title_w) / 2, (padding - title_h) / 2), data["title"], fill=(0, 0, 0), font=gotham_title)

    levels = {}
    level_step = 50
    label_text = []
    label_colors = []
    for arc in data["arcs"]:
        label_text.append(arc["label"])
        label_colors.append(arc["color"])

        if not arc["level"] in levels:
            levels[arc["level"]] = -90
        perc = (360 * arc["value"] / data["total"])
        mid_angle_rad = radians(levels[arc["level"]] + perc / 2)
        shrink = arc["level"] * level_step
        arc_xy = (padding + shrink + gap * cos(mid_angle_rad),padding + shrink + gap * sin(mid_angle_rad) , width - shrink,height - shrink)
        draw.pieslice(arc_xy, levels[arc["level"]], levels[arc["level"]] + perc, fill=arc["color"])
        levels[arc["level"]] += perc

    gotham_medium = ImageFont.truetype("fonts/opensans-semibold-webfont.ttf", 12)
    _, line_height = draw.textsize("0g", font=gotham_medium)
    box_width = full_width / len(label_text)
    box_height = line_height * 4
    x = 0
    y = full_width - box_height
    for l in range(len(label_text)):
        draw.rectangle((x, y, x + box_width, y + box_height), fill=(label_colors[l]))
        text_w, text_h = draw.textsize(label_text[l], font=gotham_medium)
        smart_text(draw, label_text[l], x + (box_width - text_w) / 2, y + 2 + (box_height - text_h) / 2, border=(100,100,100), font=gotham_medium, align="center")
        x += box_width
    # draw.text(((padding + width - title_w) / 2, (padding - title_h) / 2), data["title"], fill=(0, 0, 0), font=gotham_title)

    del draw
    im.save(filename)

def box_chart(filename, data):
    width = height = 800
    gutter = 50
    im = Image.new("RGB", (width, height + gutter), (200, 200, 200))
    draw = ImageDraw.Draw(im)
    gotham_title = ImageFont.truetype("fonts/opensans-semibold-webfont.ttf", 24)

    data_total = 0
    min_width = 3
    min_value = (min_width + 1) * data["total"] / width
    for box in data["values"]:
        box_total = 0
        for v in box:
            box_total += v["value"]
        if box_total == 0:
            continue
        if box_total < min_value:
            data_total += min_value
        else:
            data_total += box_total

    pos_y = gutter
    pos_list = []
    sub_bar_pos = []
    for box in data["values"]:
        box_total = 0
        for v in box:
            box_total += v["value"]
        if box_total == 0:
            pos_list.append(pos_y)
            continue
        box_h = width * max(min_value, box_total) / data_total
        min_h = max(min_value, box_total) / box_h
        if len(box) == 1:
            draw.rectangle((0,pos_y , width,pos_y + box_h), fill=(box[0]["color"]))
        else:
            pos_x = 0
            for i in range(len(box)):
                v = box[i]
                if v["value"] == 0:
                    sub_bar_pos.append(pos_x)
                    continue
                link_gap = (len(data["key"]) - i) * 2
                box_w = width * max(min_h, v["value"]) / box_total
                draw.rectangle((pos_x,pos_y - link_gap , pos_x + box_w,pos_y + box_h + link_gap), fill=(v["color"]))
                sub_bar_pos.append(pos_x)
                pos_x += box_w
            sub_bar_pos.append(width)
        pos_y += box_h
        pos_list.append(pos_y)
    # Labels
    prev_label_y = 0
    for i in range(len(data["values"])):
        box = data["values"][i]
        if len(box) != 1:
            continue
        if box[0]["value"] == 0:
            continue
        label_w, label_h = draw.textsize(box[0]["label"], font=gotham_title)
        label_y = max(prev_label_y, pos_list[i] - label_h - 1)
        smart_text(draw, box[0]["label"], (width - label_w) / 2, label_y, font=gotham_title, fillColor=(0, 0, 0), border=box[0]["color"])
        prev_label_y = label_y + label_h + 1

    # chart_box = (0, 0, width, height)
    # region = im.crop(chart_box)
    # region = region.rotate(90)
    # im.paste(region, chart_box)

    gotham_label = ImageFont.truetype("fonts/opensans-semibold-webfont.ttf", 12)
    # draw.rectangle((0,0 , width,gutter), fill=(0, 0, 0))
    pos_x = 0
    gap = 2
    key_w = width / len(data["key"])
    for i in range(len(data["key"])):
        key = data["key"][i]
        # Rect
        draw.rectangle((pos_x,0, pos_x + key_w,gutter), fill=key["color"])
        # Gap Line
        if i == 1:
            draw.rectangle((sub_bar_pos[i],gutter - gap , pos_x + key_w  - 1,gutter), fill=key["color"])
            # Dividers
            draw.rectangle((sub_bar_pos[i],gutter - gap - 1 , pos_x  - 1,gutter - gap - 1), fill=(0, 0, 0))
            draw.rectangle((sub_bar_pos[i + 1],gutter , pos_x + key_w  - 1,gutter), fill=(0, 0, 0))
            if sub_bar_pos[i] > 2:
                draw.rectangle((sub_bar_pos[i] - 1,gutter - gap - 1 , sub_bar_pos[i] - 1,gutter), fill=(0, 0, 0))

        # Label
        key_label_w, key_label_h = draw.textsize(key["label"], font=gotham_label)
        smart_text(
            draw, key["label"],
            pos_x + (key_w - key_label_w) / 2, (gutter - key_label_h) / 2,
            font=gotham_label, align="center"
        )
        pos_x += key_w

    del draw
    im.save(filename)


import cv2
import numpy
import PIL
import datetime

def count_Faces(frame):
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')  # Load the Haar Cascade Classifier for face detection
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Convert the frame
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)  # Use the detectMultiScale method to capture faces
    # Get the number of detected faces
    num_faces = len(faces)
    cv2.putText(frame, f'Number of Faces: {num_faces}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)  # Display the face count
    return num_faces

def Face_Cascade(frame):
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # Load the Haar Cascade Classifier for face detection
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert the frame
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # Use the detectMultiScale method to capture faces
    times = []
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # Annotate the faces by drawing rectangles

        cv2.putText(frame, f'Face: ({x}, {y})', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) #text with coordinates on the detected face

        # extract the region of interest (ROI)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = frame[y:y + h, x:x + w]
    cv2.imshow('Annotated Frame', frame) # Display the frame
    return frame, faces

if __name__=='__main__':
    cap = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('StudentName+TestId.avi', fourcc, 20.0, (640, 480))
    print(cap.isOpened())
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret == True:
            Face_Cascade(frame)
            count_Faces(frame)
            out.write(frame)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            cv2.imshow('frame', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            pass
    cap.release()
    out.release()
    cv2.destroyAllWindows()


import cv2
import numpy as np
import face_recognition
import os, csv
from datetime import datetime
from pathlib import Path

path = 'ImageDatabase'  # path of the folder where all sample images are present
images = []     # an empyt array
classNames = []
myList = os.listdir(path)   # it will fetch the name of the images ex - [Alba Test.jpg, Alba.jpg, Gal Gadot.jpg, Lulu.jpg]
print(myList)

# this loop will fetch image name from fath folder and append it to the array(classNames)

for cl in myList:
    curImg = cv2.imread(f'{path}/{cl}')
    images.append(curImg)
    classNames.append(os.path.splitext(cl)[0])
    # we dont want Name like - Alba.jpg,
    # we just want Alba so we use split function on the name an then reciving the first index
    # i.e. only "Alba" and removing .jpg
print(classNames)

# Meaning of Encoding
# Encoding is the process of converting data from one form to another.
# By encoding digital audio, video, and image files, they can be saved in a more efficient, compressed format.
# Encoded media files are typically similar in quality to their original uncompressed counterparts, but have much smaller file sizes

def findEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encodeList.append(encode)
    return encodeList

def check_dir():
    file_path = Path("./1.Entry")
    csv_file = 'EntryData_' + str(datetime.now().strftime('%d_%m_%Y')) + '.csv' # it will include the current date of the system in the file name
    csv_file_full = file_path / csv_file
    if csv_file_full.is_file():
        direxist = csv_file_full
        #print('file exist')
        return direxist
    else:
        with open(csv_file_full,'w', newline='') as cd:
            header = ['Name', 'Time']
            writer = csv.writer(cd,delimiter=',')
            writer.writerow(header)
            dircreated = csv_file_full
            #print('just created')
            return dircreated

 # this function will check for filename (csv_file) in the following dir (file_path)
 # if it will find the file it will return the full path of the file (cvs_file_full)
 # and if not it will creat one and then return the full path of the file

def Biometric_scan(name):
    filename = check_dir()
    with open(filename,'r+') as f:  # r+ == reading and writing the file at same time
        myDataList = f.readlines()
        nameList = []
        for line in myDataList:
            entry = line.split(',')
            nameList.append(entry[0])
        if name not in nameList:
            now = datetime.now()
            dtString = now.strftime('%H:%M:%S')
            f.writelines(f'\n{name},{dtString}')

encodeListKnown = findEncodings(images)
print('Encoding Complete')

cap = cv2.VideoCapture(0)

while True:
    success, img1 = cap.read()
    img = cv2.flip(img1,1)
    imgS = cv2.resize(img,(0,0),None,0.25,0.25) # reducing the size of the image by one forth
    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

    facesCurFrame = face_recognition.face_locations(imgS)  # facesCurFrame == faces in our current frame
    encodesCurFrame = face_recognition.face_encodings(imgS,facesCurFrame) # encodesCurFrame == encoding in our current frame

    for encodeFace,faceLoc in zip(encodesCurFrame,facesCurFrame):
        matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
        faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
        #print(faceDis)
        matchIndex = np.argmin(faceDis)

        if matches[matchIndex]:
            name = classNames[matchIndex].upper()
            #print(name)
            Biometric_scan(name)
        if faceDis[matchIndex]< 0.60:
            Biometric_scan(name)
        else:
            name = 'Unknown'
            #print(name)
        if name == 'Unknown':
            y1,x2,y2,x1 = faceLoc
            y1, x2, y2, x1 = y1*4,x2*4,y2*4,x1*4
            # as we have resize our image in line 84, so if we want the rectangle around the face to be align properly,
            # we have to multiply the coordinates by 4
            cv2.rectangle(img,(x1,y1),(x2,y2),(0,0,255),2)
            cv2.rectangle(img,(x1,y2-35),(x2,y2),(0,0,255),cv2.FILLED)
            # it will create a rectangular box under the frace box which will show the name. y-35 - it is for decreasing the lenght of the box/rectangle
            cv2.putText(img,name,(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
            # (x1+6,y2-6) - for aligning the text in to the small box which will show the name
        else:
            y1,x2,y2,x1 = faceLoc
            y1, x2, y2, x1 = y1*4,x2*4,y2*4,x1*4
            cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
            cv2.rectangle(img,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
            cv2.putText(img,name,(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)

    cv2.imshow('Biometric_scan Cam',img)
    key = cv2.waitKey(1)
    if key == 27:
        break
cv2.destroyAllWindows()


if False:
  from data.word_api.wordnik import _word_api
  from spec.mamba import *

with _description('_word_api'):
  with description('synonyms'):
    with it('executes without error'):
      expect(calling(_word_api.synonyms, 'string')).not_to(raise_error)

    with it('returns synonyms'):
      results = _word_api.synonyms('string')
      expect(results).to(contain('fiber', 'rope', 'cord', 'thread'))

  with description('hypernyms'):
    with it('executes without error'):
      expect(calling(_word_api.hypernyms, 'orange')).not_to(raise_error)

    with it('returns synonyms'):
      results = _word_api.hypernyms('orange')
      expect(results).to(contain('pigment'))


import matplotlib.pyplot as plt

COLORS = 'bgmck'

def read_file(fn):
    with open(f'data/{fn}', 'r') as f:
        return list(map(lambda x: int(float(x)), f.read().split()))

def write_data(fn, data):
    with open(f'data/{fn}', 'w') as f:
        f.write('\n'.join([str(x) for x in data]))

def draw(fn, setpoint=700):
    multi_draw([fn], setpoint)

def multi_draw(fns, setpoint=700):
    longest = 0
    for i, fn in enumerate(fns):
        ypoints = read_file(fn)
        xpoints = range(len(ypoints))
        longest = max(longest, len(ypoints))
        plt.plot(xpoints, ypoints, color=COLORS[i % len(COLORS)])

    xpoints = range(longest)
    plt.plot(xpoints, [setpoint] * len(xpoints), color='r')
    plt.savefig(f'data/{fn}.png')
    plt.show()

def apply_filter(fn, nfn, filter_func):
    input_data = read_file(fn)
    output_data = list(filter_func(input_data))
    write_data(nfn, output_data)


"""Takes fs-essentia-extractor_legacy embeddings, and to implements
the Gaia feature preprocessing with Python. Namely, select a subset
 of the features, normalizes each of them independently and applies
 dimensionality reduction with PCA."""

import os
import time
import glob
import yaml
import json
from pathlib import Path
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser

import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA

from lib.utils import get_fname
from lib.directories import AUDIO_DIR

# Use these statistics for each feature
PCA_DESCRIPTORS = [
    "mean",
    "dmean",
    "dmean2",
    "var",
    "dvar",
    "dvar2"
]

# Features that are multiple band
MBAND_FEATURES = [
    "barkbands",
    "erb_bands",
    "frequency_bands",
    "gfcc",
    "mfcc",
    "scvalleys",
    "spectral_contrast"
]

def load_yaml(path):
    return yaml.safe_load(Path(path).read_text())

def select_subset(output):
    """ Selects a determined subset from a large set of features"""

    # For multiband features, collect PCA_DESCRIPTORS statistics of each band separately
    mband_feats = {}
    for feat in MBAND_FEATURES:
        n_bands = len(output["lowlevel"][feat][PCA_DESCRIPTORS[0]]) # Get the Number of bands
        for i in range(n_bands): # Access each band
            mband_feats[f"{feat}_{i}"] = {}
            for stat in PCA_DESCRIPTORS:
                mband_feats[f"{feat}_{i}"][stat] = output["lowlevel"][feat][stat][i]
        del output["lowlevel"][feat]
    # Insert the collection to the rest of the lowlevel features
    for k,v in mband_feats.items():
        output["lowlevel"][k] = v
    # Select the subset of features
    embed = {}
    for feat,feat_dct in output["lowlevel"].items():
        if type(feat_dct) == dict:
            embed[feat] = []
            for stat in PCA_DESCRIPTORS:
                embed[feat].append(feat_dct[stat])
    return embed

# TODO: whiten PCA??
if __name__=="__main__":

    parser=ArgumentParser(description=__doc__,
                                   formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('embed_dir',
                        type=str,
                        help='Directory containing fs-essentia-extractor_legacy embeddings.')
    parser.add_argument("-N",
                        type=int,
                        default=100,
                        help="Number of PCA components to keep.")
    parser.add_argument('--plot-scree',
                        action='store_true',
                        help="Plot variance contributions of PCA components.")
    parser.add_argument("--output-dir",
                        type=str,
                        default="",
                        help="Path to output directory. If not provided, "
                        "a directory will be created in the same directory "
                        "as the embed_dir.")
    args = parser.parse_args()

    # Read all the embeddins
    embed_paths = glob.glob(os.path.join(args.embed_dir, "*.yaml"))
    print(f"{len(embed_paths)} embeddings found.")

    # Create the initial embeddings from model outputs
    print("Creating the initial embeddings...")
    start_time = time.time()
    fnames,embeddings = [],[]
    for i,embed_path in enumerate(embed_paths):
        if (i+1)%1000==0:
            print(f"Processed {i} embeddings...")
        # Get the fname from the path
        fnames += [get_fname(embed_path).split("-")[0]]
        # Load the features and select the subset
        feat_dict = load_yaml(embed_path)
        embeddings += [select_subset(feat_dict)]
    total_time = time.time()-start_time
    print(f"Total time: {time.strftime('%M:%S', time.gmtime(total_time))}")

    # List of all included features
    SUBSET_KEYS = list(embeddings[0].keys())
    print(f"{len(SUBSET_KEYS)} features selected.")

    # Create and store a Scaler for each feature
    print("Fitting scalers for each feature...")
    start_time = time.time()
    scalers = []
    for feat in SUBSET_KEYS:
        # Create the Data Matrix
        data = np.array([embed[feat] for embed in embeddings])
        scaler = MinMaxScaler()
        scaler.fit(data)
        scalers.append((feat,scaler))
    total_time = time.time()-start_time
    print(f"Total time: {time.strftime('%M:%S', time.gmtime(total_time))}")

    # Normalize each feature independently
    print("Normalizing each feature independently...")
    start_time = time.time()
    for i in range(len(embeddings)):
        for key,scaler in scalers:
            d = np.array(embeddings[i][key]).reshape(1,-1)
            embeddings[i][key] = scaler.transform(d).reshape(-1)
    total_time = time.time()-start_time
    print(f"Total time: {time.strftime('%M:%S', time.gmtime(total_time))}")

    # Concat all normalized features, make sure same order is followed
    print("Concatanating all the features....")
    start_time = time.time()
    for i in range(len(embeddings)):
        embeddings[i] = np.array([embeddings[i][k] for k in SUBSET_KEYS]).reshape(-1)
    embeddings = np.array(embeddings)
    total_time = time.time()-start_time
    print(f"Total time: {time.strftime('%M:%S', time.gmtime(total_time))}")

    # Determine PCA components
    n_components = args.N if args.N!=-1 else embeddings.shape[1]

    # Create the output dir
    if args.output_dir == "":
        output_dir = f"{args.embed_dir}-PCA_{n_components}"
    else:
        output_dir = os.path.join(args.output_dir, os.path.basename(args.embed_dir))
    os.makedirs(output_dir, exist_ok=True)
    print(f"Exporting the embeddings to: {output_dir}")

    # Scree plot
    if args.plot_scree:
        print(f"Plotting the PCA Scree plot next to the embeddings...")
        import matplotlib.pyplot as plt
        model = os.path.basename(args.embed_dir)
        data = os.path.basename(os.path.dirname(args.embed_dir))
        title=f'FSD50K.{data} - {model} Embeddings PCA Scree Plot'
        pca = PCA(n_components=None, copy=True)
        pca.fit(embeddings)
        PC_values = np.arange(pca.n_components_) + 1
        cumsum_variance = 100*np.cumsum(pca.explained_variance_ratio_)
        fig,ax = plt.subplots(figsize=(15,8), constrained_layout=True)
        fig.suptitle(title, fontsize=20)
        ax.plot(PC_values, cumsum_variance, 'ro-', linewidth=2)
        ax.set_xlim([-5,len(PC_values)+5])
        ax.set_yticks(np.arange(0,105,5)) # 5% increase
        ax.set_xlabel('Number of Principal Components Selected', fontsize=15)
        ax.set_ylabel('% Cumulative Variance Explained', fontsize=15)
        ax.grid()
        figure_path = os.path.join(output_dir, f'FSD50K.{data}-{model}-scree_plot.jpeg')
        fig.savefig(figure_path)

    # Apply PCA if specified
    if args.N!=-1:
        print("Applying PCA to each embedding...")
        start_time = time.time()
        pca = PCA(n_components=n_components)
        embeddings = pca.fit_transform(embeddings)
        total_time = time.time()-start_time
        print(f"Total time: {time.strftime('%M:%S', time.gmtime(total_time))}")

    # Export the transformed embeddings
    print("Exporting the embeddings...")
    for fname,embed in zip(fnames,embeddings):
        embed = {"audio_path": os.path.join(AUDIO_DIR,f"{fname}.wav"),
                "embeddings": embed.tolist()}
        output_path = os.path.join(output_dir, f"{fname}.json")
        with open(output_path, "w") as outfile:
            json.dump(embed, outfile, indent=4)

    #############
    print("Done!")


import importlib
import pickle
from os import listdir
from os.path import join, exists
from typing import List

import numpy as np
from PIL import Image
from natsort import natsorted
from pyrep.objects import VisionSensor

from rlbench.backend.const import *
from rlbench.backend.utils import image_to_float_array, rgb_handles_to_mask
from rlbench.demo import Demo
from rlbench.observation_config import ObservationConfig

class InvalidTaskName(Exception):
    pass

def name_to_task_class(task_file: str):
    name = task_file.replace('.py', '')
    class_name = ''.join([w[0].upper() + w[1:] for w in name.split('_')])
    try:
        mod = importlib.import_module("rlbench.tasks.%s" % name)
        mod = importlib.reload(mod)
    except ModuleNotFoundError as e:
        raise InvalidTaskName(
            "The task file '%s' does not exist or cannot be compiled."
            % name) from e
    try:
        task_class = getattr(mod, class_name)
    except AttributeError as e:
        raise InvalidTaskName(
            "Cannot find the class name '%s' in the file '%s'."
            % (class_name, name)) from e
    return task_class

def get_stored_demos(amount: int, image_paths: bool, dataset_root: str,
                     variation_number: int, task_name: str,
                     obs_config: ObservationConfig,
                     random_selection: bool = True,
                     from_episode_number: int = 0) -> List[Demo]:

    task_root = join(dataset_root, task_name)
    if not exists(task_root):
        raise RuntimeError("Can't find the demos for %s at: %s" % (
            task_name, task_root))

    # Sample an amount of examples for the variation of this task
    examples_path = join(
        task_root, VARIATIONS_FOLDER % variation_number,
        EPISODES_FOLDER)
    examples = listdir(examples_path)
    if amount == -1:
        amount = len(examples)
    if amount > len(examples):
        raise RuntimeError(
            'You asked for %d examples, but only %d were available.' % (
                amount, len(examples)))
    if random_selection:
        selected_examples = np.random.choice(examples, amount, replace=False)
    else:
        selected_examples = natsorted(
            examples)[from_episode_number:from_episode_number+amount]

    # Process these examples (e.g. loading observations)
    demos = []
    for example in selected_examples:
        example_path = join(examples_path, example)
        with open(join(example_path, LOW_DIM_PICKLE), 'rb') as f:
            obs = pickle.load(f)

        l_sh_rgb_f = join(example_path, LEFT_SHOULDER_RGB_FOLDER)
        l_sh_depth_f = join(example_path, LEFT_SHOULDER_DEPTH_FOLDER)
        l_sh_mask_f = join(example_path, LEFT_SHOULDER_MASK_FOLDER)
        r_sh_rgb_f = join(example_path, RIGHT_SHOULDER_RGB_FOLDER)
        r_sh_depth_f = join(example_path, RIGHT_SHOULDER_DEPTH_FOLDER)
        r_sh_mask_f = join(example_path, RIGHT_SHOULDER_MASK_FOLDER)
        oh_rgb_f = join(example_path, OVERHEAD_RGB_FOLDER)
        oh_depth_f = join(example_path, OVERHEAD_DEPTH_FOLDER)
        oh_mask_f = join(example_path, OVERHEAD_MASK_FOLDER)
        wrist_rgb_f = join(example_path, WRIST_RGB_FOLDER)
        wrist_depth_f = join(example_path, WRIST_DEPTH_FOLDER)
        wrist_mask_f = join(example_path, WRIST_MASK_FOLDER)
        front_rgb_f = join(example_path, FRONT_RGB_FOLDER)
        front_depth_f = join(example_path, FRONT_DEPTH_FOLDER)
        front_mask_f = join(example_path, FRONT_MASK_FOLDER)

        num_steps = len(obs)

        if not (num_steps == len(listdir(l_sh_rgb_f)) == len(
                listdir(l_sh_depth_f)) == len(listdir(r_sh_rgb_f)) == len(
                listdir(r_sh_depth_f)) == len(listdir(oh_rgb_f)) == len(
                listdir(oh_depth_f)) == len(listdir(wrist_rgb_f)) == len(
                listdir(wrist_depth_f)) == len(listdir(front_rgb_f)) == len(
                listdir(front_depth_f))):
            raise RuntimeError('Broken dataset assumption')

        for i in range(num_steps):
            si = IMAGE_FORMAT % i
            if obs_config.left_shoulder_camera.rgb:
                obs[i].left_shoulder_rgb = join(l_sh_rgb_f, si)
            if obs_config.left_shoulder_camera.depth or obs_config.left_shoulder_camera.point_cloud:
                obs[i].left_shoulder_depth = join(l_sh_depth_f, si)
            if obs_config.left_shoulder_camera.mask:
                obs[i].left_shoulder_mask = join(l_sh_mask_f, si)
            if obs_config.right_shoulder_camera.rgb:
                obs[i].right_shoulder_rgb = join(r_sh_rgb_f, si)
            if obs_config.right_shoulder_camera.depth or obs_config.right_shoulder_camera.point_cloud:
                obs[i].right_shoulder_depth = join(r_sh_depth_f, si)
            if obs_config.right_shoulder_camera.mask:
                obs[i].right_shoulder_mask = join(r_sh_mask_f, si)
            if obs_config.overhead_camera.rgb:
                obs[i].overhead_rgb = join(oh_rgb_f, si)
            if obs_config.overhead_camera.depth or obs_config.overhead_camera.point_cloud:
                obs[i].overhead_depth = join(oh_depth_f, si)
            if obs_config.overhead_camera.mask:
                obs[i].overhead_mask = join(oh_mask_f, si)
            if obs_config.wrist_camera.rgb:
                obs[i].wrist_rgb = join(wrist_rgb_f, si)
            if obs_config.wrist_camera.depth or obs_config.wrist_camera.point_cloud:
                obs[i].wrist_depth = join(wrist_depth_f, si)
            if obs_config.wrist_camera.mask:
                obs[i].wrist_mask = join(wrist_mask_f, si)
            if obs_config.front_camera.rgb:
                obs[i].front_rgb = join(front_rgb_f, si)
            if obs_config.front_camera.depth or obs_config.front_camera.point_cloud:
                obs[i].front_depth = join(front_depth_f, si)
            if obs_config.front_camera.mask:
                obs[i].front_mask = join(front_mask_f, si)

            # Remove low dim info if necessary
            if not obs_config.joint_velocities:
                obs[i].joint_velocities = None
            if not obs_config.joint_positions:
                obs[i].joint_positions = None
            if not obs_config.joint_forces:
                obs[i].joint_forces = None
            if not obs_config.gripper_open:
                obs[i].gripper_open = None
            if not obs_config.gripper_pose:
                obs[i].gripper_pose = None
            if not obs_config.gripper_joint_positions:
                obs[i].gripper_joint_positions = None
            if not obs_config.gripper_touch_forces:
                obs[i].gripper_touch_forces = None
            if not obs_config.task_low_dim_state:
                obs[i].task_low_dim_state = None

        if not image_paths:
            for i in range(num_steps):
                if obs_config.left_shoulder_camera.rgb:
                    obs[i].left_shoulder_rgb = np.array(
                        _resize_if_needed(
                            Image.open(obs[i].left_shoulder_rgb),
                            obs_config.left_shoulder_camera.image_size))
                if obs_config.right_shoulder_camera.rgb:
                    obs[i].right_shoulder_rgb = np.array(
                        _resize_if_needed(Image.open(
                        obs[i].right_shoulder_rgb),
                            obs_config.right_shoulder_camera.image_size))
                if obs_config.overhead_camera.rgb:
                    obs[i].overhead_rgb = np.array(
                        _resize_if_needed(Image.open(
                        obs[i].overhead_rgb),
                            obs_config.overhead_camera.image_size))
                if obs_config.wrist_camera.rgb:
                    obs[i].wrist_rgb = np.array(
                        _resize_if_needed(
                            Image.open(obs[i].wrist_rgb),
                            obs_config.wrist_camera.image_size))
                if obs_config.front_camera.rgb:
                    obs[i].front_rgb = np.array(
                        _resize_if_needed(
                            Image.open(obs[i].front_rgb),
                            obs_config.front_camera.image_size))

                if obs_config.left_shoulder_camera.depth or obs_config.left_shoulder_camera.point_cloud:
                    l_sh_depth = image_to_float_array(
                        _resize_if_needed(
                            Image.open(obs[i].left_shoulder_depth),
                            obs_config.left_shoulder_camera.image_size),
                        DEPTH_SCALE)
                    near = obs[i].misc['left_shoulder_camera_near']
                    far = obs[i].misc['left_shoulder_camera_far']
                    l_sh_depth_m = near + l_sh_depth * (far - near)
                    if obs_config.left_shoulder_camera.depth:
                        d = l_sh_depth_m if obs_config.left_shoulder_camera.depth_in_meters else l_sh_depth
                        obs[i].left_shoulder_depth = obs_config.left_shoulder_camera.depth_noise.apply(d)
                    else:
                        obs[i].left_shoulder_depth = None

                if obs_config.right_shoulder_camera.depth or obs_config.right_shoulder_camera.point_cloud:
                    r_sh_depth = image_to_float_array(
                        _resize_if_needed(
                            Image.open(obs[i].right_shoulder_depth),
                            obs_config.right_shoulder_camera.image_size),
                        DEPTH_SCALE)
                    near = obs[i].misc['right_shoulder_camera_near']
                    far = obs[i].misc['right_shoulder_camera_far']
                    r_sh_depth_m = near + r_sh_depth * (far - near)
                    if obs_config.right_shoulder_camera.depth:
                        d = r_sh_depth_m if obs_config.right_shoulder_camera.depth_in_meters else r_sh_depth
                        obs[i].right_shoulder_depth = obs_config.right_shoulder_camera.depth_noise.apply(d)
                    else:
                        obs[i].right_shoulder_depth = None

                if obs_config.overhead_camera.depth or obs_config.overhead_camera.point_cloud:
                    oh_depth = image_to_float_array(
                        _resize_if_needed(
                            Image.open(obs[i].overhead_depth),
                            obs_config.overhead_camera.image_size),
                        DEPTH_SCALE)
                    near = obs[i].misc['overhead_camera_near']
                    far = obs[i].misc['overhead_camera_far']
                    oh_depth_m = near + oh_depth * (far - near)
                    if obs_config.overhead_camera.depth:
                        d = oh_depth_m if obs_config.overhead_camera.depth_in_meters else oh_depth
                        obs[i].overhead_depth = obs_config.overhead_camera.depth_noise.apply(d)
                    else:
                        obs[i].overhead_depth = None

                if obs_config.wrist_camera.depth or obs_config.wrist_camera.point_cloud:
                    wrist_depth = image_to_float_array(
                        _resize_if_needed(
                            Image.open(obs[i].wrist_depth),
                            obs_config.wrist_camera.image_size),
                        DEPTH_SCALE)
                    near = obs[i].misc['wrist_camera_near']
                    far = obs[i].misc['wrist_camera_far']
                    wrist_depth_m = near + wrist_depth * (far - near)
                    if obs_config.wrist_camera.depth:
                        d = wrist_depth_m if obs_config.wrist_camera.depth_in_meters else wrist_depth
                        obs[i].wrist_depth = obs_config.wrist_camera.depth_noise.apply(d)
                    else:
                        obs[i].wrist_depth = None

                if obs_config.front_camera.depth or obs_config.front_camera.point_cloud:
                    front_depth = image_to_float_array(
                        _resize_if_needed(
                            Image.open(obs[i].front_depth),
                            obs_config.front_camera.image_size),
                        DEPTH_SCALE)
                    near = obs[i].misc['front_camera_near']
                    far = obs[i].misc['front_camera_far']
                    front_depth_m = near + front_depth * (far - near)
                    if obs_config.front_camera.depth:
                        d = front_depth_m if obs_config.front_camera.depth_in_meters else front_depth
                        obs[i].front_depth = obs_config.front_camera.depth_noise.apply(d)
                    else:
                        obs[i].front_depth = None

                if obs_config.left_shoulder_camera.point_cloud:
                    obs[i].left_shoulder_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
                        l_sh_depth_m,
                        obs[i].misc['left_shoulder_camera_extrinsics'],
                        obs[i].misc['left_shoulder_camera_intrinsics'])
                if obs_config.right_shoulder_camera.point_cloud:
                    obs[i].right_shoulder_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
                        r_sh_depth_m,
                        obs[i].misc['right_shoulder_camera_extrinsics'],
                        obs[i].misc['right_shoulder_camera_intrinsics'])
                if obs_config.overhead_camera.point_cloud:
                    obs[i].overhead_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
                        oh_depth_m,
                        obs[i].misc['overhead_camera_extrinsics'],
                        obs[i].misc['overhead_camera_intrinsics'])
                if obs_config.wrist_camera.point_cloud:
                    obs[i].wrist_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
                        wrist_depth_m,
                        obs[i].misc['wrist_camera_extrinsics'],
                        obs[i].misc['wrist_camera_intrinsics'])
                if obs_config.front_camera.point_cloud:
                    obs[i].front_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
                        front_depth_m,
                        obs[i].misc['front_camera_extrinsics'],
                        obs[i].misc['front_camera_intrinsics'])

                # Masks are stored as coded RGB images.
                # Here we transform them into 1 channel handles.
                if obs_config.left_shoulder_camera.mask:
                    obs[i].left_shoulder_mask = rgb_handles_to_mask(
                        np.array(_resize_if_needed(Image.open(
                            obs[i].left_shoulder_mask),
                            obs_config.left_shoulder_camera.image_size)))
                if obs_config.right_shoulder_camera.mask:
                    obs[i].right_shoulder_mask = rgb_handles_to_mask(
                        np.array(_resize_if_needed(Image.open(
                            obs[i].right_shoulder_mask),
                            obs_config.right_shoulder_camera.image_size)))
                if obs_config.overhead_camera.mask:
                    obs[i].overhead_mask = rgb_handles_to_mask(
                        np.array(_resize_if_needed(Image.open(
                            obs[i].overhead_mask),
                            obs_config.overhead_camera.image_size)))
                if obs_config.wrist_camera.mask:
                    obs[i].wrist_mask = rgb_handles_to_mask(np.array(
                        _resize_if_needed(Image.open(
                            obs[i].wrist_mask),
                            obs_config.wrist_camera.image_size)))
                if obs_config.front_camera.mask:
                    obs[i].front_mask = rgb_handles_to_mask(np.array(
                        _resize_if_needed(Image.open(
                            obs[i].front_mask),
                            obs_config.front_camera.image_size)))

        demos.append(obs)
    return demos

def _resize_if_needed(image, size):
    if image.size[0] != size[0] or image.size[1] != size[1]:
        image = image.resize(size)
    return image


# -*- encoding: utf-8 -*-

import json
import random
import requests
import sched
import time

from django.shortcuts import render
from django.http import HttpResponse
from datetime import datetime

from .load_data import load_data, load_access_token
from .getCalenderInformation import get_schedule

REPLY_ENDPOINT = 'https://api.line.me/v2/bot/message/reply'
ACCESS_TOKEN = load_access_token
HEADER = {
    "Content-Type": "application/json",
    "Authorization": "Bearer " + ACCESS_TOKEN
}

def index(request):
    return HttpResponse("This is a bot api.")

def callback(request):
    reply = ""
    #  requestの情報をdic形式で取得
    request_json = json.loads(request.body.decode('utf-8'))
    for e in request_json['events']:
        # 返信先のトークンの取得
        reply_token = e['replyToken']
        # typeの取得
        message_type = e['message']['type']

        if message_type == 'text':
            # 受信メッセージの取得
            text = e['message']['text']
            # Lineにセリフを返す
            reply += reply_text(reply_token, text)

    return HttpResponse(reply)

def reply_text(reply_token, text):
    reply = ""
    #if text.find('追加') > -1 or text.find('ついか') > -1:
     #   m_data = 'd';
    #el
    if text.find('予定') > -1 or text.find('よてい') > -1:
        m_data = get_schedule()
        if not m_data:
            reply = '直近のイベントが見つかりませんでした。'
        else:
            for i in m_data:
                reply += '・{}\n{}'.format(i[0], i[1])
                if i != m_data[-1]: reply += '\n\n'
                else: reply += '\n\nがありますよ！\n頑張ってください！'
    elif text == '言語ガチャ':
        reply = random.choice(load_data)
    else:
        reply = 'まだ実装してないよ。'
    payload = {
        "replyToken": reply_token,
        "messages": [
            {
                "type": "text",
                "text": reply
            }
        ]
    }

    # Lineにデータを送信
    requests.post(REPLY_ENDPOINT, headers=HEADER, data=json.dumps(payload))
    return reply

def news():
    m_data = get_today_schedule()
    if not m_data:
        reply = '直近のイベントが見つかりませんでした。'
    else:
        for i in m_data:
            reply += '{}\n{}がありますよ！\n頑張ってください！'.format(i[0], i[1])
            if i != m_data[-1]: reply += '\n\n'

    requests.post(REPLY_ENDPOINT, headers=HEADER, data=json.dumps(payload))
    return reply


# -*- encoding: utf-8 -*-
'''
Created on 15/01/2011

@author: ErunamoJAZZ
@license: GPLv3
@summary: Muffin Translator, ayudante para la traducción de anime.
@web: http://code.google.com/p/muffin/
'''

#import wx.stc as stc
import wx, os
import threading, time
import codecs

class MuffinText(wx.TextCtrl):
    '''
    MuffinText: Clase encargada del area de texto.
    '''

    def __init__(self, parent):
        '''
        Constructor
        '''
        #Si solo se ha abierto y no se a guardado, permanece falso
        self.esta_guardado=False
        self.path=None
        self.__parent=parent

        texto_inicial=u"""#(Modelo para traducir)
Actor 1: Well do I understand your speech, yet few strangers do so.
         Why then do you not speak in the Common Tongue,
         as is the custom in the West, if you wish to be answered?
         # TL check: The above seems to be a quote from the lord of the rings, look it up later
Actor 2: What are you babbling about?

NOTA: Use la tecla 'ESC' para pausar y despausar el video.
      Puedes usar las teclas F1 y F2 para ir 5 segundos atras o adelante en el video.
      MuffinTranslator tiene un sistema de auto-guardado cada 30seg, después de guardar la primer vez.
      #NO OLVIDES REPORTAR LOS ERRORES EN: http://code.google.com/p/muffin/issues/list"""

        wx.TextCtrl.__init__(self, self.__parent, -1, texto_inicial, style=wx.TE_MULTILINE|wx.HSCROLL)

    def __abrir_texto(self,_path):
        '''
        Abre un texto intentando primero desde utf-8, y
        luego desde Latin-1 (ANSI). Este ultimo abrirá
        mal textos en otras codificaciones, como unicode.
        '''
        try:
            file= codecs.open(_path, 'rU', 'utf-8')#OJO
            #file=open(_path,"rU")
            texto = file.read()
            print (u'Texto abierto con codificación utf-8')
        except:
            file.close()
            file= codecs.open(_path, 'rU', 'Latin-1')
            texto = file.read()
            print (u'Texto abierto con codificación Latin-1 (ANSI)')

        file.close()

        self.SetValue(texto)
        if self.esta_guardado:
            self.hiloGuardado.kill()
            self.hiloGuardado, self.path=None,None
            self.esta_guardado=False

    def __guardar_texto(self,_doc_path=None):
        '''
        Guarda el texto en un archivo, y hace un backup del mismo.
        '''
        if not _doc_path is None:
            self.path=_doc_path
        if not self.path is None:
            self.SaveFile(self.path)#soluciona problema con unicode
            self.SaveFile(self.path+'~')#guarda también un backup
            print (u"»» Archivo guardado correctamente + backup")
            if not self.esta_guardado:
                self.esta_guardado=True
                self.hiloGuardado=AutoGuardado(self)
                self.hiloGuardado.start()

        else:#si no hay path...
            self.onSaveFileWhit(wx.Event)

    #Evento para abrir Archivos
    def onLoadFile(self, event):
            dlg = wx.FileDialog(None, message=u"Seleccione un archivo de texto",
                                defaultDir=os.path.expanduser('~'), defaultFile=".txt",
                                style=wx.OPEN | wx.CHANGE_DIR )
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                self.__abrir_texto( unicode( path.replace('\\','/')  )  )
                dlg.Destroy()

    #Evento para "Guardar Archivo como..."
    def onSaveFileWhit(self, event):
            dlg = wx.FileDialog(None, message=u"Guarde como un archivo de texto",
                                defaultDir=os.path.expanduser('~'), defaultFile=".txt",
                                style=wx.SAVE | wx.CHANGE_DIR )
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                self.__guardar_texto( unicode( path.replace('\\','/')  )  )
                dlg.Destroy()

    def onSaveFile(self, event):
        self.__guardar_texto()

###############################################
########   AUTO-GUARDADO, MULTIHILO   #########
class AutoGuardado(threading.Thread):
    '''
    AutoGuardado: Clase que hereda de Therad, y
    guarda cada cierto tiempo los datos.
    '''
    def __init__(self, _wxText):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.__wxText=_wxText
        self.seguir_guardando=self.__wxText.esta_guardado

    def run(self):
        '''
        Inicia Bucle de auto-guardado.
        '''
        while True : #si ya se ha guardado
            time.sleep(30)
            if self.seguir_guardando:
                self.__wxText.SaveFile(self.__wxText.path)
                print (u"» Guardado Automatico...(30seg) "+self.getName() )
            else:
                print (unicode( self.getName()+" is dead X.x") )
                break

    def kill(self):
        '''
        Hace que el bucle se termine en la proxima iteración.
        '''
        self.seguir_guardando=False
        print (unicode( self.getName()+" pronto... morirá u.u") )
        #self.join()

#stc.PreStyledTextCtrl()