from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
import tensorflow as tf
import time
import os
from io import BytesIO
import urllib
import pathlib
import pymongo
from datetime import datetime
from dotenv import load_dotenv

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#Disable GPU and force TF to use CPU only
tf.config.set_visible_devices([], 'GPU')
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

load_dotenv(verbose=True, override=True)

class SudokuPredict():

    def __init__(self, file_name=None, version=None):
        self.version = version if version is not None else os.environ['SUDOKU_MODEL_VERSION']
        self.model_file_name = file_name if file_name is not None else os.environ['SUDOKU_MODEL_FILE_NAME']
        print(f"Model will be loaded: {self.model_file_name} with version: {self.version}")

        self.model = load_model('model/' + self.model_file_name)

        mongo_client = pymongo.MongoClient(os.getenv('MONGO_URI'))
        self.mongo_db = mongo_client.sudoku

        self.log_doc = {}

    def __load_image_from_local(self, filepath):
        # load the image from File
        img = load_img(filepath, color_mode = "grayscale", target_size=(28, 28))
        self.log_doc["loaded_img_colormode"] = "grayscale"
        self.log_doc["loaded_img_target_size"] = (28, 28)
        return img

    def __load_image_from_URL(self, URL):
        # load the image from URL
        temp_filename = self.__generate_temp_filename()

        # download image to a temp location
        filepath, _ = urllib.request.urlretrieve(URL, temp_filename)
        img = self.__load_image_from_local(filepath)

        # remove temp image
        os.remove(temp_filename)
        return img

    def get_tensorflow_parameters(self):
        return tf.version.VERSION, tf.version.COMPILER_VERSION

    def __load_image_from_memory(self, image):
        # load the image from ByteIO
        temp_filename = self.__generate_temp_filename()

        pathlib.Path(temp_filename).write_bytes(image.getbuffer())
        img = self.__load_image_from_local(temp_filename)

        os.remove(temp_filename)
        return img

    def __preprocess_image(self, image):
        # convert to array
        img = img_to_array(image)
        self.log_doc["img_to_array"] = img.tolist()

        # reshape into a single sample with 1 channel
        img = img.reshape(1, 28, 28, 1)
        # prepare pixel data
        img = img.astype('float32')
        # invert colors
        img = (255.0 - img)
        img = img / 255.0
        self.log_doc["post_process"] = img.tolist()

        return img

    def __predict(self, img):
        result = self.model.predict(img)[0].tolist()
        self.log_doc["predict_result"] = result

        prob = max(result)
        self.log_doc["predict_prob"] = float(prob)

        digit = result.index(prob)
        self.log_doc["predict_digit"] = int(digit)

        #digit = self.model.predict_classes(img)

        return (int(digit), float(prob))

    def predict_local(self, filepath):
        img = self.__load_image_from_local(filepath)
        img = self.__preprocess_image(img)

        return self.__predict(img)

    def predict_URL(self, URL):
        img = self.__load_image_from_URL(URL)
        img = self.__preprocess_image(img)

        return self.__predict(img)

    def predict_memory(self, bytesio_image, transaction_id=None):
        self.__log_init(transaction_id)

        img = self.__load_image_from_memory(bytesio_image)
        img = self.__preprocess_image(img)
        result = self.__predict(img)

        self.__save_log()
        return result

    def __generate_temp_filename(self):
        return 'tmp/' + str(int(time.time()*1000000)) + ".png"

    def __save_log(self):
        self.mongo_db.predictions_log.insert_one(self.log_doc)
        pass

    def __log_init(self, transaction_id):
        self.log_doc = {}
        self.log_doc["transaction_id"] = transaction_id
        self.log_doc["model_file_name"] = self.model_file_name
        self.log_doc["version"] = self.version
        self.log_doc["datetime"] = datetime.utcnow()


import peeweedbevolve # new; must be imported before models
from flask import Flask
from models import *
from flask import render_template, request, flash, redirect, url_for, g
from peewee import *

app = Flask(__name__)
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'pineapplesarenice'

@app.before_request
def before_request():
   db.connect()

@app.after_request
def after_request(response):
   db.close()
   return response

@app.cli.command() # new
def migrate(): # new
   db.evolve(ignore_tables={'base_model'}) # new

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

@app.route("/store", methods=['GET', 'POST'])
def store():
   store_name = request.form.get('store_name')
   stores = Store.select()

   if request.method == 'POST':
      s = Store(name=store_name)
      if s.save():
         flash(f"{store_name} was added.")
         return redirect(url_for('store'))
      else:
         # else statement isn't working when trying to add store with same name --> add validation later
         flash("Failed to add store. Try again?")
         return render_template('stores.html', stores=stores, errors=s.errors)
   else:
      return render_template('stores.html', store_name=store_name, stores=stores)

@app.route("/warehouse", methods=['GET'])
def warehouse():
   stores_wh = Store.select()
   return render_template('warehouse.html', stores_wh=stores_wh)

@app.route("/warehouse_form", methods=['POST'])
def warehouse_create():
   store = Store.get_by_id(request.form.get('stores_list'))
   wh_location_input = request.form.get('wh_location_input')

   w = Warehouse.create(store=store,location=wh_location_input)

   if w.save():
      flash("Warehouse location added.")
      return redirect(url_for('warehouse'))
   else:
      flash("Failed to add warehouse. Try again?")
      return render_template('warehouse.html')

@app.route("/store_list", methods=['GET'])
def store_index():
   stores = Store.select()
   return render_template('store_list.html', stores=stores)

@app.route("/store/<int:store_id>", methods=['GET'])
def store_show(store_id):
   s_info = Store.get_by_id(store_id)
   return render_template('store_info.html', store_id=store_id, s_info=s_info)

@app.route("/store/<int:store_id>/edit", methods=['POST'])
def edit_store(store_id):
   new_name = request.form.get('new_name')

   u = Store.update(name = new_name).where(Store.id == store_id)
   u.execute()
   return redirect(url_for('store_show', store_id=store_id))

@app.route("/store/<int:store_id>/delete", methods=['POST'])
def destroy_store(store_id):
   d = Store.get(Store.id == store_id)
   d.delete_instance()
   return redirect(url_for('store_index'))

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


import numpy as np
import cv2 as cv
import sys
sys.path.insert(0, '..')
import config as cfg

def get_resized_image(image):
  im = image.astype(np.float32, copy=True)
  target_size = cfg.vgg_size
  im_new = cv.resize(im, (target_size, target_size), interpolation=cv.INTER_LINEAR)
  im_new -= cfg.PIXEL_MEANS
  #im_new /= cfg.PIXEL_SCALE
  return im_new

def get_blobs(img, label):
  im_new = get_resized_image(img)

  #reshape these things to match the network
  #the image needs a channel swap
  channel_swap = (2, 0, 1)
  im_new = im_new.transpose(channel_swap)
  im_new = im_new.reshape((1, im_new.shape[0], im_new.shape[1], im_new.shape[2]))

  label_new = np.array(label).reshape((1,1,1,-1))

  return im_new, label_new


# -*- coding: utf-8 -*-
import numpy as np
import geatpy as ea

class TestProblem(ea.Problem): # 继承Problem父类
    def __init__(self, testName): # testName为测试集名称
        name = testName # 初始化name
        # 读取城市坐标数据
        self.places=np.loadtxt("data/" + testName + ".csv",delimiter=",",usecols=(0,1))
        M = 1 # 初始化M（目标维数）
        Dim = self.places.shape[0] # 初始化Dim（决策变量维数）
        maxormins = [1] * M # 初始化maxormins（目标最小最大化标记列表，1：最小化该目标；-1：最大化该目标）
        varTypes = [0] * Dim # 初始化varTypes（决策变量的类型，0：实数；1：整数）
        lb = [0] * Dim # 决策变量下界
        ub = [Dim - 1] * Dim # 决策变量上界
        lbin = [1] * Dim # 决策变量下边界（0表示不包含该变量的下边界，1表示包含）
        ubin = [1] * Dim # 决策变量上边界（0表示不包含该变量的上边界，1表示包含）
        # 调用父类构造方法完成实例化
        ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)

    def aimFunc(self, pop): # 目标函数
        x = pop.Phen.copy() # 得到决策变量矩阵
        # 添加最后回到出发地
        X = np.hstack([x, x[:, [0]]]).astype(int)
        ObjV = [] # 存储所有种群个体对应的总路程
        for i in range(pop.sizes):
            journey = self.places[X[i], :] # 按既定顺序到达的地点坐标
            distance = np.sum(np.sqrt(np.sum(np.diff(journey.T)**2, 0))) # 计算总路程
            ObjV.append(distance)
        pop.ObjV = np.array([ObjV]).T


import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
import histomicstk.preprocessing.color_normalization as hist

if __name__ == '__main__':
    pink_path = "/media/krukts/HDD/BioDiploma/Timofey/dataset_PINK_300k_Stage2_WithErrorNormCls/Tumor/Tumor_001_tilec_TUM2_1.png"
    blue_path = "/media/krukts/HDD/BioDiploma/Timofey/dataset_BLUE_300k/Tumor/tumor_071_tilec_TUM2_15.png"

    pink = cv2.imread(pink_path)
    pink = cv2.cvtColor(pink, cv2.COLOR_BGR2RGB)
    blue = cv2.imread(blue_path)
    blue = cv2.cvtColor(blue, cv2.COLOR_BGR2RGB)

    plt.title("PINK")
    plt.imshow(pink)
    plt.show()
    plt.close()

    plt.title("BLUE")
    plt.imshow(blue)
    plt.show()
    plt.close()
    # cv2.imwrite("l.png",pink)

    # Normalization
    result_pink = hist.deconvolution_based_normalization(pink)
    result_blue = hist.deconvolution_based_normalization(blue)

    plt.title("PostPink")
    plt.imshow(result_pink)
    plt.show()
    plt.close()

    plt.title("PostBlue")
    plt.imshow(result_blue)
    plt.show()
    plt.close()
    # cv2.imwrite("pink.png",pink)
    # cv2.imwrite("resultpink.png",result_pink)


# -*- coding: utf-8 -*-
"""somalier_extract_step module

See domain package __init__.py doc string.
See orchestration package __init__.py doc string.
"""
import logging
from typing import List, Dict

from libumccr.aws import libssm, libsqs

from data_portal.models.workflow import Workflow
from data_processors.pipeline.domain.config import SQS_SOMALIER_EXTRACT_QUEUE_ARN
from data_processors.pipeline.domain.somalier import SomalierReferenceSite
from data_processors.pipeline.domain.workflow import WorkflowType
from data_processors.pipeline.tools import liborca

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

def perform(this_workflow: Workflow):

    # prepare job list and dispatch to job queue
    job_list = prepare_somalier_extract_jobs(this_workflow)

    if job_list:
        libsqs.dispatch_jobs(queue_arn=libssm.get_ssm_param(SQS_SOMALIER_EXTRACT_QUEUE_ARN), job_list=job_list)
    else:
        logger.warning(f"Calling to prepare_somalier_extract_jobs() return empty list, no job to dispatch...")

    return {
        "somalier_extract_step": job_list
    }

def prepare_somalier_extract_jobs(this_workflow: Workflow) -> List[Dict]:
    """
    TL;DR is if there is 1 dragen wgs qc or wts or tso workflow, there will be one somalier extraction step workflow
    :param this_workflow:
    :return:
    """

    job_list = []
    gds_bam_path = None
    reference = SomalierReferenceSite.HG38_RNA.value

    if this_workflow.type_name.lower() == WorkflowType.DRAGEN_WTS.value.lower():
        gds_bam_path = liborca.parse_transcriptome_output_for_bam_file(this_workflow.output)

    elif this_workflow.type_name.lower() == WorkflowType.DRAGEN_WGS_QC.value.lower():
        gds_bam_path = liborca.parse_wgs_alignment_qc_output_for_bam_file(this_workflow.output)

    elif this_workflow.type_name.lower() == WorkflowType.DRAGEN_TSO_CTDNA.value.lower():
        gds_bam_path = liborca.parse_tso_ctdna_output_for_bam_file(this_workflow.output)
        reference = SomalierReferenceSite.HG19_RNA.value  # switch reference site to HG19 if ctTSO

    if gds_bam_path is not None:
        job_list.append({
            "index": gds_bam_path,
            "reference": reference
        })

    return job_list


fish = ' 23 walruses '

fish = fish.strip()

aFish = dict()

try:
    rexp = '(\d*)'
    aFish['numFish'] =  re.search(rexp,fish).group(1).strip()
except:
    aFish['numFish']= '?'
# Get type/species of fish
try:
    aFish['species'] = fish.replace(aFish['numFish'],'').strip()
except:
    aFish['species'] = '?'


from urllib.parse import urlparse,parse_qs
import urllib
import copy
import requests

xss_payloads = ['<xi4okv>', 'javascript::alert`1`']
sqli_payloads = ["%27","\\","/"]
lfi_payloads = ['../../../etc/passwd']

payloads = xss_payloads + sqli_payloads + lfi_payloads

def fuzz(url):
    check = []
    url = urllib.parse.unquote(url, encoding='utf-8', errors='replace')
    up = urlparse(url)
    try:
        if up.query == '':
            return
        for ext in ['.js','.jpg','.css','.gif','.png','.txt']:
            if ext in up.path:
                return
        print('fuzz ' + url)
    except:
        return

    query = parse_qs(up.query)

    #Fuzz
    for i in query:
        for j in payloads:
            queryCheck = copy.deepcopy(query)
            upCheck = copy.deepcopy(up)
            queryCheck[i][0] = j

            queryTarget = ''
            for i in queryCheck:
                data = i + '=' + queryCheck[i][0] + '&'
                queryTarget += data

            url = up.scheme + '://' + up.netloc + up.path + '?' + queryTarget

            check.append(url[:-1])

    for i in query:
        for j in payloads:
            queryCheck = copy.deepcopy(query)
            upCheck = copy.deepcopy(up)
            queryCheck[i][0] = urllib.parse.quote(j)

            queryTarget = ''
            for i in queryCheck:
                data = i + '=' + queryCheck[i][0] + '&'
                queryTarget += data

            url = up.scheme + '://' + up.netloc + up.path + '?' + queryTarget

            check.append(url[:-1])

    for url in check:
        try:
            text = requests.get(url,timeout=5).text
            for i in xss_payloads:
                if i in text:#and 'text' in text.headers['Content-Type']:
                    print ("XSS ==> " + url)
            if 'SQL syntax' in text or 'mysql_fetch' in text:
                print("SQLi ==> " + url)
            if 'root:/root:' in text:
                print("LFI ==> " + url)
        except:
            pass

#fuzz("http://testphp.vulnweb.com/artists.php?artist=3")


import unittest

import mock

import advanced_example_code

class TestAdvancedExampleCode(unittest.TestCase):
    @mock.patch('serial.Serial')
    def test_open_serial_port(self, serial_mock):
        serial_port_mock = mock.MagicMock()
        serial_mock.return_value = serial_port_mock

        serial_port = ors_tutorial_code.open_serial_port()

        serial_mock.assert_called_once_with('/dev/ttyACM0', 9600)
        self.assertEqual(serial_port, serial_port_mock)

    def test_read_serial_port(self):
        serial_port_mock = mock.MagicMock()

        ors_tutorial_code.read_serial_port(serial_port_mock)

        serial_port_mock.readline.assert_called_once_with()

    @mock.patch('sensor_msgs.msg.NavSatFix')
    def test_clean_data(self, nav_sat_fix_mock):
        data_template = '{latitude},{longitude},{altitude}'
        data = data_template.format(
            latitude=-71.4, longitude=42.3, altitude=20.5)

        ors_tutorial_code.clean_data(data)

        nav_sat_fix_mock.assert_called_once_with(
            latitude=-71.4, longitude=42.3, altitude=20.5)

    @mock.patch('sensor_msgs.msg.NavSatFix')
    def test_calculate_boat_position(self, nav_sat_fix_mock):
        sensor_position_mock = mock.MagicMock()
        sensor_position_mock.latitude = 1.1
        sensor_position_mock.longitude = 2.2
        sensor_position_mock.altitude = 4.0

        ors_tutorial_code.calculate_boat_position(sensor_position_mock)

        nav_sat_fix_call = nav_sat_fix_mock.mock_calls[0]
        nav_sat_fix_kwargs = nav_sat_fix_call[2]
        self.assertEqual(nav_sat_fix_kwargs['latitude'], 1.1)
        self.assertEqual(nav_sat_fix_kwargs['longitude'], 2.2)
        self.assertAlmostEqual(nav_sat_fix_kwargs['altitude'], 0.1)

        self.assertEqual(sensor_position_mock.latitude, 1.1)
        self.assertEqual(sensor_position_mock.longitude, 2.2)
        self.assertEqual(sensor_position_mock.altitude, 4.0)

    @mock.patch('ors_tutorial_code.calculate_boat_position')
    @mock.patch('ors_tutorial_code.clean_data')
    @mock.patch('ors_tutorial_code.read_serial_port')
    def test_publish_position(self, read_serial_port_mock, clean_data_mock,
                              calculate_boat_position_mock):
        serial_port_mock = mock.MagicMock()
        publisher_mock = mock.MagicMock()

        read_serial_port_mock.return_value = 1
        clean_data_mock.return_value = 2
        calculate_boat_position_mock.return_value = 3

        ors_tutorial_code.publish_position(serial_port_mock, publisher_mock)

        read_serial_port_mock.assert_called_once_with(serial_port_mock)
        clean_data_mock.assert_called_once_with(1)
        calculate_boat_position_mock.assert_called_once_with(2)
        publisher_mock.publish.assert_called_once_with(3)

if __name__ == '__main__':
    unittest.main()


from django.db import models
from django.db.utils import IntegrityError
import twitter
from . import twittercreds
import pandas as pd
import os

countries = pd.read_csv(
    os.path.join(os.path.dirname(__file__), 'csv/countries.csv'))
# Keep only country names
country_names = countries.iloc[:, 0:1].values.tolist()

# countries = countries[]
t = twitter.Twitter(
    auth=twitter.OAuth(
        twittercreds.token,
        twittercreds.token_secret,
        twittercreds.consumer_key,
        twittercreds.consumer_secret))

class TwitterManager(models.Manager):
    """
    Manager extension to add the fetch method to collect tweets.
    """
    def fetch(self, user, n_latest):
        """
        Fetch the n_latest latest tweets from @user and store them.
        """
        collected = t.statuses.user_timeline(
            screen_name=user,
            count=n_latest,
            tweet_mode='extended')
        for tuit in collected:
            # print(tuit.get('user').get('screen_name'))
            # print(tuit.get('full_text'))
            # print(tuit.get('retweeted_status', False))
            try:
                rt = tuit.get('retweeted_status', False)
                if rt:
                    text = rt.get('full_text')
                else:
                    text = tuit.get('full_text')
                # This is very far from optimal, but very flexible for an
                # iterative development:
                tweet_country=False
                for country in country_names:
                    if country[0] in text:
                        tweet_country = country[0]
                        break
                Tweet.objects.create(user=tuit.get('user').get('screen_name'),
                                     message=text,
                                     country=tweet_country
                                     )
            except IntegrityError:
                print("This tweet is already in the database")

class Tweet(models.Model):
    """
    Simplistic tweet model
    """
    # Not caring yet about the actual max length on twitter:
    message = models.CharField(max_length=300)
    user = models.CharField(max_length=35)
    # Starting as a charfield instead of another model
    country = models.CharField(max_length=50, null=True, blank=True)
    objects = TwitterManager()

    def __str__(self):
        return self.user + ': ' + self.message[:20] + '...'

    class Meta:
        unique_together = ("user", "message")


import cv2

bgr = cv2.imread('l1-1.jpg', 1)
yuv = cv2.cvtColor(bgr, cv2.COLOR_BGR2YUV)
y, u, v = cv2.split(yuv)

cv2.imshow('y', y)
cv2.imshow('u', u)
cv2.imshow('v', v)
cv2.waitKey(0)
cv2.destroyAllWindows()