import importlib
import json
import logging
import os
import tempfile
from io import StringIO

from celery import shared_task
from django.conf import settings
from django.utils.timezone import now
from scrapy.crawler import Crawler, CrawlerProcess
from scrapy.utils.spider import iter_spider_classes

from .models import Spider as SpiderModel, Execution, Item

@shared_task
def start_spiders():
    for spider in SpiderModel.objects.all():
        run_spider.delay(spider.id)

@shared_task
def run_spider(spider_id):
    spider = SpiderModel.objects.get(id=spider_id)
    execution = Execution.objects.create(spider_id=spider_id, time_started=now())

    user_settings = getattr(settings, 'SCRATCHY_SPIDERS', {})

    item_storage = tempfile.NamedTemporaryFile(delete=False)

    default_settings = {
        'DNS_TIMEOUT': 5,
        'DOWNLOAD_TIMEOUT': 5,
        'AUTOTHROTTLE_ENABLED': True,
        'AUTOTHROTTLE_START_DELAY': 1,
        'AUTOTHROTTLE_MAX_DELAY': 5,
        'AUTOTHROTTLE_TARGET_CONCURRENCY': 1.0,
    }

    internal_settings = {
        'FEED_FORMAT': 'jsonlines',
        'FEED_EXPORT_ENCODING': 'utf-8',
        'FEED_URI': item_storage.name,

    }

    scrapy_settings = {
        **default_settings,
        **user_settings,
        **spider.settings,
        **internal_settings,  # last because these must not be overwritten
    }

    scrapy_spider_cls = None

    module = importlib.import_module(spider.module)

    for cls in iter_spider_classes(module):
        scrapy_spider_cls = cls
        break  # use first valid class in module

    if scrapy_spider_cls is None:
        raise RuntimeError(f'No valid spider class found in module {module}')

    process = CrawlerProcess(settings=None, install_root_handler=False)

    log_capture_string = StringIO()
    log_handler = logging.StreamHandler(log_capture_string)
    log_handler.setLevel(spider.log_level)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    log_handler.setFormatter(formatter)
    scrapy_logger = logging.getLogger('scrapy')
    scrapy_logger.addHandler(log_handler)
    spider_logger = logging.getLogger(scrapy_spider_cls.name)  # scrapy uses the spider name as logger id
    spider_logger.addHandler(log_handler)

    crawler = Crawler(scrapy_spider_cls, scrapy_settings)

    process.crawl(crawler)
    process.start()
    # blocks here

    log_contents = log_capture_string.getvalue()
    spider_logger.removeHandler(log_handler)
    scrapy_logger.removeHandler(log_handler)
    log_capture_string.close()

    execution.time_ended = now()
    execution.stats = crawler.stats._stats
    execution.log = log_contents
    execution.save()

    item_storage.seek(0)

    items = []
    for line in item_storage.readlines():
        item_data = json.loads(line)
        item = Item(spider_id=spider_id, execution=execution, data=item_data)
        items.append(item)

    Item.objects.bulk_create(items, batch_size=100)

    item_storage.close()
    os.remove(item_storage.name)


'''Tests for GinjinnConfiguration
'''

import pkg_resources
import tempfile
import os
import pytest
import yaml
import copy

from ginjinn.ginjinn_config import GinjinnConfiguration, InvalidGinjinnConfigurationError
from ginjinn.ginjinn_config.config_error import InvalidModelConfigurationError

@pytest.fixture(scope='module', autouse=True)
def tmp_input_paths():
    tmpdir = tempfile.TemporaryDirectory()

    img_path_train = os.path.join(tmpdir.name, 'images_train')
    os.mkdir(img_path_train)
    img_path_test = os.path.join(tmpdir.name, 'images_test')
    os.mkdir(img_path_test)
    img_path_validation = os.path.join(tmpdir.name, 'images_validation')
    os.mkdir(img_path_validation)

    pvoc_ann_path_train = os.path.join(tmpdir.name, 'annotations_train')
    os.mkdir(pvoc_ann_path_train)
    pvoc_ann_path_test = os.path.join(tmpdir.name, 'annotations_test')
    os.mkdir(pvoc_ann_path_test)
    pvoc_ann_path_validation = os.path.join(tmpdir.name, 'annotations_validation')
    os.mkdir(pvoc_ann_path_validation)

    coco_ann_path_train = os.path.join(tmpdir.name, 'annotations_train.json')
    with open(coco_ann_path_train, 'w') as ann_f:
        ann_f.write('')
    coco_ann_path_test = os.path.join(tmpdir.name, 'annotations_test.json')
    with open(coco_ann_path_test, 'w') as ann_f:
        ann_f.write('')
    coco_ann_path_validation = os.path.join(tmpdir.name, 'annotations_valiation.json')
    with open(coco_ann_path_validation, 'w') as ann_f:
        ann_f.write('')

    yield {
        'coco_ann_path_train': coco_ann_path_train,
        'coco_ann_path_test': coco_ann_path_test,
        'coco_ann_path_validation': coco_ann_path_validation,
        'pvoc_ann_path_train': pvoc_ann_path_train,
        'pvoc_ann_path_test': pvoc_ann_path_test,
        'pvoc_ann_path_validation': pvoc_ann_path_validation,
        'img_path_train': img_path_train,
        'img_path_test': img_path_test,
        'img_path_validation': img_path_validation,
        'project_dir': tmpdir.name,
    }

    tmpdir.cleanup()

@pytest.fixture
def config_dicts(tmp_input_paths):
    simple_config = {
        'project_dir': tmp_input_paths['project_dir'],
        'task': 'bbox-detection',
        'input': {
            'type': 'PVOC',
            'training': {
                'annotation_path': tmp_input_paths['pvoc_ann_path_train'],
                'image_path': tmp_input_paths['img_path_train'],
            },
        },
        'model': {
            'name': 'faster_rcnn_R_50_FPN_3x',
            'weights': '',
            'model_parameters': {
                'roi_heads': {
                    'batch_size_per_image': 4096,
                    'iou_thresholds': [0.5],
                },
                'anchor_generator': {
                    'sizes': [[32, 64, 128, 256]],
                    'angles': [[-90, 0, 90]],
                    'aspect_ratios': [[0.25, 0.5, 0.75, 1.0, 1.25]]
                },
                'rpn': {
                    'iou_thresholds': [0.3, 0.7],
                    'batch_size_per_image': 256,
                },
                'box_head': {
                    'class_agnostic': False,
                    'train_on_pred_boxes': False,
                }
            },
        },
        'training': {
            'learning_rate': 0.002,
            'batch_size': 1,
            'max_iter': 10000,
        },
        'augmentation': [
            {
                'horizontal_flip': {
                    'probability': 0.25
                }
            },
            {
                'vertical_flip': {
                    'probability': 0.25
                }
            },
        ],
    }

    return [
        simple_config
    ]

@pytest.fixture
def config_file_examples():
    example_config_0_path = pkg_resources.resource_filename(
        'ginjinn', 'data/ginjinn_config/example_config_0.yaml',
    )
    example_config_1_path = pkg_resources.resource_filename(
        'ginjinn', 'data/ginjinn_config/example_config_1.yaml',
    )

    return [
        example_config_0_path,
        example_config_1_path,
    ]

def read_config_file(file_path):
    with open(file_path) as config_file:
        config = yaml.safe_load(config_file)
    return config

def test_from_dictionary_simple(config_dicts):
    simple_config_dict = config_dicts[0]

    ginjinn_config_0 = GinjinnConfiguration.from_dictionary(simple_config_dict)
    assert ginjinn_config_0.task == simple_config_dict['task'] and\
        ginjinn_config_0.project_dir == simple_config_dict['project_dir'],\
        'simple base configuration not set.'
    # TODO implement model and augmentation assertions!
    assert ginjinn_config_0.model.name == simple_config_dict['model']['name']
    assert ginjinn_config_0.training.learning_rate == simple_config_dict['training']['learning_rate']
    assert ginjinn_config_0.input.type == simple_config_dict['input']['type']

def test_from_config_file_simple(config_file_examples):
    simple_config_file_0 = config_file_examples[0]
    simple_config_dict_0 = read_config_file(simple_config_file_0)
    print(simple_config_dict_0)
    if not os.path.exists(simple_config_dict_0['input']['training']['annotation_path']):
        os.mkdir(simple_config_dict_0['input']['training']['annotation_path'])
    if not os.path.exists(simple_config_dict_0['input']['training']['image_path']):
        os.mkdir(simple_config_dict_0['input']['training']['image_path'])

    simple_config_0 = GinjinnConfiguration.from_config_file(simple_config_file_0)
    assert simple_config_0.task == simple_config_dict_0['task'] and\
        simple_config_0.project_dir == simple_config_dict_0['project_dir'] and\
        simple_config_0.input.train.annotation_path == os.path.abspath(simple_config_dict_0['input']['training']['annotation_path']) and\
        simple_config_0.input.train.image_path ==  os.path.abspath(simple_config_dict_0['input']['training']['image_path']),\
        'GinjinnConfig was not successfully constructed from simple configuration file.'

    assert simple_config_0.model.name == simple_config_dict_0['model']['name']
    assert simple_config_0.training.learning_rate == simple_config_dict_0['training']['learning_rate']

    os.rmdir(simple_config_dict_0['input']['training']['annotation_path'])
    os.rmdir(simple_config_dict_0['input']['training']['image_path'])

    simple_config_file_1 = config_file_examples[1]
    simple_config_dict_1 = read_config_file(simple_config_file_1)
    print(simple_config_dict_1)
    if not os.path.exists(simple_config_dict_1['input']['training']['annotation_path']):
        with open(simple_config_dict_1['input']['training']['annotation_path'], 'w') as f:
            f.write('')
    if not os.path.exists(simple_config_dict_1['input']['training']['image_path']):
        os.mkdir(simple_config_dict_1['input']['training']['image_path'])

    simple_config_1 = GinjinnConfiguration.from_config_file(simple_config_file_1)
    assert simple_config_1.task == simple_config_dict_1['task'] and\
        simple_config_1.project_dir == simple_config_dict_1['project_dir'] and\
        simple_config_1.input.train.annotation_path ==  os.path.abspath(simple_config_dict_1['input']['training']['annotation_path']) and\
        simple_config_1.input.train.image_path ==  os.path.abspath(simple_config_dict_1['input']['training']['image_path']),\
        'GinjinnConfig was not successfully constructed from simple configuration file.'

    assert simple_config_1.model.name == simple_config_dict_1['model']['name']
    assert simple_config_1.training.learning_rate == simple_config_dict_1['training']['learning_rate']

    os.remove(simple_config_dict_1['input']['training']['annotation_path'])
    os.rmdir(simple_config_dict_1['input']['training']['image_path'])

def test_invalid_task(config_dicts):
    simple_config_dict = copy.deepcopy(config_dicts[0])
    simple_config_dict['task'] = 'foobar'

    with pytest.raises(InvalidGinjinnConfigurationError):
        GinjinnConfiguration.from_dictionary(simple_config_dict)

def test_incompatible_task(config_dicts):
    simple_config_dict = copy.deepcopy(config_dicts[0])
    simple_config_dict['task'] = 'instance-segmentation'

    with pytest.raises(InvalidGinjinnConfigurationError):
        GinjinnConfiguration.from_dictionary(simple_config_dict)

def test_to_detectron2_config(config_dicts):
    simple_config_dict = config_dicts[0]

    ginjinn_config_0 = GinjinnConfiguration.from_dictionary(simple_config_dict)
    detectron2_config_0 = ginjinn_config_0.to_detectron2_config()

    # TODO additional tests

def test_model_parameters(config_dicts):

    config_dict = copy.deepcopy(config_dicts[0])
    config_dict['model']['model_parameters']['invalid_parameter'] = {}
    with pytest.raises(InvalidModelConfigurationError):
        GinjinnConfiguration.from_dictionary(config_dict)

    config_dict = copy.deepcopy(config_dicts[0])
    config_dict['model']['model_parameters']['anchor_generator']['invalid_parameter'] = 1
    with pytest.raises(InvalidModelConfigurationError):
        GinjinnConfiguration.from_dictionary(config_dict)

    config_dict = copy.deepcopy(config_dicts[0])
    config_dict['model']['model_parameters']['rpn']['invalid_parameter'] = 1
    with pytest.raises(InvalidModelConfigurationError):
        GinjinnConfiguration.from_dictionary(config_dict)

    config_dict = copy.deepcopy(config_dicts[0])
    config_dict['model']['model_parameters']['roi_heads']['invalid_parameter'] = 1
    with pytest.raises(InvalidModelConfigurationError):
        GinjinnConfiguration.from_dictionary(config_dict)

    config_dict = copy.deepcopy(config_dicts[0])
    config_dict['model']['model_parameters']['box_head']['invalid_parameter'] = 1
    with pytest.raises(InvalidModelConfigurationError):
        GinjinnConfiguration.from_dictionary(config_dict)

    config_dict = copy.deepcopy(config_dicts[0])
    config_dict['model']['name'] = 'mask_rcnn_R_50_C4_1x'
    config_dict['task'] = 'instance-segmentation'
    del config_dict['model']['model_parameters']['box_head']
    config_dict['model']['model_parameters']['mask_head'] = {
        'class_agnostic': False,
        'pooler_resolution': 10,
    }
    GinjinnConfiguration.from_dictionary(config_dict)

    config_dict['model']['model_parameters']['mask_head']['invalid_parameter'] = 1
    with pytest.raises(InvalidModelConfigurationError):
        GinjinnConfiguration.from_dictionary(config_dict)


import cv2
import numpy as np

######################
nPlateCascade = cv2.CascadeClassifier("D:\Fauzan Alif\Learn Python\Resources/haarcascade_russian_plate_number.xml")
widthImg = 640
heightImg = 480
######################
#kita ingin mendeteksi plat nomor kendaraan menggunakan cascades
#cascades adalah semacam library untuk mendeteksi object tertentu, yg bisa didapatkan dari opencv sndiri
#cascades berupa file .xml

cap = cv2.VideoCapture(0)
cap.set (3,widthImg)
cap.set(4,heightImg)
cap.set(10,100)

count = 0
minArea= 500

while True:
    success, img = cap.read()

    imgGray = cv2.cvtColor(img ,cv2.COLOR_BGR2GRAY)

    #face cascade.detect multi scale (target gambar, luas besar kotaknya
    # , minNeighbor (dapat dilihat pada kite) )
    platDetector = nPlateCascade.detectMultiScale(imgGray, 1.1,4)
    for (x, y, w, h) in platDetector:
        area = w*h
        if area > minArea:
            cv2.rectangle(img, (x, y), (x+w, y+h), (255,0,0), 3)
            cv2.putText(img, "Plat Kendaraan",(x,y-5), cv2.FONT_HERSHEY_COMPLEX, 2, (255,0,0), 2)
            imgRoi = img[y:y+h, x:x+w]

            cv2.imshow("Plat Detector", img)

    #sekarang, setelah kita memunculkan plate detection, kita ingin menyimpan hasil deteksi ke dalam folder
    if cv2.waitKey(1) & 0xFF == ord('q'): #bisa jg ord key nya diganti ke huruf s (karena saved)
        #kita ingin menyimpan hasilnya ke folder tertentu
        cv2.imwrite("D:\Fauzan Alif\Learn Python\Resources/Scanned/No_Plate_"+str(count)+".jpg", imgRoi)
        #kita ingin memberikan block warna pada text
        cv2.rectangle(img, (0,200), (640,300), (0,255,0), cv2.FILLED)
        #kita ingin memberikan kalimat notif klo udah disave
        cv2.putText(img, "Plat Numbernya telah disave", (150,265), cv2.FONT_HERSHEY_COMPLEX, 2, (255,0,0), 2)

        cv2.imshow("Plat telah disave",img)
        cv2.waitKey(500)
        count+=1

cv2.destroyAllWindows()


from typing import Optional

import torch
from torch.nn.parallel import DistributedDataParallel as DDP

from algorithmic_efficiency import param_utils
from algorithmic_efficiency import spec
from algorithmic_efficiency.pytorch_utils import pytorch_setup
from algorithmic_efficiency.workloads.librispeech_conformer.librispeech_pytorch.models import \
    initialize
from algorithmic_efficiency.workloads.librispeech_conformer.librispeech_pytorch.workload import \
    LibriSpeechConformerWorkload
from algorithmic_efficiency.workloads.librispeech_deepspeech.librispeech_pytorch.models import \
    DeepspeechConfig
from algorithmic_efficiency.workloads.librispeech_deepspeech.librispeech_pytorch.models import \
    DeepspeechEncoderDecoder

USE_PYTORCH_DDP, RANK, DEVICE, N_GPUS = pytorch_setup()

MAX_INPUT_LENGTH = 320000

class LibriSpeechDeepSpeechWorkload(LibriSpeechConformerWorkload):

  def init_model_fn(
      self,
      rng: spec.RandomState,
      dropout_rate: Optional[float] = None,
      aux_dropout_rate: Optional[float] = None) -> spec.ModelInitState:
    """Deepspeech model init function.

    Here we use dropout_rate as feed_forward_dropout_rate, and aux_dropout_rate
    as input_dropout_rate.
    """
    torch.random.manual_seed(rng[0])
    model = DeepspeechEncoderDecoder(
        DeepspeechConfig(
            feed_forward_dropout_rate=dropout_rate,
            use_specaug=self.use_specaug,
            input_dropout_rate=aux_dropout_rate)).eval()
    self.ctc_loss = torch.nn.CTCLoss(blank=0, reduction='none')
    # Run model once to initialize lazy layers.
    t = MAX_INPUT_LENGTH
    wave = torch.randn((2, t))
    pad = torch.zeros_like(wave)
    _ = model(wave, pad)
    initialize(model)

    self._param_shapes = param_utils.pytorch_param_shapes(model)
    self._param_types = param_utils.pytorch_param_types(self._param_shapes)
    model.to(DEVICE)
    self.requires_sync_before_eval = False
    if N_GPUS > 1:
      if USE_PYTORCH_DDP:
        model = DDP(model, device_ids=[RANK], output_device=RANK)
      else:
        model = torch.nn.DataParallel(model)
    return model, None

  def is_output_params(self, param_key: spec.ParameterKey) -> bool:
    return param_key in ['lin.weight', 'lin.bias']

  @property
  def validation_target_value(self) -> float:
    return 0.119936

  @property
  def test_target_value(self) -> float:
    return 0.074143

  @property
  def step_hint(self) -> int:
    """Max num steps the baseline algo was given to reach the target."""
    return 48_000

  @property
  def max_allowed_runtime_sec(self) -> int:
    return 55_506  # ~15.4 hours


from c7n.provider import clouds

import copy
import itertools
import logging
import sys

import boto3

from c7n.credentials import SessionFactory
from c7n.registry import PluginRegistry
from c7n import utils

log = logging.getLogger('custodian.aws')

def _default_region(options):
    marker = object()
    value = getattr(options, 'regions', marker)
    if value is marker:
        return

    if len(value) > 0:
        return

    try:
        options.regions = [utils.get_profile_session(options).region_name]
    except Exception:
        log.warning('Could not determine default region')
        options.regions = [None]

    if options.regions[0] is None:
        log.error('No default region set. Specify a default via AWS_DEFAULT_REGION '
                  'or setting a region in ~/.aws/config')
        sys.exit(1)

    log.debug("using default region:%s from boto" % options.regions[0])

def _default_account_id(options):
    if options.assume_role:
        try:
            options.account_id = options.assume_role.split(':')[4]
            return
        except IndexError:
            pass
    try:
        session = utils.get_profile_session(options)
        options.account_id = utils.get_account_id_from_sts(session)
    except Exception:
        options.account_id = None

@clouds.register('aws')
class AWS(object):

    resource_prefix = 'aws'
    # legacy path for older plugins
    resources = PluginRegistry('resources')

    def initialize(self, options):
        """
        """
        _default_region(options)
        _default_account_id(options)
        return options

    def get_session_factory(self, options):
        return SessionFactory(
            options.region,
            options.profile,
            options.assume_role,
            options.external_id)

    def initialize_policies(self, policy_collection, options):
        """Return a set of policies targetted to the given regions.

        Supports symbolic regions like 'all'. This will automatically
        filter out policies if their being targetted to a region that
        does not support the service. Global services will target a
        single region (us-east-1 if only all specified, else first
        region in the list).

        Note for region partitions (govcloud and china) an explicit
        region from the partition must be passed in.
        """
        from c7n.policy import Policy, PolicyCollection
        policies = []
        service_region_map, resource_service_map = get_service_region_map(
            options.regions, policy_collection.resource_types)

        for p in policy_collection:
            available_regions = service_region_map.get(
                resource_service_map.get(p.resource_type), ())

            # its a global service/endpoint, use user provided region
            # or us-east-1.
            if not available_regions and options.regions:
                candidates = [r for r in options.regions if r != 'all']
                candidate = candidates and candidates[0] or 'us-east-1'
                svc_regions = [candidate]
            elif 'all' in options.regions:
                svc_regions = available_regions
            else:
                svc_regions = options.regions

            for region in svc_regions:
                if available_regions and region not in available_regions:
                    level = ('all' in options.regions and
                             logging.DEBUG or logging.WARNING)
                    # TODO: fixme
                    policy_collection.log.log(
                        level, "policy:%s resources:%s not available in region:%s",
                        p.name, p.resource_type, region)
                    continue
                options_copy = copy.copy(options)
                options_copy.region = str(region)

                if len(options.regions) > 1 or 'all' in options.regions and getattr(
                        options, 'output_dir', None):
                    options_copy.output_dir = (
                        options.output_dir.rstrip('/') + '/%s' % region)
                policies.append(
                    Policy(p.data, options_copy,
                           session_factory=policy_collection.session_factory()))
        return PolicyCollection(policies, options)

def get_service_region_map(regions, resource_types):
    # we're not interacting with the apis just using the sdk meta information.
    session = boto3.Session(
        region_name='us-east-1',
        aws_access_key_id='never',
        aws_secret_access_key='found')

    resource_service_map = {
        r: clouds['aws'].resources.get(r).resource_type.service
        for r in resource_types if r != 'account'}
    # support for govcloud and china, we only utilize these regions if they
    # are explicitly passed in on the cli.
    partition_regions = {}
    for p in ('aws-cn', 'aws-us-gov'):
        for r in session.get_available_regions('s3', partition_name=p):
            partition_regions[r] = p

    partitions = ['aws']
    for r in regions:
        if r in partition_regions:
            partitions.append(partition_regions[r])

    service_region_map = {}
    for s in set(itertools.chain(resource_service_map.values())):
        for partition in partitions:
            service_region_map.setdefault(s, []).extend(
                session.get_available_regions(s, partition_name=partition))
    return service_region_map, resource_service_map


#modulos
import main_edited as main
from tkinter import *
from tkinter import ttk, Tk, Frame, Menu
import tkinter as tk
from PIL import Image, ImageTk

#chama main
def chama(*args):
    m1=matg1var.get()
    m2=matg2var.get()
    m3=matg3var.get()
    m4=matg4var.get()
    f1=flug1var.get()
    f2=flug2var.get()
    f3=flug3var.get()
    f4=flug4var.get()
    ti1=Tempming1.get()
    ti2=Tempming2.get()
    ti3=Tempming3.get()
    ti4=Tempming4.get()
    ts1=Tempmaxg1.get()
    ts2=Tempmaxg2.get()
    ts3=Tempmaxg3.get()
    ts4=Tempmaxg4.get()
    tf1=Tempambg1.get()
    tf2=Tempambg2.get()
    tf3=Tempambg3.get()
    tf4=Tempambg4.get()
    vs1=estg1.get()
    if vs1 == 1:
        s1=txtg1.get()
    else:
        s1=formg1.get()
    vs2=estg2.get()
    if vs2 == 1:
        s2=txtg2.get()
    else:
        s2=formg2.get()
    vs3=estg3.get()
    if vs3 == 1:
        s3=txtg3.get()
    else:
        s3=formg3.get()
    vs4=estg4.get()
    if vs4 == 1:
        s4=txtg4.get()
    else:
        s4=formg4.get()

    if Tempxvar.get()==True :
        ts2=ts1
        ts3=ts1
        ts4=ts1
    if Tempmvar.get() == True :
        ti2=ti1
        ti3=ti1
        ti4=ti1
    if estvar.get() == True :
        vs2=vs1
        vs3=vs1
        vs4=vs1
        s2=s1
        s3=s1
        s4=s1
    if matvar.get() == True :
        m2=m1
        m3=m1
        m4=m1
    if fluvar.get() == True :
        f2=f1
        f3=f1
        f4=f1
    if Tempavar.get() == True :
        tf2=tf1
        tf3=tf1
        tf4=tf1

    main.simula(m1,m2,m3,m4,f1,f2,f3,f4,float(ti1),float(ti2),float(ti3),float(ti4),float(ts1),float(ts2),float(ts3),float(ts4),float(tf1),float(tf2),float(tf3),float(tf4),vs1,s1,vs2,s2,vs3,s3,vs4,s4)

#defs show/hide
def updatecheck():
    if Tempxvar.get()==True and Tempmvar.get() == True and estvar.get() == True and matvar.get() == True and fluvar.get() == True and Tempavar.get() == True:
        titulog1.configure(text="Chapas:", font=('Oswald',14))
        titulog2.grid_forget()
        titulog3.grid_forget()
        titulog4.grid_forget()
    else:
        titulog1.configure(text="Chapa 1:", font=('Oswald',14))
        titulog2.grid(column=0, row=5, sticky=(W))
        titulog3.grid(column=0, row=7, sticky=(W))
        titulog4.grid(column=0, row=9, sticky=(W))

    if Tempavar.get() == True:
        tempatitg2.grid_forget()
        Tempag2.grid_forget()

        tempatitg3.grid_forget()
        Tempag3.grid_forget()

        tempatitg4.grid_forget()
        Tempag4.grid_forget()
    else:
        tempatitg2.grid(column=2, row=0, pady=5)
        Tempag2.grid(column=3, row=0 )

        tempatitg3.grid(column=2, row=0, pady=5)
        Tempag3.grid(column=3, row=0 )

        tempatitg4.grid(column=2, row=0, pady=5)
        Tempag4.grid(column=3, row=0 )

    if Tempxvar.get() == True:
        tempxtitg2.grid_forget()
        Tempxg2.grid_forget()

        tempxtitg3.grid_forget()
        Tempxg3.grid_forget()

        tempxtitg4.grid_forget()
        Tempxg4.grid_forget()
    else:
        tempxtitg2.grid(column=2, row=1, pady=5)
        Tempxg2.grid(column=3, row=1 )

        tempxtitg3.grid(column=2, row=1, pady=5)
        Tempxg3.grid(column=3, row=1 )

        tempxtitg4.grid(column=2, row=1, pady=5)
        Tempxg4.grid(column=3, row=1 )

    if Tempmvar.get() == True:
        tempmtitg2.grid_forget()
        Tempmg2.grid_forget()

        tempmtitg3.grid_forget()
        Tempmg3.grid_forget()

        tempmtitg4.grid_forget()
        Tempmg4.grid_forget()
    else:
        tempmtitg2.grid(column=2, row=2, pady=5)
        Tempmg2.grid(column=3, row=2 )

        tempmtitg3.grid(column=2, row=2, pady=5)
        Tempmg3.grid(column=3, row=2 )

        tempmtitg4.grid(column=2, row=2, pady=5)
        Tempmg4.grid(column=3, row=2 )

    if estvar.get() == True:
        esttitg2.grid_forget()
        esttxtg2.grid_forget()
        estformg2.grid_forget()
        formtitg2.grid_forget()
        formentg2.grid_forget()
        txtentg2.grid_forget()
        txttitg2.grid_forget()

        esttitg3.grid_forget()
        esttxtg3.grid_forget()
        estformg3.grid_forget()
        formtitg3.grid_forget()
        formentg3.grid_forget()
        txtentg3.grid_forget()
        txttitg3.grid_forget()

        esttitg4.grid_forget()
        esttxtg4.grid_forget()
        estformg4.grid_forget()
        formtitg4.grid_forget()
        formentg4.grid_forget()
        txtentg4.grid_forget()
        txttitg4.grid_forget()
    else:
        esttitg2.grid(column=4, row=0, padx=5)
        esttxtg2.grid(column=4, row=1,padx=5)
        estformg2.grid(column=4, row=2,padx=5)

        esttitg3.grid(column=4, row=0, padx=5)
        esttxtg3.grid(column=4, row=1,padx=5)
        estformg3.grid(column=4, row=2,padx=5)

        esttitg4.grid(column=4, row=0, padx=5)
        esttxtg4.grid(column=4, row=1,padx=5)
        estformg4.grid(column=4, row=2,padx=5)

    if matvar.get() == True:
        matnameg2.grid_forget()
        matg2.grid_forget()

        matnameg3.grid_forget()
        matg3.grid_forget()

        matnameg4.grid_forget()
        matg4.grid_forget()
    else:
        matnameg2.grid(column=0, row=0)
        matg2.grid(column=0, row=1)

        matnameg3.grid(column=0, row=0)
        matg3.grid(column=0, row=1)

        matnameg4.grid(column=0, row=0)
        matg4.grid(column=0, row=1)

    if fluvar.get() == True:
        flunameg2.grid_forget()
        flug2.grid_forget()

        flunameg3.grid_forget()
        flug3.grid_forget()

        flunameg4.grid_forget()
        flug4.grid_forget()
    else:
        flunameg2.grid(column=1, row=0)
        flug2.grid(column=1, row=1, padx=10)

        flunameg3.grid(column=1, row=0)
        flug3.grid(column=1, row=1, padx=10)

        flunameg4.grid(column=1, row=0)
        flug4.grid(column=1, row=1, padx=10)

def updateg1():
    x1=estg1.get()
    if x1 == 1:
        formtitg1.grid_forget()
        formentg1.grid_forget()
        txtentg1.grid(column=5,row=1,padx=5)
        txttitg1.grid(column=5, row=0)
    if x1 == 2:
        txtentg1.grid_forget()
        txttitg1.grid_forget()
        formtitg1.grid(column=5,row=0)
        formentg1.grid(column=5,row=1,padx=5)

def updateg2():
    x2=estg2.get()
    if x2 == 1:
        formtitg2.grid_forget()
        formentg2.grid_forget()
        txtentg2.grid(column=5,row=1,padx=5)
        txttitg2.grid(column=5, row=0)
    if x2 == 2:
        txtentg2.grid_forget()
        txttitg2.grid_forget()
        formtitg2.grid(column=5,row=0)
        formentg2.grid(column=5,row=1,padx=5)

def updateg3():
    x3=estg3.get()
    if x3 == 1:
        formtitg3.grid_forget()
        formentg3.grid_forget()
        txtentg3.grid(column=5,row=1,padx=5)
        txttitg3.grid(column=5, row=0)
    if x3 == 2:
        txtentg3.grid_forget()
        txttitg3.grid_forget()
        formtitg3.grid(column=5,row=0)
        formentg3.grid(column=5,row=1,padx=5)

def updateg4():
    x4=estg4.get()
    if x4 == 1:
        formtitg4.grid_forget()
        formentg4.grid_forget()
        txtentg4.grid(column=5,row=1,padx=5)
        txttitg4.grid(column=5, row=0)
    if x4 == 2:
        txtentg4.grid_forget()
        txttitg4.grid_forget()
        formtitg4.grid(column=5,row=0)
        formentg4.grid(column=5,row=1,padx=5)

#defs Menu
def openProjeto():
    Projetowin = Toplevel(root)
    Projetowin.title("Sobre o projeto")
    projframe = ttk.Frame(Projetowin, padding=(5, 5, 10, 10))
    projframe.grid(column=0, row=0, sticky=(N,W,E,S))
    Projetowin.grid_columnconfigure(0, weight=1)
    Projetowin.grid_rowconfigure(0,weight=1)

    projtxt=ttk.Label(projframe, text ="Sobre o projeto em Python de um simulador de resfriamento de chapas finas")
    projtxt.grid(column=0,row=0)
    Projetowin.mainloop()

def openIntegrantes():
    Integranteswin = Toplevel(root)
    Integranteswin.title("Sobre os integrantes")
    inteframe = ttk.Frame(Integranteswin, padding=(5, 5, 10, 10))
    inteframe.grid(column=0, row=0, sticky=(N,W,E,S))
    Integranteswin.grid_columnconfigure(0, weight=1)
    Integranteswin.grid_rowconfigure(0,weight=1)

    projtxt=ttk.Label(inteframe, text ="Sobre os integrantes:\n Enzo \n Gabriela \n Lucas \n Matheus \n Pedro \n Rafael")
    projtxt.grid(column=0,row=0)
    Integranteswin.mainloop()

#interface
root=Tk()
root.title('Simulador de resfriamento de chapas finas')

mainframe = ttk.Frame(root, padding=(5, 5, 10, 10))
mainframe.grid(column=0, row=0, sticky=(N,W,E,S))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0,weight=1)

root.tk.call('source', 'breeze.tcl')
style=ttk.Style(root)
style.theme_use('Breeze')

#Menu
menubar = Menu(root)
root.config(menu=menubar)

about_menu= Menu(menubar, tearoff=False)
about_menu.add_command(label='Projeto', command=openProjeto)
about_menu.add_command(label='Integrantes', command=openIntegrantes)
menubar.add_cascade(
    label="Sobre",
    menu=about_menu,
    underline=0
)

help_menu = Menu(menubar, tearoff=False)
help_menu.add_command(label='Manual')
menubar.add_cascade(
    label="Ajuda",
    menu=help_menu,
    underline=0
)

#Inicio
frametit=ttk.Frame(mainframe, padding=(5, 5, 10, 10))
frametit.grid(column=0,row=0, sticky=(N, W))
img = Image.open("USP.gif")
img = img.resize((75,90), Image.ANTIALIAS)
photoImg =  ImageTk.PhotoImage(img)
eelicon=ttk.Label(frametit, image=photoImg)
eelicon.grid(column=0,row=0,sticky=(N,W))

titulo = ttk.Label(frametit, text="Simulador de resfriamento de chapas finas:", font=('Oswald',16))
titulo.grid(column=1, row=0, sticky=(W),padx=20)

butao=ttk.Button(mainframe, text="Simular", width=15, command=chama)
butao.grid(column=1 , row=0 , sticky=(E))

framein=ttk.Frame(mainframe, padding=(5, 5, 10, 10))
framein.grid(column=0,row=1, sticky=(W))

frase1 = ttk.Label(framein, text="• Utilizar quais dados iguais para todas as chapas:  ")
frase1.grid(column=0, row=0, sticky=(W))
Tempxvar = BooleanVar(value=False)
checkTempx= ttk.Checkbutton(framein, text='Temperatura de estampagem', variable=Tempxvar, onvalue=True, offvalue=False, command=updatecheck)
checkTempx.grid(column=3, row=0, sticky=(W))
Tempmvar = BooleanVar(value=False)
checkTempm= ttk.Checkbutton(framein, text='Temperatura inicial', variable=Tempmvar, onvalue=True, offvalue=False, command=updatecheck)
checkTempm.grid(column=2, row=0, sticky=(W))
matvar = BooleanVar(value=False)
checkmat= ttk.Checkbutton(framein, text='Material', variable=matvar, onvalue=True, offvalue=False, command=updatecheck)
checkmat.grid(column=1, row=1, sticky=(W))
fluvar = BooleanVar(value=False)
checkflu= ttk.Checkbutton(framein, text='Fluido', variable=fluvar, onvalue=True, offvalue=False, command=updatecheck)
checkflu.grid(column=3, row=1, sticky=(W))
estvar = BooleanVar(value=False)
checkest= ttk.Checkbutton(framein, text='Estampagem', variable=estvar, onvalue=True, offvalue=False, command=updatecheck)
checkest.grid(column=1, row=0, sticky=(W))
Tempavar = BooleanVar(value=False)
checkTempa= ttk.Checkbutton(framein, text='Temperatura do fluido', variable=Tempavar, onvalue=True, offvalue=False, command=updatecheck)
checkTempa.grid(column=2, row=1, sticky=(W))

#Grafico 1

titulog1 = ttk.Label(mainframe, text="Chapa 1:", font=('Oswald',14))
titulog1.grid(column=0, row=3, sticky=(W))
frameg1=ttk.Frame(mainframe, padding=(5, 5, 10, 10))
frameg1.grid(column=0, row=4)

matnameg1= ttk.Label(frameg1, text="• Material")
matnameg1.grid(column=0, row=0)
matg1var= StringVar()
matg1= ttk.Combobox(frameg1, textvariable=matg1var)
matg1['values']=('Cobre','Ferro','Alumínio','Bronze','Ouro','Prata','Aço','Chumbo')
matg1.grid(column=0, row=1)

flunameg1= ttk.Label(frameg1, text="• Fluido")
flunameg1.grid(column=1, row=0)
flug1var= StringVar()
flug1= ttk.Combobox(frameg1, textvariable=flug1var)
flug1['values']=('Ar','Ar em movimento','Água','Água em movimento','Óleo','Óleo em movimento')
flug1.grid(column=1, row=1, padx=10)

tempatitg1=ttk.Label(frameg1, text="• Temperatura do fluido:")
tempatitg1.grid(column=2, row=0, pady=5)
Tempambg1= StringVar()
Tempag1=ttk.Entry(frameg1, width=7, textvariable=Tempambg1)
Tempag1.grid(column=3, row=0 )

tempxtitg1=ttk.Label(frameg1, text="• Temperatura de estampagem:")
tempxtitg1.grid(column=2, row=1, pady=5)
Tempmaxg1= StringVar()
Tempxg1=ttk.Entry(frameg1, width=7, textvariable=Tempmaxg1)
Tempxg1.grid(column=3, row=1 )

tempmtitg1=ttk.Label(frameg1, text="• Temperatura inicial:")
tempmtitg1.grid(column=2, row=2, pady=5)
Tempming1= StringVar()
Tempmg1=ttk.Entry(frameg1, width=7, textvariable=Tempming1)
Tempmg1.grid(column=3, row=2 )

estg1=IntVar()
esttitg1=ttk.Label(frameg1, text="• Estampagem:")
esttitg1.grid(column=4, row=0, padx=20, sticky=(W))

esttxtg1=ttk.Radiobutton(frameg1, text="Em texto", variable=estg1, value=1, command=updateg1)
esttxtg1.grid(column=4, row=1,padx=20,sticky=(W))
txtg1=StringVar()
txttitg1=ttk.Label(frameg1, text='• Texto:')
txtentg1=ttk.Entry(frameg1, width=7, textvariable=txtg1)

estformg1=ttk.Radiobutton(frameg1, text="Formas geométricas", variable=estg1, value=2, command=updateg1)
estformg1.grid(column=4, row=2,padx=20, sticky=(W))
formg1=StringVar()
formtitg1=ttk.Label(frameg1, text='• Forma:')
formentg1=ttk.Combobox(frameg1, textvariable=formg1)
formentg1['values']=('Anel circular','Anel elíptico','Círculo','Furo circular')

#Grafico 2

titulog2 = ttk.Label(mainframe, text="Chapa 2:", font=('Oswald',14))
titulog2.grid(column=0, row=5, sticky=(W))
frameg2=ttk.Frame(mainframe, padding=(5, 5, 10, 10))
frameg2.grid(column=0, row=6)

matnameg2= ttk.Label(frameg2, text="• Material")
matnameg2.grid(column=0, row=0)
matg2var= StringVar()
matg2= ttk.Combobox(frameg2, textvariable=matg2var)
matg2['values']=('Cobre','Ferro','Alumínio','Bronze','Ouro','Prata','Aço','Chumbo')
matg2.grid(column=0, row=1)

flunameg2= ttk.Label(frameg2, text="• Fluido")
flunameg2.grid(column=1, row=0)
flug2var= StringVar()
flug2= ttk.Combobox(frameg2, textvariable=flug2var)
flug2['values']=('Ar','Ar em movimento','Água','Água em movimento','Óleo','Óleo em movimento')
flug2.grid(column=1, row=1, padx=10)

tempatitg2=ttk.Label(frameg2, text="• Temperatura do fluido:")
tempatitg2.grid(column=2, row=0, pady=5)
Tempambg2= StringVar()
Tempag2=ttk.Entry(frameg2, width=7, textvariable=Tempambg2)
Tempag2.grid(column=3, row=0 )

tempxtitg2=ttk.Label(frameg2, text="• Temperatura de estampagem:")
tempxtitg2.grid(column=2, row=1, pady=5)
Tempmaxg2= StringVar()
Tempxg2=ttk.Entry(frameg2, width=7, textvariable=Tempmaxg2)
Tempxg2.grid(column=3, row=1 )

tempmtitg2=ttk.Label(frameg2, text="• Temperatura inicial:")
tempmtitg2.grid(column=2, row=2, pady=5)
Tempming2= StringVar()
Tempmg2=ttk.Entry(frameg2, width=7, textvariable=Tempming2)
Tempmg2.grid(column=3, row=2 )

estg2=IntVar()
esttitg2=ttk.Label(frameg2, text="• Estampagem:")
esttitg2.grid(column=4, row=0, padx=20, sticky=(W))

esttxtg2=ttk.Radiobutton(frameg2, text="Em texto", variable=estg2, value=1, command=updateg2)
esttxtg2.grid(column=4, row=1,padx=20,sticky=(W))
txtg2=StringVar()
txttitg2=ttk.Label(frameg2, text='• Texto:')
txtentg2=ttk.Entry(frameg2, width=7, textvariable=txtg2)

estformg2=ttk.Radiobutton(frameg2, text="Formas geométricas", variable=estg2, value=2, command=updateg2)
estformg2.grid(column=4, row=2,padx=20, sticky=(W))
formg2=StringVar()
formtitg2=ttk.Label(frameg2, text='• Forma:')
formentg2=ttk.Combobox(frameg2, textvariable=formg2)
formentg2['values']=('Anel circular','Anel elíptico','Círculo','Furo circular')

#Grafico 3

titulog3 = ttk.Label(mainframe, text="Chapa 3:", font=('Oswald',14))
titulog3.grid(column=0, row=7, sticky=(W))
frameg3=ttk.Frame(mainframe, padding=(5, 5, 10, 10))
frameg3.grid(column=0, row=8)

matnameg3= ttk.Label(frameg3, text="• Material")
matnameg3.grid(column=0, row=0)
matg3var= StringVar()
matg3= ttk.Combobox(frameg3, textvariable=matg3var)
matg3['values']=('Cobre','Ferro','Alumínio','Bronze','Ouro','Prata','Aço','Chumbo')
matg3.grid(column=0, row=1)

flunameg3= ttk.Label(frameg3, text="• Fluido")
flunameg3.grid(column=1, row=0)
flug3var= StringVar()
flug3= ttk.Combobox(frameg3, textvariable=flug3var)
flug3['values']=('Ar','Ar em movimento','Água','Água em movimento','Óleo','Óleo em movimento')
flug3.grid(column=1, row=1, padx=10)

tempatitg3=ttk.Label(frameg3, text="• Temperatura do fluido:")
tempatitg3.grid(column=2, row=0, pady=5)
Tempambg3= StringVar()
Tempag3=ttk.Entry(frameg3, width=7, textvariable=Tempambg3)
Tempag3.grid(column=3, row=0 )

tempxtitg3=ttk.Label(frameg3, text="• Temperatura de estampagem:")
tempxtitg3.grid(column=2, row=1, pady=5)
Tempmaxg3= StringVar()
Tempxg3=ttk.Entry(frameg3, width=7, textvariable=Tempmaxg3)
Tempxg3.grid(column=3, row=1 )

tempmtitg3=ttk.Label(frameg3, text="• Temperatura inicial:")
tempmtitg3.grid(column=2, row=2, pady=5)
Tempming3= StringVar()
Tempmg3=ttk.Entry(frameg3, width=7, textvariable=Tempming3)
Tempmg3.grid(column=3, row=2 )

estg3=IntVar()
esttitg3=ttk.Label(frameg3, text="• Estampagem:")
esttitg3.grid(column=4, row=0, padx=20, sticky=(W))

esttxtg3=ttk.Radiobutton(frameg3, text="Em texto", variable=estg3, value=1, command=updateg3)
esttxtg3.grid(column=4, row=1,padx=20,sticky=(W))
txtg3=StringVar()
txttitg3=ttk.Label(frameg3, text='• Texto:')
txtentg3=ttk.Entry(frameg3, width=7, textvariable=txtg3)

estformg3=ttk.Radiobutton(frameg3, text="Formas geométricas", variable=estg3, value=2, command=updateg3)
estformg3.grid(column=4, row=2,padx=20, sticky=(W))
formg3=StringVar()
formtitg3=ttk.Label(frameg3, text='• Forma:')
formentg3=ttk.Combobox(frameg3, textvariable=formg3)
formentg3['values']=('Anel circular','Anel elíptico','Círculo','Furo circular')

#Grafico 4

titulog4 = ttk.Label(mainframe, text="Chapa 4:", font=('Oswald',14))
titulog4.grid(column=0, row=9, sticky=(W))
frameg4=ttk.Frame(mainframe, padding=(5, 5, 10, 10))
frameg4.grid(column=0, row=10)

matnameg4= ttk.Label(frameg4, text="• Material")
matnameg4.grid(column=0, row=0)
matg4var= StringVar()
matg4= ttk.Combobox(frameg4, textvariable=matg4var)
matg4['values']=('Cobre','Ferro','Alumínio','Bronze','Ouro','Prata','Aço','Chumbo')
matg4.grid(column=0, row=1)

flunameg4= ttk.Label(frameg4, text="• Fluido")
flunameg4.grid(column=1, row=0)
flug4var= StringVar()
flug4= ttk.Combobox(frameg4, textvariable=flug4var)
flug4['values']=('Ar','Ar em movimento','Água','Água em movimento','Óleo','Óleo em movimento')
flug4.grid(column=1, row=1, padx=10)

tempatitg4=ttk.Label(frameg4, text="• Temperatura do fluido:")
tempatitg4.grid(column=2, row=0, pady=5)
Tempambg4= StringVar()
Tempag4=ttk.Entry(frameg4, width=7, textvariable=Tempambg4)
Tempag4.grid(column=3, row=0 )

tempxtitg4=ttk.Label(frameg4, text="• Temperatura de estampagem:")
tempxtitg4.grid(column=2, row=1, pady=5)
Tempmaxg4= StringVar()
Tempxg4=ttk.Entry(frameg4, width=7, textvariable=Tempmaxg4)
Tempxg4.grid(column=3, row=1 )

tempmtitg4=ttk.Label(frameg4, text="• Temperatura inicial:")
tempmtitg4.grid(column=2, row=2, pady=5)
Tempming4= StringVar()
Tempmg4=ttk.Entry(frameg4, width=7, textvariable=Tempming4)
Tempmg4.grid(column=3, row=2 )

estg4=IntVar()
esttitg4=ttk.Label(frameg4, text="• Estampagem:")
esttitg4.grid(column=4, row=0, padx=20, sticky=(W))

esttxtg4=ttk.Radiobutton(frameg4, text="Em texto", variable=estg4, value=1, command=updateg4)
esttxtg4.grid(column=4, row=1,padx=20,sticky=(W))
txtg4=StringVar()
txttitg4=ttk.Label(frameg4, text='• Texto:')
txtentg4=ttk.Entry(frameg4, width=7, textvariable=txtg4)

estformg4=ttk.Radiobutton(frameg4, text="Formas geométricas", variable=estg4, value=2, command=updateg4)
estformg4.grid(column=4, row=2,padx=20, sticky=(W))
formg4=StringVar()
formtitg4=ttk.Label(frameg4, text='• Forma:')
formentg4=ttk.Combobox(frameg4, textvariable=formg4)
formentg4['values']=('Anel circular','Anel elíptico','Círculo','Furo circular')

root.mainloop()


import argparse
import torch

import models_torch as models
import utils

def inference(parameters, verbose=True):

    # resolve device
    device = torch.device(
        "cuda:{}".format(parameters["gpu_number"]) if parameters["device_type"] == "gpu"
        else "cpu"
    )

    # load input images
    datum_l_cc = utils.load_images(parameters['image_path'], 'L-CC')
    datum_r_cc = utils.load_images(parameters['image_path'], 'R-CC')
    datum_l_mlo = utils.load_images(parameters['image_path'], 'L-MLO')
    datum_r_mlo = utils.load_images(parameters['image_path'], 'R-MLO')

    # construct models and prepare data
    if parameters["model_type"] == 'cnn':
        model = models.BaselineBreastModel(device, nodropout_probability=1.0, gaussian_noise_std=0.0).to(device)
        model.load_state_dict(torch.load(parameters["model_path"]))
        x = {
            "L-CC": torch.Tensor(datum_l_cc).permute(0, 3, 1, 2).to(device),
            "L-MLO": torch.Tensor(datum_l_mlo).permute(0, 3, 1, 2).to(device),
            "R-CC": torch.Tensor(datum_r_cc).permute(0, 3, 1, 2).to(device),
            "R-MLO": torch.Tensor(datum_r_mlo).permute(0, 3, 1, 2).to(device),
        }
    elif parameters["model_type"] == 'histogram':
        model = models.BaselineHistogramModel(num_bins=parameters["bins_histogram"]).to(device)
        model.load_state_dict(torch.load(parameters["model_path"]))
        x = torch.Tensor(utils.histogram_features_generator([
            datum_l_cc, datum_r_cc, datum_l_mlo, datum_r_mlo
        ], parameters)).to(device)
    else:
        raise RuntimeError(parameters["model_type"])

    # run prediction
    with torch.no_grad():
        prediction_density = model(x).cpu().numpy()

    if verbose:
        # nicely prints out the predictions
        print('Density prediction:\n'
              '\tAlmost entirely fatty (0):\t\t\t' + str(prediction_density[0, 0]) + '\n'
              '\tScattered areas of fibroglandular density (1):\t' + str(prediction_density[0, 1]) + '\n'
              '\tHeterogeneously dense (2):\t\t\t' + str(prediction_density[0, 2]) + '\n'
              '\tExtremely dense (3):\t\t\t\t' + str(prediction_density[0, 3]) + '\n')

    return prediction_density[0]

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Run Inference')
    parser.add_argument('model_type')
    parser.add_argument('--bins-histogram', default=50)
    parser.add_argument('--model-path', default=None)
    parser.add_argument('--device-type', default="cpu")
    parser.add_argument('--image-path', default="images/")
    args = parser.parse_args()

    parameters_ = {
        "model_type": args.model_type,
        "bins_histogram": args.bins_histogram,
        "model_path": args.model_path,
        "device_type": args.device_type,
        "image_path": args.image_path,
    }

    if parameters_["model_path"] is None:
        if args.model_type == "histogram":
            parameters_["model_path"] = "saved_models/BreastDensity_BaselineHistogramModel/model.p"
        if args.model_type == "cnn":
            parameters_["model_path"] = "saved_models/BreastDensity_BaselineBreastModel/model.p"

    inference(parameters_)

"""
python density_model_torch.py histogram
python density_model_torch.py cnn
"""


"""move osm id generation to loader code

Revision ID: 106942da4dcc
Revises: ae776f283429
Create Date: 2022-03-29 17:52:28.916521

"""
import os

from alembic import op

here = os.path.dirname(os.path.realpath(__file__))

# revision identifiers, used by Alembic.
revision = "106942da4dcc"
down_revision = "8832d7a1b5fb"
branch_labels = None
depends_on = None

revision_dir = f"{here}/{revision}"

# idea from https://github.com/tbobm/alembic-sequeled
def process_migration(script_name: str):
    filename = f"{revision_dir}/{script_name}.sql"

    query = "\n".join(open(filename))
    if len(query) > 0:
        op.execute(query)

def upgrade():
    process_migration("upgrade")

def downgrade():
    process_migration("downgrade")


from PyQt6.QtWidgets import QApplication, QLabel, QWidget

app = QApplication([])

window = QWidget()
window.setWindowTitle("PyQt App")
window.resize(300, 100)
msg = QLabel("
Hello, World!
", parent=window)

window.show()
app.exec()


from itertools import permutations

numbers = "011"

def isprime(n):
    if(n == 0):
        return False
    elif(n == 1):
        return False
    else:
        for i in range(2, n):
            if(n % i == 0):
                return False
                break
    return True

def solution(numbers):
    answer = 0
    temp = []

    for l in range(1, len(numbers)+1):
        pm = list(permutations(numbers, l))
        for el in pm:
            temp.append(int("".join(el)))

    temp = list(set(temp))
    for i in range(len(temp)):
        if(isprime(temp[i])):
            answer += 1

    return answer

solution(numbers)


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

import pydot
import pandas as pd
import numpy as np
import seaborn as sns
sns.set()

import matplotlib
import matplotlib.pyplot as plt

# Ignore warnings
import warnings
warnings.filterwarnings('ignore')

import sklearn
from sklearn import metrics
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import export_graphviz, DecisionTreeClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.exceptions import NotFittedError

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras import optimizers
from keras.wrappers.scikit_learn import KerasClassifier

from IPython.display import display

#Plotting Display Functions
def display_confusion_matrix(target, prediction, score=None):
    cm = metrics.confusion_matrix(target, prediction)
    plt.figure(figsize=(6,6))
    sns.heatmap(cm, annot=True, fmt=".3f", linewidths=.5, square=True, cmap='Blues_r')
    plt.ylabel('Actual label')
    plt.xlabel('Predicted label')
    if score:
        score_title = 'Accuracy Score: {0}'.format(round(score, 5))
        plt.title(score_title, size = 14)
    classification_report = pd.DataFrame.from_dict(metrics.classification_report(target, prediction, output_dict=True), orient='index')
    display(classification_report.round(2))

# Importing the dataset
dataset = pd.read_csv('path to training csv dataset')
X = dataset.drop(['OBJECTID', 'Cluster'], axis=1)
y = dataset['Cluster']

# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

#################Logistic Regression CLASSIFIER

# Create and train model on train data sample
lg = LogisticRegression(solver='lbfgs', random_state=42)
lg.fit(X_train, y_train)

#Predict for test data sample
logistic_prediction = lg.predict(X_test)

#Compute error between predicted data and true response and display it in confusion matrix
score = metrics.accuracy_score(y_test, logistic_prediction)
display_confusion_matrix(y_test, logistic_prediction, score=score)

from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,logistic_prediction))
print(classification_report(y_test,logistic_prediction))

################Decision Tree

dt = DecisionTreeClassifier(min_samples_split=15, min_samples_leaf=20, random_state=42)
dt.fit(X_train, y_train)
dt_prediction = dt.predict(X_test)

score = metrics.accuracy_score(y_test, dt_prediction)
display_confusion_matrix(y_test, dt_prediction, score=score)

from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,dt_prediction))
print(classification_report(y_test,dt_prediction))

############### SVM CLASSIFIER

svm = SVC(gamma='auto', random_state=42)
svm.fit(X_train, y_train)
svm_prediction = svm.predict(X_test)

score = metrics.accuracy_score(y_test, svm_prediction)
display_confusion_matrix(y_test, svm_prediction, score=score)

from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,svm_prediction))
print(classification_report(y_test,svm_prediction))

############# RANDOM fOREST CLASSIFIER
rf = RandomForestClassifier(n_estimators=200, random_state=42)
rf.fit(X_train, y_train)
rf_prediction = rf.predict(X_test)

score = metrics.accuracy_score(y_test, rf_prediction)
display_confusion_matrix(y_test, rf_prediction, score=score)

from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,rf_prediction))
print(classification_report(y_test,rf_prediction))

############# Multilayer Perceprton (ANN)
def build_ann(optimizer='adam'):

    # Initializing our ANN
    model = Sequential()

    # Adding the input layer and the first hidden layer of our ANN with dropout
    model.add(Dense(output_dim = 3, init = 'uniform', activation = 'relu',input_dim = 3))

    # Adding the second hidden layer
    model.add(Dense(output_dim = 3, init = 'uniform', activation = 'relu'))

    # Adding the output layer
    model.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))

    # Compiling the ANN
    model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

    return model

    # Initializing our ANN
    ann = Sequential()

    # Adding the input layer and the first hidden layer of our ANN with dropout
    ann.add(Dense(output_dim = 3, init = 'uniform', activation = 'relu', input_dim = 3))

    # Adding the second hidden layer
    ann.add(Dense(output_dim = 3, init = 'uniform', activation = 'relu'))

    # Adding the output layer
    ann.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))

    # Compiling the ANN
    ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

    return ann

opt = optimizers.Adam(lr=0.001)
ann = build_ann(opt)

 #Fitting the ANN to the Training set
ann.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)

 #Predicting the Test set results
ann_prediction = ann.predict(X_test)
ann_prediction = (ann_prediction > 0.5)

score = metrics.accuracy_score(y_test, ann_prediction)
display_confusion_matrix(y_test, ann_prediction, score=score)

from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,ann_prediction))
print(classification_report(y_test,ann_prediction))

######################K-FOLD CROSS VALIDATION#######################

n_folds = 10
cv_score_lg = cross_val_score(estimator=lg, X=X_train, y=y_train, cv=n_folds, n_jobs=-1)
cv_score_dt = cross_val_score(estimator=dt, X=X_train, y=y_train, cv=n_folds, n_jobs=-1)
cv_score_svm = cross_val_score(estimator=svm, X=X_train, y=y_train, cv=n_folds, n_jobs=-1)
cv_score_rf = cross_val_score(estimator=rf, X=X_train, y=y_train, cv=n_folds, n_jobs=-1)
cv_score_ann = cross_val_score(estimator=KerasClassifier(build_fn=build_ann, batch_size=16, epochs=20, verbose=0), X=X_train, y=y_train, cv=n_folds, n_jobs=-1)

cv_result = {'lg': cv_score_lg, 'dt': cv_score_dt, 'svm': cv_score_svm, 'rf': cv_score_rf, 'ann': cv_score_ann}
cv_data = {model: [score.mean(), score.std()] for model, score in cv_result.items()}
cv_df = pd.DataFrame(cv_data, index=['Mean_accuracy', 'Variance'])
cv_df

plt.figure(figsize=(20,8))
plt.plot(cv_result['lg'])
plt.plot(cv_result['dt'])
plt.plot(cv_result['svm'])
plt.plot(cv_result['rf'])
plt.plot(cv_result['ann'])
plt.title('Models Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Trained fold')
plt.xticks([k for k in range(n_folds)])
plt.legend(['logreg', 'tree', 'randomforest', 'ann', 'svm'], loc='upper left')
plt.show()

#####################TEST ON NEW DATASET

test_df_raw = pd.read_csv('Test.csv')
test = test_df_raw.copy()
test.head()

# Create and train model on train data sample
model_test = ann()
model_test.fit(X, y)

# Predict for test data sample
prediction = model_test.predict(test)

from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,ann_prediction))
print(classification_report(y_test,ann_prediction))

result_df = test_df_raw.copy()
result_df['Cluster'] = prediction
result_df.to_csv('Test.csv', columns=['Cluster'], index=False)

Classification of Informal Buildings/Classification.py


from bs4 import BeautifulSoup
from csv import DictWriter
import requests

"""
grab links from rithm schol blog
Data: store URL, anchor tag text and date into CSV
"""

url = 'https://www.rithmschool.com/blog'
i = 2
with open('posts.csv', 'w') as file:
    f_names = ['title', 'link', 'date']
    writer = DictWriter(file, fieldnames=f_names)
    writer.writeheader()

    while True:
        response = requests.get(url)
        content = response.text
        print(f'Scraping posts data from: {url}...')
        soup = BeautifulSoup(content, 'html.parser')
        if soup.find('p', attrs={'class': 'lead'}).text.strip() == 'No matches found':
            break
        articles = soup.find_all("article")
        for article in articles:
            link = article.find("a")['href']
            title = article.find("a").text.strip()
            date = article.find("time")['datetime']
            writer.writerow({'title': title,
                             'link': link,
                             'date': date
                             })
        print(f'Scraped from {url} successful.')
        url = f'https://www.rithmschool.com/blog?page={i}'
        i += 1


from discord.ext import commands
from random import randint
from textblob import TextBlob

class user_message(commands.Cog):
    phrases = {}
    def __init__(self, bot):
        self.bot = bot

        file = open("texts/greet.txt")
        self.phrases["greet"] = file.read().splitlines()
        file.close()

        file = open("texts/negative.txt")
        self.phrases["negative"] = file.read().splitlines()
        file.close()

        file = open("texts/positive.txt")
        self.phrases["positive"] = file.read().splitlines()
        file.close()

    def check_valid_message(self, message):
        return message.author != self.bot.user and str(message.channel.type) != "private"

    async def is_command(self, message):
        return str(message.channel.type) == "private"

    async def print_negative_message(self, message):
        negatives = self.phrases["negative"]
        await message.channel.send(negatives[randint(0, len(negatives) - 1)])

    async def print_positive_message(self, message):
        positives = self.phrases["positive"]
        await message.channel.send(positives[randint(0, len(positives) - 1)])

    async def handle_message(self, message):
        if "seraphine" in message.content.lower() and TextBlob(message.content).sentiment.polarity >= 0 and not message.content.startswith("$"):
            await self.print_positive_message(message)
        elif "seraphine" in message.content.lower() and not message.content.startswith("$"):
            await self.print_negative_message(message)

    @commands.Cog.listener()
    async def on_message(self, message):
        if not self.check_valid_message(message):
            return
        await self.handle_message(message)
def setup(bot):
    bot.add_cog(user_message(bot))


from django.shortcuts import get_object_or_404
from ninja import Router
from ninja.orm import create_schema
from typing import List
from apiv1.models import Company, Category, Product

product_router = Router(tags=['product'])

ProductIn = create_schema(Product, exclude=['id'])
ProductOut = create_schema(Product)

@product_router.post('')
def create_product(request, payload: ProductIn):
    print(payload.dict())
    test = payload.dict()
    test['company'] = get_object_or_404(Company, id=test['company'])
    test['category'] = get_object_or_404(Category, id=test['category'])
    product = Product.objects.create(**test)
    return {'id': product.id}

@product_router.get('/{id}', response=ProductOut)
def get_product(request, id: int):
    product = get_object_or_404(Product, id=id, is_active=True)
    return product

@product_router.get('', response=List[ProductOut])
def list_products(request):
    if request.user.groups.filter(name='SuperAdmin').exists():
        qs = Product.objects.all()
        return qs
    else:
        qs = Product.objects.filter(is_active=True)
        return qs

@product_router.put('/{id}')
def update_product(request, id: int, payload: ProductIn):
    product = get_object_or_404(Product, id=id)
    test = payload.dict()
    test['company'] = get_object_or_404(Company, id=test['company'])
    test['category'] = get_object_or_404(Category, id=test['category'])
    for attr, value in test.items():
        setattr(product, attr, value)
    product.save()
    return {"success": True}

@product_router.delete('/{id}')
def delete_product(request, id: int):
    product = get_object_or_404(Product, id=id)
    product.delete()
    return {"success": True}


from datetime import datetime

from requests import Response, Session

from . import __version__
from .constants import (
    DAY_END,
    DAY_START,
    DEFAULT_DAY,
    DEFAULT_YEAR,
    URL_PATTERN,
    YEAR_END,
    YEAR_START,
)
from .io import get_session_cookie

class AoCSession(Session):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.headers.update({"User-Agent": f"aocli/{__version__}"})
        self.cookies.update(get_session_cookie(raise_on_missing=False))

    def request(self, method: str, url: str, raise_for_status: bool = False, *args, **kwargs) -> Response:  # type: ignore
        # validate url
        year, day = URL_PATTERN.match(url).groups()  # type: ignore
        year = int(year)
        day = int(day)

        # check for year
        if not YEAR_START <= year <= YEAR_END:
            raise ValueError(
                f"Invalid year: {year}. Must be between {YEAR_START} and {YEAR_END}."
            )
        # check for day
        if not DAY_START <= day <= DAY_END:
            raise ValueError(
                f"Invalid day: {day}. Must be between {DAY_START} and {DAY_END}."
            )
        if year == DEFAULT_YEAR:
            if day > DEFAULT_DAY:
                raise ValueError(
                    f"Invalid day: {day} for year {year}. Must be between 1 and {DEFAULT_DAY}."
                )
            elif day == DEFAULT_DAY and datetime.utcnow().hour - 5 < 0:
                raise ValueError(
                    f"Invalid day: {day} for year {year}. Must be between 1 and {DEFAULT_DAY-1}."
                )

        resp = super().request(method, url, *args, **kwargs)
        if raise_for_status:
            resp.raise_for_status()
        return resp

    def get(self, url: str, *args, raise_for_status: bool = False, **kwargs) -> Response:  # type: ignore
        return self.request("GET", url, *args, raise_for_status, **kwargs)

    def post(self, url: str, *args, raise_for_status: bool = False, **kwargs) -> Response:  # type: ignore
        return self.request("POST", url, *args, raise_for_status, **kwargs)

# initialize Session
session = AoCSession()


from basic.blog.managers import PublicManager
from basic.media.models import Photo
from django.contrib.auth.models import User
from django.db import models
from django.db.models import permalink
from django.utils.translation import ugettext_lazy as _
from tagging.fields import TagField
from tinymce import models as tinymce
import tagging
from filebrowser import fields as filebrowser
import datetime
from django_extensions.db.fields import AutoSlugField

class Category(models.Model):
    """Category model."""
    title       = models.CharField(_('title'), max_length=100)
    slug        = models.SlugField(_('slug'), unique=True)

    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('categories')
        db_table = 'blog_categories'
        ordering = ('title',)

    class Admin:
        pass

    def __unicode__(self):
        return u'%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('blog_category_detail', None, {'slug': self.slug})

class Post(models.Model):
    """Post model."""
    STATUS_CHOICES = (
        (1, _('Draft')),
        (2, _('Public')),
    )
    title           = models.CharField(_('title'), max_length=200)
    slug            = AutoSlugField(_('slug'), editable=True, blank=False, populate_from='title')
    author          = models.ForeignKey(User, blank=True, null=True)
    thumb           = filebrowser.FileBrowseField(format='Image', max_length=200, blank=True, null=True)
    body            = tinymce.HTMLField(_('body'))
    tease           = models.TextField(_('tease'), blank=True)
    status          = models.IntegerField(_('status'), choices=STATUS_CHOICES, default=1)
    allow_comments  = models.BooleanField(_('allow comments'), default=True)
    publish         = models.DateTimeField(_('publish'), default=datetime.datetime.now())
    created         = models.DateTimeField(_('created'), auto_now_add=True)
    modified        = models.DateTimeField(_('modified'), auto_now=True)
    categories      = models.ForeignKey(Category, default=1)
    tags            = TagField()
    objects         = PublicManager()

    class Meta:
        verbose_name = _('post')
        verbose_name_plural = _('posts')
        db_table  = 'blog_posts'
        ordering  = ('-publish',)
        get_latest_by = 'publish'

    class Admin:
        list_display  = ('title', 'publish', 'status')
        list_filter   = ('publish', 'categories', 'status')
        search_fields = ('title', 'body')

    def __unicode__(self):
        return u'%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('blog_detail', None, {
            'year': self.publish.year,
            'month': self.publish.strftime('%b').lower(),
            'day': self.publish.day,
            'slug': self.slug
        })

    def get_previous_post(self):
        return self.get_previous_by_publish(status__gte=2)

    def get_next_post(self):
        return self.get_next_by_publish(status__gte=2)


from kaggle_environments import make, utils
from kaggle_environments.envs.connectx.connectx import renderer
import gym
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import datetime
import pytz
from stable_baselines3.common.env_checker import check_env
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.vec_env import SubprocVecEnv
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
from stable_baselines3.common.results_plotter import load_results, ts2xy
from stable_baselines3.common.monitor import Monitor
# from stable_baselines3.common import set_global_seeds

## define env for connectx trainig using stable-baselines3
class ConnectXEnv(gym.Env):

    def __init__(self, policy="negamax", turn="sente"):
        super(ConnectXEnv, self).__init__()
        self.env = make("connectx", debug=False)
        self.set_trainer(policy, turn)

        # アクション数定義
        ACTION_NUM = self.env.configuration["columns"]
        self.action_space = gym.spaces.Discrete(ACTION_NUM)

        # 状態の範囲を定義
        self.WIDTH = self.env.configuration["columns"]
        self.HEIGHT = self.env.configuration["rows"]

        self.obs_shape = [self.HEIGHT, self.WIDTH, 3] # treat as image
        self.observation_space = gym.spaces.Box(low=0, high=255, shape=self.obs_shape, dtype=np.uint8)

        self.reset()

    def set_trainer(self, policy, turn):
        if turn=="sente":
            self.trainer = self.env.train([None, policy])
        elif turn=="gote":
            self.trainer = self.env.train([policy, None])
        else:
            raise ValueError("Turn is incorrect! Select \"sente\" or \"gote\"!")

    def reset(self):
        state = self.trainer.reset()
        self.state = state
        self.board = state["board"]
        self.mark = state["mark"]
        self.done = False
        self.boardImg = self.board2img(self.board)

        return self.boardImg

    # kaggle_environment seems not to have env.seed()
    # def seed(self, seed):
    #     self.env.seed(seed)

    def step(self, action):
        action = int(action) # added this row in order to avoid error like "Invalid Action: 4 is not of type 'integer'"
        state, reward, done, info = self.trainer.step(action)

        self.state = state
        self.board = state["board"]
        self.boardImg = self.board2img(self.board)
        self.reward = -1 if reward==None else reward
        self.done = done

        return self.boardImg, self.reward, self.done, info

    def render(self, **kwargs):
        self.env.render(**kwargs)

    def board2img(self, board):
        tmp = np.array(board).reshape(self.obs_shape[:2])
        boardImg = np.zeros(self.obs_shape, dtype=np.uint8)
        boardImg[:,:,0] = np.array(tmp==self.mark)*255 # ch.0 : own mark
        boardImg[:,:,1] = np.array(tmp==(3-self.mark))*255 # ch.1 : opponent mark
        return boardImg

    def boardshow(self, verbose=0):
        plt.figure(figsize=(2,2))
        plt.imshow(self.boardImg)
        if verbose:
            print("red : own piece")
            print("green : opponent piece")

    def renderBoard(self):
        columns = self.env.configuration.columns
        rows = self.env.configuration.rows
        board = self.state["board"]

        def print_row(values, delim="|"):
            return f"{delim} " + f" {delim} ".join(str(v) for v in values) + f" {delim}\n"

        row_bar = "+" + "+".join(["---"] * columns) + "+\n"
        out = row_bar
        for r in range(rows):
            out = out + \
                print_row(board[r * columns: r * columns + columns]) + row_bar

        print(out)

# define functions to make wrapper for env
def make_ConnectXEnv(rank, seed=0, policy="random", turn="sente"):
    def _init():
        env = ConnectXEnv(policy, turn)
        # kaggle_environment seems not to have env.seed()
        # env.seed(seed+rank)
        return env
    # set_global_seeds(seed)
    return _init

# define custom CNN for stable-baselines3
class CustomCNN(BaseFeaturesExtractor):
    """
    :param observation_space: (gym.Space)
    :param features_dim: (int) Number of features extracted.
        This corresponds to the number of unit for the last layer.
    """

    def __init__(self, observation_space: gym.spaces.Box, features_dim: int = 256):
        super(CustomCNN, self).__init__(observation_space, features_dim)
        # We assume CxHxW images (channels first)
        # Re-ordering will be done by pre-preprocessing or wrapper
        n_input_channels = observation_space.shape[0]
        self.cnn = nn.Sequential(
            # 形を変えずにCNN
            nn.Conv2d(n_input_channels, 16, kernel_size=3, stride=1, padding=1), # 3x6x7 to 16x6x7
            nn.ReLU(),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1), # 16x6x7 to 16x6x7
            nn.ReLU(),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1), # 16x6x7 to 16x6x7
            nn.ReLU(),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1), # 16x6x7 to 16x6x7
            nn.ReLU(),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1), # 16x6x7 to 16x6x7
            nn.ReLU(),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1), # 16x6x7 to 16x6x7
            nn.ReLU(),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1), # 16x6x7 to 16x6x7
            nn.ReLU(),
            # 縦方向に全結合
            nn.Conv2d(16, 16, kernel_size=(6,1), stride=1, padding=0), # 16x6x7 to 16x1x7
            nn.ReLU(),
            nn.Flatten(), # 16x1x7 to 112
        )

        # Compute shape by doing one forward pass
        with torch.no_grad():
            n_flatten = self.cnn(
                torch.as_tensor(observation_space.sample()[None]).float()
            ).shape[1]

        self.linear = nn.Sequential(nn.Linear(n_flatten, features_dim), nn.ReLU())

    def forward(self, observations: torch.Tensor) -> torch.Tensor:
        return self.linear(self.cnn(observations))

## define callback function used in model.learn
class Callback():

    def __init__(self, model_dir="./models"):
        self.num_update = 0
        self.best_mean_reward = -np.inf

        self.model_dir = model_dir

    def __call__(self, _locals, _globals):
        if (self.num_update+1)%1000==0:
            _, y = ts2xy(load_results(self.model_dir), "timesteps")
            if len(y)>0:
                mean_reward = np.mean(y[-1000:])
                update_model = mean_reward > self.best_mean_reward
                if update_model:
                    self.best_mean_reward = mean_reward
                    _locals["self"].save(self.model_dir + "/best_model")

                print("time: {}, num_update: {}, mean: {:.3f}, best_mean: {:.3f}, model_update: {}".format(
                    datetime.datetime.now(pytz.timezone("Asia/Tokyo")),
                    self.num_update,
                    mean_reward,
                    self.best_mean_reward,
                    update_model
                ))

        self.num_update += 1

        return True

## define agent
class RolloutAgent():
    def __init__(self, modelpath):
        self.model =  PPO.load(modelpath)

        self.env = make("connectx", debug=False)

        # アクション数定義
        ACTION_NUM = self.env.configuration["columns"]
        self.action_space = gym.spaces.Discrete(ACTION_NUM)

        # 状態の範囲を定義
        self.WIDTH = self.env.configuration["columns"]
        self.HEIGHT = self.env.configuration["rows"]

        self.obs_shape = [self.HEIGHT, self.WIDTH, 3] # treat as image
        self.observation_space = gym.spaces.Box(low=0, high=255, shape=self.obs_shape, dtype=np.uint8)

    def obs2boardImg(self, obs):

        #state, reward, done, info = obs
        state = obs

        board = state["board"]
        mark = state["mark"]

        tmp = np.array(board).reshape(self.obs_shape[:2])
        boardImg = np.zeros(self.obs_shape, dtype=np.uint8)
        boardImg[:,:,0] = np.array(tmp==mark)*255 # ch.0 : own mark
        boardImg[:,:,1] = np.array(tmp==(3-mark))*255 # ch.1 : opponent mark
        return boardImg

    def __call__(self, obs, config):
        # obs is a state of field as an image

        boardImg = self.obs2boardImg(obs)
        boardImg = boardImg[None] #
        action, _states = self.model.predict(boardImg, deterministic=True)

        return int(action)


import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.5,3.5,300)  # 在0.5到3.5之间均匀地取300个数
y = np.sin(x)
z = np.cos(x)

# fig, ax = plt.subplots(figsize=(14,7))  不设置子图
fig, ax = plt.subplots(1,2,figsize=(14,7))
ax[0].plot(x,y)
ax[1].plot(x,z)

ax[0].set_title('sin(x)', fontsize=18)
ax[1].set_title('cos(x)', fontsize=18)

ax[0].set_xlabel('x', fontsize=18, fontfamily = 'sans-serif', fontstyle='italic')
ax[1].set_xlabel('x', fontsize=18, fontfamily = 'sans-serif', fontstyle='italic')

ax[0].set_ylabel('y', fontsize='x-large',fontstyle='oblique')
ax[1].set_ylabel('y', fontsize='x-large',fontstyle='oblique')

ax[0].set_xlim(0,16)
ax[0].grid(axis='both')

ax[1].xaxis.set_tick_params(rotation=45, labelsize=12)
start, end = ax[1].get_xlim()
ax[1].xaxis.set_ticks(np.arange(start, end, 1))  # np.arange(start, end, step)
ax[1].yaxis.tick_right()

plt.savefig('figure.png', dpi=800)
plt.show()


import ConfigParser
import logging
import logging.config
import time

import sqlite3

class HumidifierDAO():

    CONFIG_SECTION = 'HumidifierDAO_Options'

    def __init__(self, config):
        self.logger = logging.getLogger('HumidifierDao')
        self.db_file = config.get(HumidifierDAO.CONFIG_SECTION, 'db_fullpath')

    def insert_humid_mode(self, mode):
        ''' Insert new humidifier mode into database.
            The time of the insertion will be used as the [created] field.
        '''

        insert = ''' insert into humidifier(created, mode)
                     values(datetime('now', 'localtime'), ?); '''

        try:
            conn = self.get_connection()
            with conn:
                cur = conn.cursor()
                cur.execute(insert, (mode,))
                return cur.lastrowid

        except Exception as e:
            self.logger.error(e)

        finally:
            conn.close()

    def select_range(self, begin, end):
        ''' Return readings from provided time range.
            begin: datetime string of format %Y-%m-%d %H:%M(:%S)
            end  : datetime string of format %Y-%m-%d %H:%M(:%S)
        '''

        sql = """ select created, mode
                  from humidifier
                  where created > ?
                      and created < ?; """

        self.logger.debug(sql.replace('?', '{}').format(begin, end))

        try:
            conn = self.get_connection()
            with conn:
                cur = conn.cursor()
                cur.execute(sql, (begin, end))
                data = cur.fetchall()
                return data

        except Exception as e:
            self.logger.error(e)

        finally:
            conn.close()

    def get_connection(self):
        return sqlite3.connect(self.db_file)


# -*- coding: utf-8 -*-
"""
Created on Wed Aug 30 16:48:28 2017

@author: Vibhuti
"""
#This is streaming API that keeps the connection open to extract upcoming tweets of an event
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import argparse
#create below file with twitter data
import string
import config     #Authorises our app to access Twitter on our behalf, we need to use the OAuth
import json
def get_parser():
    """Get parser for command line arguments."""
    parser = argparse.ArgumentParser(description="Twitter Downloader")
    parser.add_argument("-q",
                        "--query",
                        dest="query",
                        help="Query/Filter",
                        default='-')
    parser.add_argument("-d",
                        "--data-dir",
                        dest="data_dir",
                        help="Output/Data Directory")
    return parser

class MyListener(StreamListener):
    """Custom StreamListener for streaming data."""

#create the file where data will streaming data will be stored in JSON format
    def __init__(self, data_dir, query):
        query_fname = format_filename(query)
        self.outfile = "%s/stream_%s.json" % (data_dir, query_fname)

#Write the data into the JSON file
    def on_data(self, data):
        try:
            with open(self.outfile, 'a') as f:
                f.write(data)
                print(data)
                return True
        except BaseException as e:
            print("Error on_data: %s" % str(e))
            time.sleep(5)
        return True
#Display error in case of file error
    def on_error(self, status):
        print(status)
        return True

def format_filename(fname):
    """Convert file name into a safe string.
    Arguments:
        fname -- the file name to convert
    Return:
        String -- converted file name
    """
    return ''.join(convert_valid(one_char) for one_char in fname)

def convert_valid(one_char):
    """Convert a character into '_' if invalid.
    Arguments:
        one_char -- the char to convert
    Return:
        Character -- converted char
    """
    valid_chars = "-_.%s%s" % (string.ascii_letters, string.digits)
    if one_char in valid_chars:
        return one_char
    else:
        return '_'

@classmethod
def parse(cls, api, raw):
    status = cls.first_parse(api, raw)
    setattr(status, 'json', json.dumps(raw))
    return status

if __name__ == '__main__':
    parser = get_parser()
    args = parser.parse_args()
    auth = OAuthHandler(config.consumer_key, config.consumer_secret)
    auth.set_access_token(config.access_token, config.access_secret)
    api = tweepy.API(auth)
# run from command prommp ||||||python twitter_stream_download.py -q cricket(replace this with any term you want to search) -d data
    twitter_stream = Stream(auth, MyListener(args.data_dir, args.query))
    twitter_stream.filter(track=[args.query])


from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse, reverse_lazy
from django.http import HttpResponseRedirect, Http404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from .models import User
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
from Post.models import Post
from Favorite.models import Favorite

def index(request):
    posts = Post.objects.all()
    context = {
        'posts': posts
    }
    return render(request, 'blog/index.html', context)

class PostListView(ListView):
    model = Post
    template_name = 'blog/index.html'
    context_object_name = 'posts'
    paginate_by = 5

class PostDetailView(DetailView):
    model = Post
    context_object_name = 'post'

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    fields = ['title', 'content', 'image']

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

class PostEditView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['title', 'content', 'image']

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

    def test_func(self):
        return self.request.user == self.get_object().author

class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    success_url = reverse_lazy('index')

    def test_func(self):
        return self.request.user == self.get_object().author

def login(request):
    return render(request, 'blog/login.html')

def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Welcome {username}, your profile  has been created successfully! You can now login.')
            return redirect('login')
    else:
        form = UserRegisterForm()

    context = {
        'form': form
    }
    return render(request, 'blog/register.html', context)

@login_required
def profile(request, *args, **kwargs):
    if request.method == 'POST':
        user_update_form = UserUpdateForm(request.POST, instance=request.user)
        profile_update_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if user_update_form.is_valid() and profile_update_form.is_valid():
            user_update_form.save()
            profile_update_form.save()
            messages.success(request, 'Your profile info has been updated successfully!')
            return redirect(reverse('profile') + f'?page=1&username={ request.user.username }')
    else:
        user = get_object_or_404(User, username=request.GET.get('username'))
        page_num = request.GET.get('page')
        user_update_form = UserUpdateForm(instance=user)
        profile_update_form = ProfileUpdateForm(instance=user.profile)
        fav_posts = user.favorite.posts.all()

    paginate_by = 5
    posts = Post.objects.filter(author=user)
    paginator = Paginator(posts, paginate_by)
    user_posts = None

    try:
        page_obj = paginator.page(int(page_num))
        user_posts = page_obj.object_list

    except:
        raise Http404("Page does not exist.")

    context = {
        'page_user': user,
        'user_update_form': user_update_form,
        'profile_update_form': profile_update_form,
        'user_posts': user_posts,
        'page_obj': page_obj,
        'is_paginated': True,
        'fav_posts': fav_posts,
    }

    return render(request, 'users/profile.html', context)


from pyspark import keyword_only
import pyspark.sql.functions as F
from pyspark.sql import DataFrame
from pyspark.sql.types import StringType, DecimalType
from pyspark.ml import Transformer
from pyspark.ml.param.shared import HasInputCol, HasOutputCol, Param

# noinspection PyUnusedLocal,PyUnresolvedReferences
class ValueMappingTransformer(Transformer, HasInputCol, HasOutputCol):

    """
    Maps the key values on inputCol to values in the mapDict
    """

    @keyword_only
    def __init__(self,
                 inputCol: str = None,
                 outputCol: str = None,
                 mapDict: dict = None):
        # call Transformer classes constructor since were extending it.
        super(Transformer, self).__init__()

        # set Parameter objects minimum mapping dictionary
        self.mapDict = Param(self,
                             "mapDict",
                             "A key string to value string dict, key is the value to be replaced with the value.")
        self._setDefault(mapDict={None: None})

        # set the input keyword arguments
        kwargs = self._input_kwargs
        self.setParams(**kwargs)

        # initialize any other objects
        # self.my_obj = my_utility_objects()

    @keyword_only
    def setParams(self,
                  inputCol: str = None,
                  outputCol: str = None,
                  mapDict: dict = None
                  ) -> None:
        kwargs = self._input_kwargs
        return self._set(**kwargs)

    def _trans_func(self, in_map_value: str) -> str:
        """
        Replacing a string value with another string value.
        The input value is looked up as a key on the dictMap and replaced
        by the corresponding dict value.

        Args:
            in_map_value: A string to be replaced with a corresponding value string on mapDict

        Returns:
            The mapped target value to be placed in the output column

        """

        # Get the specified mapping dictionary
        map_dict = self.getMapDict()

        # Select mapping value from dict and assign to out_col
        out_map_value = map_dict[in_map_value]

        return out_map_value

    def _transform(self, df: DataFrame) -> DataFrame:
        # Get the names of the input and output columns to use
        out_col = self.getOutputCol()
        in_col = self.getInputCol()

        # create the transformation function UDF
        trans_func_udf = F.udf(self._trans_func, StringType())

        # apply the UDF to the in_col column in the dataframe and add a
        # new out_col column with the transformed results
        df2 = df.withColumn(out_col, trans_func_udf(df[in_col]))

        return df2

    def setMapDict(self, value):
        self._paramMap[self.mapDict] = value
        return self

    def getMapDict(self) -> int:
        return self.getOrDefault(self.mapDict)

# noinspection PyUnusedLocal,PyUnresolvedReferences
class TitleFoldTransformer(Transformer, HasInputCol, HasOutputCol):
    """
    Converts capitalized strings to start letter capital/title-caps.
    This string folding won't be performed for strings of defined length,
    contain numbers or are title-caps already.
    """

    @keyword_only
    def __init__(self,
                 inputCol: str = None,
                 outputCol: str = None,
                 capLength: int = None,
                 strSep: str = None):
        # call Transformer classes constructor since were extending it.
        super(Transformer, self).__init__()

        # set Parameter objects minimum capital letters and string separator
        self.capLength = Param(self,
                               "capLength",
                               "Number of characters in a String that should remain all-cap.")
        self._setDefault(capLength=0)
        self.strSep = Param(self,
                            "strSep",
                            "Single character String by which to separate multiple consecutive strings in inputCol.")
        self._setDefault(strSep="")

        # set the input keyword arguments
        kwargs = self._input_kwargs
        self.setParams(**kwargs)

        # initialize any other objects
        # self.my_obj = my_utility_objects()

    @keyword_only
    def setParams(self,
                  inputCol: str = None,
                  outputCol: str = None,
                  capLength: int = None,
                  strSep: str = None
                  ) -> None:

        kwargs = self._input_kwargs
        return self._set(**kwargs)

    @staticmethod
    def _contains_number(in_str: str) -> str:
        """
        Checks a single string of any length for any digit.

        Args:
            in_str: A string

        Returns:
            True if a digit is found. False otherwise

        """
        return_bool = False
        return_bool = any(char.isdigit() for char in in_str)

        return return_bool

    def _trans_func(self, in_value: str) -> str:
        """
        Folds all words in an input string into title-caps, where words are
        separated by strSep cannot contain numbers and
        only words longer than capLength are changed.

        Args:
            in_value: A string with one or more words separated by a strSep

        Returns:
            A string with all words in title-cap

        """

        # Get the specified string length to keep capitalized and separator
        cap_length = self.getCapLength()
        str_sep = self.getStrSep()

        if in_value is not None:
            in_value_list = in_value.split(str_sep)
            out_value = ""

            # TODO: Needs good testing; var is only assigned if if-statements completed as expected!!!
            for item in in_value_list:
                done = False
                if in_value_list[-1] == item:
                    # Only convert strings long enough, without numbers and not starting with single capital letter
                    if len(item) <= cap_length or self._contains_number(item) or item.istitle():
                        out_value = out_value + item
                        done = True
                    if (len(item) > cap_length or not self._contains_number(item) or item.istitle()) and not done:
                        out_value = out_value + item.title()
                if in_value_list[-1] != item:
                    # Only convert strings long enough, without numbers and not starting with single capital letter
                    if len(item) <= cap_length or self._contains_number(item) or item.istitle():
                        out_value = out_value + item + str_sep
                        done = True
                    if (len(item) > cap_length or self._contains_number(item) or item.istitle()) and not done:
                        out_value = out_value + item.title() + str_sep
        if in_value is None:
            out_value = None

        return out_value

    def _transform(self, df: DataFrame) -> DataFrame:
        # Get the names of the input and output columns to use
        out_col = self.getOutputCol()
        in_col = self.getInputCol()

        # create the transformation function UDF
        trans_func_udf = F.udf(self._trans_func, StringType())

        # apply the UDF to the in_col column in the dataframe and add a
        # new out_col column with the transformed results
        df2 = df.withColumn(out_col, trans_func_udf(df[in_col]))

        return df2

    def setCapLength(self, value):
        self._paramMap[self.capLength] = value
        return self

    def getCapLength(self) -> int:
        return self.getOrDefault(self.capLength)

    def setStrSep(self, value):
        self._paramMap[self.strSep] = value
        return self

    def getStrSep(self) -> int:
        return self.getOrDefault(self.strSep)

# noinspection PyUnusedLocal,PyUnresolvedReferences
class StringDecimalTransformer(Transformer, HasInputCol, HasOutputCol):
    """
    Converts string numbers into decimals and removing white space,
    commas and thousands indicators as specified in the process
    """

    @keyword_only
    def __init__(self,
                 inputCol: str = None,
                 outputCol: str = None,
                 removeTokens: list = None,
                 decSplitStr: str = None,
                 precision: int = None,
                 scale: int = None):
        # call Transformer classes constructor since were extending it.
        super(Transformer, self).__init__()

        # set Parameter objects
        self.removeTokens = Param(self,
                                  "removeTokens",
                                  "A list of string tokens to remove, if one use [char], e.g., thousands indicators.")
        self._setDefault(removeTokens=[None])
        self.decSplitStr = Param(self,
                                 "decSplitStr",
                                 "A single character string indicating the decimal character, e.g. '.' in '0.1'.")
        self._setDefault(decSplitStr="")
        self.precision = Param(self,
                               "precision",
                               "Int for max digits before decimal, e.g, '2' in '0.1'. Defaults of DecimalType()")
        self._setDefault(precision=None)
        self.scale = Param(self,
                           "scale",
                           "Int number of precision to after decimals, e.g. '1' in '0.1'. Defaults of DecimalType()")
        self._setDefault(scale=None)

        # set the input keyword arguments
        kwargs = self._input_kwargs
        self.setParams(**kwargs)

        # initialize any other objects
        # self.my_obj = my_utility_objects()

    @keyword_only
    def setParams(self,
                  inputCol: str = None,
                  outputCol: str = None,
                  removeTokens: list = None,
                  decSplitStr: str = None,
                  precision: int = None,
                  scale: int = None
                  ) -> None:
        kwargs = self._input_kwargs
        return self._set(**kwargs)

    def _num_formater(self, in_value: str) -> str:
        """
        Checks if the in_value only contains permissible characters such as digits, tokens or separator,
        then removes these items.
        Additionally ensures that there is only a single separator.

        Args:
            in_value: A string to be checked

        Returns:
            A string of digits with tokens removed and a single separator value. None otherwise.

        """
        remove_tokens = self.getRemoveTokens()
        dec_split_str = self.getDecSplitStr()
        digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
        allowed = remove_tokens + [dec_split_str] + digits
        return_val = ""
        split_ct = 0

        for item in in_value:
            if item == dec_split_str:
                split_ct = split_ct + 1
                if split_ct > 1:
                    return_val = None
                    break
            if item not in allowed:
                return_val = None
                break
            if item in remove_tokens:
                in_value = in_value.replace(item, "")
            if item == in_value[-1]:
                return_val = in_value

        # Number formats work poor with anything else than "." therefore replacing dec_split_str
        if dec_split_str != "." and return_val is not None:
            return_val = return_val.replace(dec_split_str, ".")

        return return_val

    def _trans_func(self, in_value: str) -> str:

        clean_in_value = self._num_formater(str(in_value))

        return clean_in_value

    def _transform(self, df: DataFrame) -> DataFrame:
        # Get the names of the input and output columns to use
        # TODO: Make this work for a number of column names
        out_col = self.getOutputCol()
        in_col = self.getInputCol()

        # create the transformation function UDF
        trans_func_udf = F.udf(self._trans_func, StringType())

        # apply the UDF to the in_col column in the dataframe and add a
        # new out_col column with the transformed results
        df = df.withColumn(out_col, trans_func_udf(df[in_col]))

        # Compute precision/scale for adequate decimal value conversion unless defined by user
        # Note: This expects that the decSplitStr has been replaced by _num_formater with a "."!!!
        # TODO: Consider to simplify the query
        if self.getScale() is None or self.getPrecision() is None:
            pre_scl_df = df.select(df[out_col]) \
                .withColumn("pre_dec",
                            F.when(F.instr(out_col, ".") != 0,
                                   F.instr(out_col, ".") - 1) \
                            .otherwise(F.length(df[out_col]))) \
                .withColumn("post_dec",
                            F.when(
                                    F.when(F.instr(out_col, ".") != 0,
                                        F.length(df[out_col]) - 1) \
                                    .otherwise(F.length(df[out_col])) \
                                   - F.when(F.instr(out_col, ".") - 1 > 0,
                                            F.instr(out_col, ".") - 1) \
                                   .otherwise(0) == \
                                    F.when(F.instr(out_col, ".") != 0,
                                        F.length(df[out_col]) - 1) \
                                    .otherwise(F.length(df[out_col])), 0) \
                            .otherwise(
                                    F.when(F.instr(out_col, ".") != 0,
                                        F.length(df[out_col]) - 1) \
                                    .otherwise(F.length(df[out_col])) \
                                       - F.when(F.instr(out_col, ".") - 1 > 0,
                                                F.instr(out_col, ".") - 1) \
                                       .otherwise(0)))

            scale = pre_scl_df.agg(F.max("post_dec")).collect()[0][0]
            precision = pre_scl_df.agg(F.max("pre_dec")).collect()[0][0] + scale

            # TODO: Make this additive so that precision = scale + max characters because now too much of precision
            #       gets used by scale!
            self.setPrecision(precision)
            self.setScale(scale)

        df = df.withColumn(out_col, F.col(out_col).cast(DecimalType(self.getPrecision(),
                                                                    self.getScale())))

        return df

    def setRemoveTokens(self, value):
        self._paramMap[self.removeTokens] = value
        return self

    def getRemoveTokens(self) -> int:
        return self.getOrDefault(self.removeTokens)

    def setDecSplitStr(self, value):
        self._paramMap[self.decSplitStr] = value
        return self

    def getDecSplitStr(self) -> int:
        return self.getOrDefault(self.decSplitStr)

    def setPrecision(self, value):
        self._paramMap[self.precision] = value
        return self

    def getPrecision(self) -> int:
        return self.getOrDefault(self.precision)

    def setScale(self, value):
        self._paramMap[self.scale] = value
        return self

    def getScale(self) -> int:
        return self.getOrDefault(self.scale)