#!/usr/bin/env python3
#-*- coding: utf-8 -*-

"""
Created on Wed Dec 25 12:47:44 2019
@author: Amal Htait
"""

from elasticsearch import Elasticsearch, helpers
import os, uuid, json
import common as c
import sentiment as s

dirpath = os.getcwd()

# Generator to push bulk data from a JSON file into an Elasticsearch index / that function is changed according to files content
def bulkJsonData(json_file, _index, whatStuff):
    json_list = c.getDataFromFile(json_file)
    for doc in json_list:

        json_doc = json.loads(doc)

        sentiment=[0,0,0]

        # clean the text in comments and title from special character and emojies after json conversion
        if "data" in json_doc:
            my_text_location = json_doc["data"][0]["comment"]
            my_text = my_text_location["comment"]

            #get sentiment
            sentiment = s.getSentiment(my_text)

            clean_my_text = c.cleanText(my_text)
            my_text_location.update([ ("comment", clean_my_text) ])
            json_doc.update([ ("all_text", clean_my_text) ])

            if "group" in my_text_location:
                my_group = my_text_location["group"]
                clean_my_group = c.cleanText(my_group)
                my_text_location.update([ ("group", clean_my_group) ])

        my_title = json_doc["title"]
        clean_my_title = c.cleanText(my_title)
        json_doc.update([ ("title", clean_my_title) ])

        # add sentiment
        json_doc.update([ ("mySentiment", sentiment[0]) ])
        json_doc.update([ ("sentPositive", sentiment[1]) ])
        json_doc.update([ ("sentNegative", sentiment[2]) ])

        # add load_type, used later for filter
        json_doc.update([ ("load_type", whatStuff) ])
        json_doc.update([ ("source_type", "facebook") ])
        new_doc = str(json_doc).replace("'", '"')

        # use a 'yield' generator so that the data isn't loaded into memory
        if '{"index"' not in new_doc:
            yield {
                "_index": _index,
                "_id": uuid.uuid4(),
                 "_source": new_doc
            }

def fct():
    # create a new instance of the Elasticsearch client class
    #elastic = Elasticsearch("localhost")
    elastic = Elasticsearch(hosts=[{'host':'localhost','port':9200}])

    # the Schema, used to force specific types and to add alias/ it is changed according to files content
    schema = {

       "settings": {
          "analysis": {
            "analyzer": {
              "my_english_analyzer": {"type": "standard","stopwords": "_english_"}
            }
          }
        },

      "mappings":{
         "properties":{

              "timestamp":   { "type":"date", "format":"EEE MMM dd HH:mm:ss ZZ yyyy||date_optional_time||epoch_second"},
              "created_at": { "type": "alias", "path": "timestamp" },
              "all_text": { "type": "text", "analyzer": "my_english_analyzer", "fields": {"keyword": { "type": "keyword"}}, "fielddata": True},
              "mySentiment":   { "type":"float"},
              "sentPositive":   { "type":"float"},
              "sentNegative":   { "type":"float"},

              "data":  {
                  "properties": {
                      "comment":{
                          "type": "nested",
                          "properties": {
                              "comment":{ "type": "text", "analyzer": "my_english_analyzer", "fields": {"keyword": { "type": "keyword"}}, "fielddata": True}
                              }
                          }
                      }
                  }

            }
          }

    }

    # Create index with a schema
    c.createIndex('dfp_text_fb_comment', schema, elastic)

    inputFolder = dirpath+"/script/dataSource/json-facebook_data/comments"
    for loadType in ["comments"]:
        whatFile = os.path.join(inputFolder, loadType+'.json')
        #try:
        response = helpers.bulk(elastic, bulkJsonData(whatFile, "dfp_text_fb_comment",loadType))
        print ("Insert Facebook Comments")
        #except:
        #    print ("Error in "+ whatFile)
        #    pass


import os

import torch
import torchvision
from torch import nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from cnn import CNN

# 批训练加速模型的训练速度
EPOCH = 1  # 定义批次训练的batch数
BATCH_SIZE = 50  # 定义批次训练的batch大小

LR = 0.001  # 学习率，学习率越高模型训练速度越快，但对应得会损失精度

cnn = CNN()
print(cnn)
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)
loss_func = nn.CrossEntropyLoss()  # 项目内容本质上是分类问题，针对多分类问题一般用交叉熵，因为它计算的是一个概率

test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)  # 数据还是0-255之间
test_x = torch.unsqueeze(test_data.data, dim=1).type(torch.FloatTensor)[:2000] / 255.  # 该操作把255数据压缩到0-1之间
test_y = test_data.targets[:2000]
x = torch.unsqueeze(test_data.data, dim=1).type(torch.FloatTensor)[
    :2000] / 255.  # unsqueeze 是加上了batchsize的维度，/255是压缩到0-1
y = test_data.targets[:2000]  # 为了节约时间只取前2000个

DOWNLOAD_MNIST = False
if not (os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
    # not mnist dir or mnist is empyt dir
    DOWNLOAD_MNIST = True

train_data = torchvision.datasets.MNIST(
    root='./mnist/',
    train=True,  # 是true下载训练集，如果是false 则下载测试集
    transform=torchvision.transforms.ToTensor(),  # Converts a PIL.Image or numpy.ndarray to
    # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
    # 载下来的数据转化为tensor格式（0-1），原始数据是pxl（0-255）
    download=DOWNLOAD_MNIST,
)
print(train_data)
train_loader = DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)

for epoch in range(EPOCH):
    for step, (x, y) in enumerate(train_loader):
        b_x = Variable(x)  # 需要把X，Y使用Variable转化为pytorch能处理的张量形式
        b_y = Variable(y)

        output = cnn(b_x)
        loss = loss_func(output, b_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if step % 50 == 0:
            test_output = cnn(test_x)
            pred_y = torch.max(test_output, 1)[1].data.squeeze()
            accuracy = sum(pred_y == test_y) / test_y.size(0)
            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)

test_output = cnn(test_x[:10])
pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()
print(pred_y, 'prediction number')
print(test_y[:10].squeeze(), 'real number')


from sklearn.linear_model import LinearRegression, ElasticNet
from sklearn.metrics import mean_squared_error
from dataset import Dataset

def main():
    print("Hello World!")
    path = "../dataset/housing.data"
    dataset = Dataset(path)
    lm = LinearRegression()

    lm.fit(dataset.X, dataset.Y)

    y_hats = lm.predict(dataset.X)

    print(y_hats[0], dataset.Y[0])
    mse = mean_squared_error(dataset.Y, y_hats)
    print("Linear Regression - MSE: %f" % mse)

    enm = ElasticNet()
    enm.fit(dataset.X, dataset.Y)

    y_hats = enm.predict(dataset.X)

    print(y_hats[0], dataset.Y[0])
    mse = mean_squared_error(dataset.Y, y_hats)
    print("ElasticNet - MSE: %f" % mse)

if __name__ == "__main__":
    main()


from hwlib.model import ModelDB
import hwlib.model as model
import hwlib.props as proplib
import ops.op as oplib
import ops.interval as intervallib
import util.util as util

def get_params(db,conc_circ, \
               block_name,loc,port, \
               mode, \
               handle=None):
  block = conc_circ.board.block(block_name)
  config = conc_circ.config(block_name,loc)
  mode = util.DeltaModel(mode)
  if not block.has_handle(config.comp_mode,port,handle):
    raise Exception("no handle %s in %s[%s].%s %s" % \
                    (handle,block_name,oc,port,config.comp_mode))
  coeff = block.coeff(config.comp_mode, \
                      config.scale_mode, \
                      port, handle)
  oprange = None
  sample_rate = None
  quantize = None
  if block.has_prop(config.comp_mode, \
                    config.scale_mode, \
                    port,handle):
    prop = block.props(config.comp_mode, \
                      config.scale_mode, \
                      port,handle)
    oprange = prop.interval()
    if isinstance(prop,proplib.DigitalProperties):
      quantize = prop.values()
      sample_rate = prop.sample_rate

  gain = model.get_gain(db,
                        circ=conc_circ, \
                        block_name=block_name, \
                        loc=loc,
                        port=port, \
                        handle=handle, \
                        mode=mode)
  opsc_l,opsc_u = model.get_oprange_scale(db, \
                                          circ=conc_circ, \
                                          block_name=block_name, \
                                          loc=loc,
                                          port=port, \
                                          handle=handle, \
                                          mode=mode)
  unc = model.get_variance(db, \
                           circ=conc_circ, \
                           block_name=block_name, \
                           loc=loc,
                           port=port, \
                           handle=handle,
                           mode=mode)

  if not oprange is None:
    lb = opsc_l*oprange.lower
    ub = opsc_u*oprange.upper
    ival = intervallib.Interval(lb,ub)
  else:
    ival = None
  return {
    'interval': ival,
    'gain':coeff*gain,
    'unc':unc
  }

def build_output(db,conc_circ,block_name,loc,port,expr,
                 mode):
  def phys_expr(expr,handle):
    pars = get_params(db,conc_circ,block_name,loc,port,\
                      handle=handle,
                      mode=mode)
    e = oplib.Mult(oplib.Const(pars['gain']),expr)
    if not pars['interval'] is None:
      e = oplib.Clamp(e,pars['interval'])
    #e = oplib.Add(e,
    #              oplib.RandomVar(pars['unc']))
    return e

  if block_name == "integrator":
    assert(isinstance(expr,oplib.Integ))
    init_cond = phys_expr(expr.init_cond, \
                          expr.ic_handle)
    deriv = phys_expr(expr.deriv, \
                      expr.deriv_handle)
    return init_cond,deriv

  else:
    return phys_expr(expr, None)

def build_stub(db,conc_circ,block_name,loc,port,expr,
                 mode):
  config = conc_circ.config(block_name,loc)
  pars = get_params(db,conc_circ,block_name,loc,port,\
                    handle=':z',
                    mode=mode)
  pars2 = get_params(db,conc_circ,block_name,loc,port,\
                    handle=None,
                    mode=mode)

  e = oplib.Mult(oplib.Const(pars['gain']*pars2['gain']), expr)
  return e

def build_input(db,conc_circ,block_name,loc,port,expr,
                 mode):
  pars = get_params(db,conc_circ,block_name,loc,port,\
                    handle=None,
                    mode=mode)
  e = oplib.Clamp(expr,pars['interval'])
  return e

def build_expr(db,conc_circ, \
               block_name,loc,port, \
               mode, \
               stvars=[],
               depth=0):
  block = conc_circ.board.block(block_name)
  config = conc_circ.config(block_name,loc)

  if (block_name,loc,port) in stvars and \
     depth > 0:
    lbl = config.label(port)
    print("stub %s[%s].%s -> %s" % (block_name,loc,port,lbl))
    expr = oplib.Var(lbl)
    stub = build_stub(db,conc_circ,
                      block_name,loc,port, \
                      expr, \
                      mode=mode)
    return stub

  if port in block.outputs:
    expr = block.get_dynamics(config.comp_mode, port)
    in_exprs = {}
    for v in expr.vars():
      in_e = build_expr(db,conc_circ, \
                        block_name,loc,v, \
                        mode=mode,
                        stvars=stvars,
                        depth=depth+1)
      in_exprs[v] = in_e

    out_expr = expr.substitute(in_exprs)
    print("out %s[%s].%s -> %s" % \
      (block_name,loc,port,out_expr))
    return build_output(db,conc_circ, \
                        block_name,loc,port, \
                        out_expr, \
                        mode=mode)

  elif port in block.inputs:
    inputs = []
    if config.has_dac(port):
      dac_val = config.dac(port)
      scf = config.scf(port)
      inputs.append(oplib.Const(dac_val*scf))

    for sblk,sloc,sport in \
        conc_circ.get_conns_by_dest(block_name,loc,port):
      src_expr= build_expr(db,conc_circ, \
                           sblk,sloc,sport,
                           mode=mode,
                           stvars=stvars,
                           depth=depth+1)
      inputs.append(src_expr)

    in_expr = oplib.mkadd(inputs)
    print("in %s[%s].%s val=%s" % \
      (block_name,loc,port,in_expr))
    return build_input(db,conc_circ, \
                       block_name,loc,port, \
                       in_expr, \
                       mode=mode)

def build_diffeqs(conc_circ,mode):
  db = ModelDB()
  stvars = []
  for loc,_ in  \
      conc_circ.instances_of_block("integrator"):
    stvars.append(('integrator',loc,'out'))

  initials = {}
  var_initials = {}
  derivs = {}
  var_derivs = {}
  for block,loc,port in stvars:
    init_cond,deriv = build_expr(db,conc_circ, \
                                 block,loc,port, \
                                 mode=mode, \
                                 stvars=stvars)
    config = conc_circ.config(block,loc)
    label = config.label(port)
    assert(not label in initials)
    var_initials[label],initials[label]  \
      = oplib.to_python(init_cond)
    var_derivs[label],derivs[label] \
      = oplib.to_python(deriv)

  return initials,derivs

def build_simulation(board,conc_circ,mode):
  mode = util.DeltaModel(mode)
  init_conds,derivs = build_diffeqs(conc_circ,mode)
  return init_conds,derivs


from django.urls import path
from .views import *

urlpatterns = [
    path('', homePage, name="Home_Page"),
    path('listStatic/', listEventsStatic, name="events_list_static"),
    path('list/', listEvents, name="events_list"),
    path('add/', addEvent, name="events_add"),
    path('details/<int:id>', detailsEvent, name="events_details"),
    path('participate/<int:id>', participateEvent, name="events_participate"),
    path('listEvents/', EventsList.as_view(), name="events_listC"),
    path('eventsDetails/<int:pk>', EventsDetails.as_view(), name="events_DetailsC"),
    path('eventUpdate/<int:pk>', EventUpdateView.as_view(), name="events_UpdateC"),
    path('eventDelete/<int:pk>', EventDelete.as_view(), name="events_DeleteC"),
]


from pathlib import Path

import sys

ROOT_PATH = Path(__file__).resolve().parent.parent

sys.path.append(str(ROOT_PATH))

from libru.libru import parse  # noqa

menu = [
    [
        "carne bovina á portuguesa",
        "abóbora cozida (almoço)",
        "purê de batatas (jantar)",
        "repolho",
        "beterraba",
        "molho de ervas",
        "laranja",
    ],
    [
        "filé mignon suíno ao molho agridoce",
        "farofa simples",
        "alface",
        "cenoura",
        "molho de mostarda",
        "maçã",
    ],
    ["iscas de frango", "vagem cozida", "chicória", "rabanete", "vinagrete", "banana"],
    [
        "quibe",
        "couve-flor refogada",
        "acelga",
        "salada de soja em grãos",
        "molho de mostarda",
        "laranja",
    ],
    [
        "risoto de frango",
        "batata palha",
        "agrião",
        "pepino",
        "molho de ervas",
        "banana",
    ],
    [],
    [],
]

def test_ru():
    with open(ROOT_PATH / "tests/ru.html") as mock_html:
        html = mock_html.read()

    for weekday in range(7):
        assert parse(html, weekday) == menu[weekday]


import json
import time
from StubHub_API import StubHub_API, StubHub_API_Request_Error, GetListingsError
import datetime

JSON_FORMAT = {'sort_keys': True, 'indent': 4, 'separators': (',', ': ')}
DATETIME_FORMAT = "%Y-%m-%d_%H-%M-%S"

def stubhub_list_scrape(credfile='credentials_prod.json', eventfile='events.json', settingsfile='settings.json', warnfile='warnings.log', handle_failures=True):
    """
    Continuously scrapes listings from a set of StubHub events and stores them to files.

    :param eventfile:
    :param settingsfile:
    :param warnfile: File to store some warnings (a hacky way to highlight these warnings separate from other messages)
    :param handle_failures: If True, handle StubHub_API_Request_Errors and continue logging for other events
    :return:
    """
    # Instance the StubHub_API and get credentials
    stubhub = StubHub_API()
    stubhub.set_scope(scope='PRODUCTION')
    stubhub.load_credentials(file=credfile)

    while True:
        print("Loading settings and next EventID")
        settings = load_settings(settingsfile)
        event = get_next_event(eventfile=eventfile, update_eventfile=True, remove_next=True, add_next_to_end=True)
        event_loc = event.get('save_location', './')
        eventid = event['eventid']
        file_format = event.get('file_format', 'txt')

        now = datetime.datetime.today().strftime(DATETIME_FORMAT)
        list_file = event_loc + '/' + str(eventid) + "_" + now + ".json"
        print("Saving listings for event {0} in file {1}".format(eventid, list_file))
        try:
            stubhub.store_event_inventory(filename=list_file, eventid=eventid, file_format=file_format, warnfile=warnfile)
        except (StubHub_API_Request_Error, GetListingsError) as e:
            if handle_failures:
                warn("{1}: Warning: Failed to store event inventory for event {0} - event skipped".format(eventid, now), filename=warnfile)
            else:
                raise e
        if settings['stop']:
            print("Stop requested in settings file - stopping scrape")
            break
        else:
            print("Sleeping {0} seconds before next action".format(settings['wait_between_listings']))
            time.sleep(settings['wait_between_listings'])

def load_settings(settingsfile=None):
    """
    Loads stubhub_list_scrape settings, applying defaults to any that are missing, and returns as a dict.

    :param settingsfile:
    :return: dict of settings
    """
    # Load settings
    with open(settingsfile, 'r') as f:
        settings = json.load(f)

    # Apply defaults
    settings['stop'] = settings.get('stop', False)
    settings['wait_between_listings'] = settings.get('wait_between_listings', 60)

    return settings

def get_next_event(eventfile=None, update_eventfile=True, remove_next=True, add_next_to_end=True):
    """
    Return the next event in an eventfile and then overwrite the eventfile with an updated set of events.

    :param eventfile:
    :param remove_next: If True, remove the first event in the list before saving the eventfile
    :param add_next_to_end: If True, add_seat the first event in the list to the end of the list before saving the eventfile
    :return: The eventid of the next event in the list
    """

    with open(eventfile, 'r') as f:
        events = json.load(f)['events']
    next_event = events[0]

    if update_eventfile:
        if remove_next:
            events.pop(0)

        if add_next_to_end:
            events.append(next_event)

        with open(eventfile, 'w') as f:
            json.dump({'events': events}, f, **JSON_FORMAT)

    return next_event

def warn(message, filename=None):
    """
    Print a warning to the screen and log in a file
    :param filename: Filename to append warning (or none))
    :return: None
    """
    print(message)
    if filename:
        with open(filename, 'a') as f:
            f.write(message + "\n")

if __name__ == "__main__":
    stubhub_list_scrape(eventfile='events_Panthers_Hornets.json')


import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
'''
一种简单场景是在GPA得分和SAT得分数据的基础上，预测一个学生是否有可能被大学生本科培养计划接受
'''

mpl.rcParams['axes.facecolor'] = '#f8f8f8'
mpl.rcParams['grid.color'] = '#303030'
# mpl.rcParams['grid.color'] = '#303030'
mpl.rcParams['grid.linestyle'] = '--'
# SAT Score
x = [2400, 2350, 2400, 2290, 2100, 2380, 2300, 2280, 2210, 2390]

# High school GPA
y = [4.4, 4.5, 4.2, 4.3, 4.0, 4.1, 3.9, 4.0, 4.3, 4.5]

a = '#6D0000'
r = '#00006f'
# Acceptance or rejections score
z = [a, a, a, r, r, a, r, r, a, a]

plt.figure(figsize=(8, 6))
plt.scatter(x, y, c=z, s=600)

# To see where the separation lies
for i in range(1, 5):
    x_plot = np.linspace(2490-i*2, 2150+i*2, 20)
    y_plot = np.linspace(3.3+i*0.2, 5-0.2*i, 20)
    plt.plot(x_plot, y_plot, c='gray')

plt.grid(True)

plt.xlabel('SAT Score', fontsize=18)
plt.xlabel('GPA', fontsize=18)
plt.title('Acceptance in College', fontsize=20)
plt.legend()

plt.show()


import json

ask_input = False
run = True

command = ""
amount_loot_now = ""
amount_roll_now = ""
amount_fish_now = ""

with open("values.json") as values_file:
    raw_data_values = values_file.read()
values_data = json.loads(raw_data_values)

amount_all_now = 0


# -*- coding: utf-8 -*-
from strupy import units as u
from strupy.steel.SectionBase import SectionBase
from strupy.steel.MaterialSteel import MaterialSteel
from strupy.x_graphic.SvgCreator2D import SvgCreator2D
import svgwrite

base = SectionBase()
steel = MaterialSteel()

#! ##Baza profili EU/US/UK

#!--------------------------------------------------------------------------------

basename = 'EU'

basenam_list = base.get_available_databasenames()
basename = basenam_list[0] #<<<< Baza profili -

base.set_databasename(basename)

full_basename = base.get_database_name()
#! Baza val_basename / val_full_basename

types = base.get_database_sectiontypes()
family = types[0] #<<<< Rodzina profilu -
names = base.get_database_sectionlistwithtype(family)
name = names[2] #<<<< Profil -

active = False #<<<  Parametry profilu
if active:
	prep = base.get_sectionparameters(name)
	prepdescription = base.get_parameters_description()
	selectedtype_description = base.get_database_sectiontypesdescription()[prep['figure']]
	#----------
	text=selectedtype_description + '\n'
	text+=  'Properites of  '+ prep['sectionname'] + '  section: \n'
	preptodisplay=('Ax', 'Ay', 'Az', 'Iomega', 'Ix', 'Iy', 'Iz', 'Wply', 'Wplz', 'Wtors',
	'b', 'ea', 'es', 'gamma', 'h', 'mass', 'surf', 'Wy', 'Wz', 'vpy', 'vpz', 'vy', 'vz')
	for i in prep :
		if i in preptodisplay:
			text = i + '='+ str(prep[i])+' - '+ prepdescription[i]
			#! val_text

active = False #<<<  Ksztalt profilu
if active:
	svg_scene = SvgCreator2D()
	svg_scene.scene = svgwrite.Drawing(size = ('300', '300'))
	scale_list = [10*u.mm, 25*u.mm, 50*u.mm, 75*u.mm, 100*u.mm]
	scale = scale_list[1] #<<<< >Skala -
	svg_scene.set_unit(scale / 20)
	svg_scene.setvievbox(center=[0.0*u.mm, 0.0*u.mm], width = 300, high=300, boarder = True)
	base.draw_sectiongeometry(svg_scene, name, 1)
	grid = 20.0 * svg_scene.unit
	svg_scene.addLine([5*grid, -3*grid], [6*grid, -3*grid])
	svg_scene.addLine([5*grid, -2.8*grid], [5*grid, -3.2*grid])
	svg_scene.addLine([6*grid, -2.8*grid], [6*grid, -3.2*grid])
	svg_scene.addText(str(1*grid), [4.4*grid, -3*grid])
	svg_document = svg_scene.scene  #%svg

'''
SeeID : 93429003
SeeField : XbeamStructure
SeeCategory : Steel
SeeName : Baza profili
SeeDescription : Baza profili stalowych EU, UK, US.
'''


#!/usr/bin/env python -W ignore::DeprecationWarning

from texture_features_RC import *
import numpy as np
import skimage.feature as ft
import pandas as pd
import statistics
import time
from operator import itemgetter
from datetime import datetime as dt
from networkx import algorithms
import os

#classificacao
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import cross_val_predict, KFold,GroupKFold
from sklearn.svm import SVC

from _thread import start_new_thread, allocate_lock
import threading

logs_metricas = []

def write_csv(data,file):
		df = pd.DataFrame(data)
		df.to_csv('../logs/'+file+'.csv', mode='a', sep=';',index=False, header=False)

def read_csv(file):

		df1 = pd.DataFrame.from_csv('../dataset/%s'%(file),sep=';',index_col=0,encoding ='ISO-8859-1')

		df1 = df1.reset_index()

		return df1

def write_txt(data):

    with open('features.log', 'w') as out:
       np.savetxt(out, data, '%4.6f')

def load_patologias(path,rc):

    file_list = []

    dir_list = []

    df = pd.DataFrame(columns=['files','class'])

    for root, pth, filenames in os.walk(path):
        file_list.append(filenames)

    df_files = []
    df_class = []

    cnt = 0
    c = 0
    for files in file_list:
        if cnt > 0:
            for n in files:
                if (n.find('.tif')) == -1:
                    pass

                else:
                    #print(n)
                    clc = n[0]
                    df_files.append(path+'/'+clc+'/'+n)
                    df_class.append(c)

            c += 1

        else:
            cnt += 1

    df['files'] = df_files
    df['class'] = df_class

    return df

def clf_randomForest(X,target):

    clf_rf = RandomForestClassifier(n_estimators=1000,criterion='gini',max_features='log2')
    kf = KFold(10, shuffle=True, random_state=1)

    ac_v = []
    cm_v = []
    p_v = []
    r_v = []
    f1_v = []
    e_v = []
    predicts = []

    for train_index,teste_index in kf.split(X,target):

        X_train, X_test = list(itemgetter(*train_index)(X)), list(itemgetter(*teste_index)(X))
        y_train, y_test = list(itemgetter(*train_index)(target)), list(itemgetter(*teste_index)(target))
        clf_rf.fit(X_train,y_train)
        pred = clf_rf.predict(X_test)
        ac = accuracy_score(y_test, pred)
        p = precision_score(y_test, pred,average='weighted')
        r = recall_score(y_test, pred,average='weighted')
        f1 = (2*p*r)/(p+r)
        e = mean_squared_error(y_test, pred)
        cm = confusion_matrix(y_test,pred)
        cm_v.append(cm)
        ac_v.append(ac)
        p_v.append(p)
        r_v.append(r)
        f1_v.append(f1)
        e_v.append(e)

    ac = statistics.median(ac_v)
    p = statistics.median(p_v)
    f1 = statistics.median(f1_v)
    r = statistics.median(r_v)
    e = statistics.median(e_v)

    return (ac,p,f1,r,e)

def convert_df(df):

    new_def = []

    for t in df:

        if t  == True:
            new_def.append(1)

        elif t == False:
            new_def.append(0)

    return new_def

def crop_slices(imgray):

    array = []

    row = np.size(imgray,0)
    col = np.size(imgray,1)

    s = int(row/4)
    s1 = int(col/4)

    array.append(imgray)
    array.append(imgray[1:s,1:s1])
    array.append(imgray[s:(2*s),s1:(2*s1)])
    array.append(imgray[(2*s):(3*s),(2*s1):(3*s1)])
    array.append(imgray[(3*s):(4*s),(3*s1):(4*s1)])

    return array

def test_lbp(imgs_dicom,target):

    features_lbp = []

    METHOD = 'uniform'
    P = 50
    R = 15

    print('Iniciando extração de caracteristicas com LBP...')

    for im in imgs_dicom:
        #print('Calc LBP')
        lbp = ft.local_binary_pattern(im[0], P, R, METHOD)
        flbp, _ = np.histogram(lbp, normed=True, bins=P + 2, range=(0, P + 2))
        hist, bin_edges = np.histogram(im, density=True)
        f = flbp.tolist() + hist.tolist()
        features_lbp.append(f)

    print('Test LBP...')

    ac,p,f1,r,e = clf_randomForest(features_lbp,target)

    l = 'lbp', ac,p,f1,r,e, str(dt.now())

    logs_metricas.append(l)

    print('Acuracia = %f'%(ac))

    print('Precisao = %f'%(p))

    print('Recall = %f'%(r))

    print('Erro = %f'%(e))

    print('Test LBP Finalizado.')

    time.sleep(5)

def test_rc(imgs_dicom, target):

    features_rc = []

    print('Iniciando extração de caracteristicas com RC...')

    count = 0
    for im in imgs_dicom:
        count += 1
        print('%i'%(count))
        #secs = crop_slices(im[0])
        hist, bin_edges = np.histogram(im, density=True)
        frc = rc.extract_texture([im[0]])
        f = frc + hist.tolist()
        features_rc.append(f)
        write_txt(features_rc)

    print('Test RC...')

    ac,p,f1,r,e = clf_randomForest(features_rc,target)

    l = 'rc', ac,p,f1,r,e, str(dt.now())

    logs_metricas.append(l)

    print('Acuracia = %f'%(ac))

    print('Precisao = %f'%(p))

    print('Recall = %f'%(r))

    print('Erro = %f'%(e))

    print('Test RC Finalizado.')

def _test_rc(target):

    features_rc = []
    with open("features_.log", "r") as ins:
        for line in ins:
            line = line.strip("\n")
            vals = line.split(' ')
            vals = [float(i) for i in vals]
            features_rc.append(vals)

    print('Test RC...')

    ac,p,f1,r,e = clf_randomForest(features_rc,target)

    l = 'rc', ac,p,f1,r,e, str(dt.now())

    logs_metricas.append(l)

    print('Acuracia = %f'%(ac))

    print('Precisao = %f'%(p))

    print('Recall = %f'%(r))

    print('Erro = %f'%(e))

    print('Test RC Finalizado.')

if __name__ == '__main__':

    print("Iniciando do algoritmo...")

    #datas = read_csv('overview.csv')

    #df = convert_df(datas['Contrast'])

    #datas['Contrast'] =  df

    rc = texture_features_RC()

    df = load_patologias('../../KIMIA',rc)

    imgs_med = []

    print("Carregando as imagens...")

    for name in df['files']:
        imgs = rc.load_img(name)
        imgs_med.append(imgs)

    #test_rc(imgs_med,df['ca'])

    test_rc(imgs_med,df['class'])

    test_lbp(imgs_med,df['class'])

    write_csv(logs_metricas,'metricas')


import cv2
import time
import mediapipe as mp

# Grabbing the Holistic Model from Mediapipe and Initializing the Model
mp_holistic = mp.solutions.holistic
holistic_model = mp_holistic.Holistic(
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5
)

# Initializing the drawing utils for drawing the facial landmarks on the image
mp_drawing = mp.solutions.drawing_utils

# (0) in VideoCapture is used to connect to your computer's default camera
capture = cv2.VideoCapture(0)

# Initializing current time and previous time for calculating the FPS
previousTime = 0
currentTime = 0

# Focal length of the camera (adjust as per your camera)
focal_length = 1.40625

while capture.isOpened():
    # Capture frame by frame
    ret, frame = capture.read()

    # Resizing the frame for better view
    frame = cv2.resize(frame, (800, 600))

    # Converting from BGR to RGB
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Making predictions using the holistic model
    image.flags.writeable = False
    results = holistic_model.process(image)
    image.flags.writeable = True

    # Converting back the RGB image to BGR
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    # Drawing the facial landmarks
    mp_drawing.draw_landmarks(
        image,
        results.face_landmarks,
        mp_holistic.FACEMESH_CONTOURS,
        mp_drawing.DrawingSpec(color=(255, 0, 255), thickness=1, circle_radius=1),
        mp_drawing.DrawingSpec(color=(0, 255, 255), thickness=1, circle_radius=1)
    )

    # Drawing the right hand landmarks
    mp_drawing.draw_landmarks(
        image,
        results.right_hand_landmarks,
        mp_holistic.HAND_CONNECTIONS
    )

    # Drawing the left hand landmarks
    mp_drawing.draw_landmarks(
        image,
        results.left_hand_landmarks,
        mp_holistic.HAND_CONNECTIONS
    )

    # Calculating the FPS
    currentTime = time.time()
    fps = 1 / (currentTime - previousTime)
    previousTime = currentTime

    # Displaying FPS on the image
    cv2.putText(image, str(int(fps)) + " FPS", (10, 70), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)

    # # Accessing and calculating the depth of hand landmarks
    # if results.right_hand_landmarks:
    #     for landmark in results.right_hand_landmarks.landmark:
    #         # Get the normalized coordinates
    #         landmark_x = landmark.x
    #         landmark_y = landmark.y
    #         landmark_z = landmark.z
    #
    #         # Calculate the depth (distance from the camera)
    #         depth = landmark_z * focal_length  # Adjust the scale as needed
    #         print("Right Hand Landmark - Depth:", depth)

    if results.left_hand_landmarks:
        for landmark in results.left_hand_landmarks.landmark:
            # Get the normalized coordinates
            landmark_x = landmark.x
            landmark_y = landmark.y
            landmark_z = landmark.z

            # Calculate the depth (distance from the camera)
            depth = landmark_z   # Adjust the scale as needed
            print("Left Hand Landmark - Depth:", depth)

    # #Accessing and calculating the depth of face landmarks
    # if results.face_landmarks:
    #     for idx, landmark in enumerate(results.face_landmarks.landmark):
    #         # Get the normalized coordinates
    #         landmark_x = landmark.x
    #         landmark_y = landmark.y
    #         landmark_z = landmark.z
    #
    #         # Calculate the depth (distance from the camera)
    #         depth = landmark_z * focal_length  # Adjust the scale as needed
    #         print("Face Landmark", idx, "- Depth:", depth)

    # Display the resulting image
    cv2.imshow("Facial and Hand Landmarks", image)

    # Enter key 'q' to break the loop
    if cv2.waitKey(5) & 0xFF == ord('q'):
        break
capture.release()
cv2.destroyAllWindows()


#!/bin/python3

import sys
import os
import os.path as osp
from PIL import Image
import argparse
import logging

from plan2scene.config_manager import ConfigManager
from plan2scene.texture_gen.custom_transforms.random_crop import RandomResizedCropAndDropAlpha

if __name__ == "__main__":
    """
    This script is used to prepare texture crops (from texture dataset) used to train the substance classifier.
    """

    parser = argparse.ArgumentParser(description="Extract rectified surface crops from the opensurfaces dataset, to train the substance classifier.")
    parser.add_argument("output_path", type=str, help="Output directory to save texture crops.")
    parser.add_argument("input_path", type=str, help="Directory containing textures.")
    parser.add_argument("--crops-per-image", type=int, default=20)
    parser.add_argument("--crop-size", type=int, default=256)
    parser.add_argument("--output-size", type=int, default=128)
    parser.add_argument("--attempt-count", type=int, default=100)
    conf = ConfigManager()
    conf.add_args(parser)
    args = parser.parse_args()
    conf.process_args(args)

    output_path = args.output_path
    input_path = args.input_path

    # Configuration used
    crop_count = args.crops_per_image
    crop_size = (args.crop_size, args.crop_size)
    output_size = (args.output_size, args.output_size)
    attempt_count = args.attempt_count

    if osp.exists(output_path):
        logging.error("Output directory already exist")
        sys.exit(1)

    if not osp.exists(output_path):
        os.makedirs(output_path)

    image_file_paths = [osp.join(input_path, a) for a in os.listdir(input_path)]
    image_file_paths = [a for a in image_file_paths if osp.splitext(a)[1] in [".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG"]]
    logging.info("Found {count} files.".format(count=len(image_file_paths)))

    with open(osp.join(output_path, "index.html"), "w") as f:
        for image_file_path in image_file_paths:
            img_name = image_file_path.split("/")[-1]
            img = Image.open(image_file_path)
            index = 0
            for i in range(crop_count):
                crop = RandomResizedCropAndDropAlpha(crop_size, attempt_count, ratio=(1.0, 1.0))(img)
                if crop is not None:
                    crop = crop.resize(output_size)
                    crop.save(osp.join(output_path, img_name.split(".")[0] + "_crop%d.png" % index))
                    logging.info("Saved {file}.".format(file=osp.join(output_path, img_name.split(".")[0] + "_crop%d.png" % index)))
                    f.write("

%s
" % (
                    img_name.split(".")[0] + "_crop%d.png" % index, img_name))
                    index += 1
            f.flush()


import json
import urllib.request

"""
Inversion of Control (IoC)

In normal control sequence
  * the object/function finds the objects it depends on by itself
  * then calls them

With IoC, this is reversed (inverted)
  * The dependencies are handed to the object when it's created
  * Or functioncality to handle is passed to the function
"""

class SensorStore:
    URL = 'https://dummy-sensors.azurewebsites.net/api/sensor'
    def get_sensor(self, id):
        sensor_response = urllib.request.urlopen(f'{self.URL}/{id}') # <-- Not Inversion of control
        # SensorStore
        payload = sensor_response.read()
        return json.loads(payload.decode('utf-8'))

class Example:
    def __init__(self, store):
        self.store = store  # <-- Inversion of control / Dependency injenction

    def execute_logic(self, id, handle_data):
        sensor = self.store.get_sensor(id)
        if sensor:
            handle_data(sensor)  # <-- Inversion of control / callback
            # Using e.g. event bus would also be IoC
            # self.event_bus.publish({ type: 'new_data', payload: sensor })

def callback(sensor):
    id = sensor["id"]
    message = "hot" if sensor["data"] > 25 else "cold"
    print(f'{id}: {message}')

store= SensorStore()
example = Example(store)
example.execute_logic('iddqd', callback)


import pytest

import pytest_asyncio

from tests.integration.common import (
    COINBASE,
)
from tests.utils import (
    get_open_port,
)
from web3 import (
    AsyncWeb3,
    WebsocketProviderV2,
)
from web3._utils.module_testing.go_ethereum_admin_module import (
    GoEthereumAsyncAdminModuleTest,
)
from web3._utils.module_testing.go_ethereum_personal_module import (
    GoEthereumAsyncPersonalModuleTest,
)

from .common import (
    GoEthereumAsyncEthModuleTest,
    GoEthereumAsyncNetModuleTest,
)
from .utils import (
    wait_for_aiohttp,
)

@pytest.fixture(scope="module")
def ws_port():
    return get_open_port()

@pytest.fixture(scope="module")
def endpoint_uri(ws_port):
    return f"ws://localhost:{ws_port}"

def _geth_command_arguments(ws_port, base_geth_command_arguments, geth_version):
    yield from base_geth_command_arguments
    if geth_version.major == 1:
        yield from (
            "--miner.etherbase",
            COINBASE[2:],
            "--ws",
            "--ws.port",
            ws_port,
            "--ws.api",
            "admin,eth,net,web3,personal,miner",
            "--ws.origins",
            "*",
            "--ipcdisable",
            "--allow-insecure-unlock",
        )
        if geth_version.minor not in [10, 11]:
            raise AssertionError("Unsupported Geth version")
    else:
        raise AssertionError("Unsupported Geth version")

@pytest.fixture(scope="module")
def geth_command_arguments(
    geth_binary, get_geth_version, datadir, ws_port, base_geth_command_arguments
):
    return _geth_command_arguments(
        ws_port, base_geth_command_arguments, get_geth_version
    )

@pytest_asyncio.fixture(scope="module")
async def async_w3(geth_process, endpoint_uri):
    await wait_for_aiohttp(endpoint_uri)
    async with AsyncWeb3.persistent_websocket(
        WebsocketProviderV2(endpoint_uri, call_timeout=30)
    ) as w3:
        yield w3

class TestGoEthereumAsyncAdminModuleTest(GoEthereumAsyncAdminModuleTest):
    @pytest.mark.xfail(
        reason="running geth with the --nodiscover flag doesn't allow peer addition"
    )
    async def test_admin_peers(self, async_w3: "AsyncWeb3") -> None:
        await super().test_admin_peers(async_w3)

    async def test_admin_start_stop_http(self, async_w3: "AsyncWeb3") -> None:
        # This test causes all tests after it to fail on CI if it's allowed to run
        pytest.xfail(
            reason="Only one HTTP endpoint is allowed to be active at any time"
        )
        await super().test_admin_start_stop_http(async_w3)

    async def test_admin_start_stop_ws(self, async_w3: "AsyncWeb3") -> None:
        # This test inconsistently causes all tests after it to
        # fail on CI if it's allowed to run
        pytest.xfail(
            reason="Only one WebSocket endpoint is allowed to be active at any time"
        )
        await super().test_admin_start_stop_ws(async_w3)

class TestGoEthereumAsyncEthModuleTest(GoEthereumAsyncEthModuleTest):
    pass

class TestGoEthereumAsyncNetModuleTest(GoEthereumAsyncNetModuleTest):
    pass

class TestGoEthereumAsyncPersonalModuleTest(GoEthereumAsyncPersonalModuleTest):
    pass


#!/usr/bin/python

import os
import subprocess
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient
import json

# Setup the environment
os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'
os.environ['AWS_ACCESS_KEY_ID'] = '<YOUR_KEY_ID>'
os.environ['AWS_SECRET_ACCESS_KEY'] = '<YOUR_KEY>'
os.environ['AWS_SESSION_TOKEN'] = '<YOUR_TOKEN>'
os.environ['GST_PLUGIN_PATH'] = '~/project/amazon-kinesis-video-streams-producer-sdk-cpp/build'

# Setup our MQTT client and security certificates
# Make sure your certificate names match what you downloaded from AWS IoT

# Note: We are using the Shadow Client
mqttc = AWSIoTMQTTShadowClient("enzhi_camera_stream")

# Make sure you use the correct region!
mqttc.configureEndpoint(
    "<YOUR_ENDPOINT>", 8883)
mqttc.configureCredentials(
    "./AmazonRootCA1.pem", "./private.pem.key", "./certificate.pem.crt")

shadowClient = mqttc.createShadowHandlerWithName("enzhi_camera_stream", True)

# Function to encode a payload into JSON

def json_encode(string):
    return json.dumps(string)

mqttc.json_encode = json_encode
shadowClient.json_encode = json_encode

def on_message(message, response, token):
    print(message)
    delta = json.loads(message)
    if 'switch' in delta['state'] and delta['state']['switch'] == 1:
        # print(os.getenv('GST_PLUGIN_PATH'))
        width = 100 #1280 640
        height = 100 #720 480
        if 'width' in delta['state']:
            width = delta['state']['width']
        if 'height' in delta['state']:
            height = delta['state']['height']
        strcmd = 'gst-launch-1.0 autovideosrc ! videoconvert ! video/x-raw,format=I420,width=' + str(width) + ',height=' + str(height) + ',framerate=30/1 ! vtenc_h264_hw allow-frame-reordering=FALSE realtime=TRUE max-keyframe-interval=45 bitrate=500 ! h264parse ! video/x-h264,stream-format=avc,alignment=au,profile=baseline ! kvssink stream-name=enzhi_camera_stream storage-size=128'
        print('###'+ strcmd)
        subprocess.run(strcmd, shell=True)

mqttc.on_message = on_message
shadowClient.on_message = on_message

mqttc.connect()
print("Connected")
shadowClient.shadowRegisterDeltaCallback(on_message)
print("Listening for Delta Messages")

# Loop forever
while True:
    pass


from __future__ import annotations
import lib.utils as utils

import lib.commons.Platform as Platform
import lib.commons.Credentials as Credentials
import lib.commons.Repository as Repository
import lib.commons.User as User

import requests
import logging

class Gitea(Platform):
    @staticmethod
    @utils.deprecated("Use Platform.from_dict instead")
    def from_env(key: str) -> Gitea:
        import lib.secrets as secrets
        return Gitea(secrets.get_env_or_raise("%s_GITEA_URL" % key))

    def __init__(self, instance: str):
        super().__init__(instance)

    def get_user_repositores(self, user: User, credentials: Credentials) -> list[Repository]:
        auth = (credentials.get_username(),
                credentials.get_token())
        headers = {
            "accept": "application/json"
        }
        username = user.get_username()
        response = requests.get("%s/api/v1/users/%s/repos" % (self.get_url(),
                                                              username),
                                headers=headers, auth=auth)
        return Repository.from_dict_list(response.json())

    def _validate_migration_config(self, config: dict = None) -> dict:
        if config is None:
            config = {}
        if "mirror" not in config:
            config["mirror"] = False
        if "private" not in config:
            config["private"] = False
        if "wiki" not in config:
            config["wiki"] = True
        return config

    def migrate_repository(self, repo: Repository, config: dict, credentials: Credentials) -> Repository:
        config = self._validate_migration_config(config)
        url = "%s/api/v1/repos/migrate" % self.get_url()
        auth = (credentials.get_username(),
                credentials.get_token())
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        data = {
            "auth_username": credentials.get_username(),
            "auth_token": credentials.get_token(),
            "clone_addr": repo.get_url(),
            "mirror": config["mirror"],
            "private": config["private"],
            "repo_name": repo.get_name(),
            "repo_owner": credentials.get_username(),
            "service": "git",
            "uid": 0,
            "wiki": config["wiki"]
        }
        response = requests.post(url, headers=headers, auth=auth, json=data)
        if bool(response):
            return Repository.from_dict(response.json())
        else:
            logging.error("%s: %s" % (repo, response.json()['message']))
            return None

    def delete_repository(self, repo: Repository, credentials: Credentials) -> bool:
        url = "%s/api/v1/repos/%s" % (self.get_url(), repo.get_full_name())
        print(url)
        auth = (credentials.get_username(),
                credentials.get_token())
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        response = requests.delete(url, headers=headers, auth=auth)
        if bool(response):
            return True
        else:
            # logging.error("%s: %s" % (repo, response.json()['message']))
            logging.error("%s: %s" % (repo, response))
            return False


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time       : 2020/12/10 15:33
@Author     : Boyka
@Contact    : upcvagen@163.com
@File       : multi_scale.py
@Project    : GraduationProject
@Description:
"""
import cv2

if __name__ == '__main__':
    origin_image = "1.jpg"
    tar_image = "2.jpg"
    img = cv2.imread(origin_image)
    print(img.shape)
    cv2.imwrite(tar_image, img[0: int(img.shape[0] / 2), 0: int(img.shape[1] / 2)])


from sklearn.preprocessing import MultiLabelBinarizer

def get_index_to_meshid(indx = -1, class_name = '', meshIDs=[]):
    if meshIDs == []:
        mapping_id = {}
        with open("/KenMeSH-master/meSH_pair.txt", 'r') as f:
            for line in f:
                (key, value) = line.split('=')
                mapping_id[key] = value.strip()
        meshIDs = list(mapping_id.values())

    # print("meshIDs: ", meshIDs)

    mlb = MultiLabelBinarizer(classes=meshIDs)
    mlb.fit(meshIDs)

    if indx != -1:
        return mlb.classes_[indx]
    else:
        return list(mlb.classes_).index(class_name)

# print(get_index_to_meshid(indx=13536))