import numpy as np

from bases import SimpleGate
from pycyt import math, util

class PolyGate(SimpleGate):

	def __init__(self, channels, vertices, **kwargs):

		if len(channels) != 2:
			raise ValueError('PolyGate must have exactly two channels')

		super(PolyGate, self).__init__(channels, **kwargs)

		if len(vertices) < 2:
			raise ValueError('Must have at least 3 vertices')

		# Process vertices
		self._vertices = []
		for vertex in vertices:

			# Convert to ndarray, remove extra axes
			vertex = np.squeeze(vertex)

			if vertex.shape != (2,):
				raise ValueError('Vertices must be 2-dimensional')

			self._vertices.append(vertex)

		# Check if polygon is convex
		self._is_convex, self._is_rh = math.is_poly_convex(self._vertices)

		# Compute bounding box
		self._bbox = [
			[f(v[i] for v in self._vertices) for f in (min, max)]
			for i in range(2)]

	@property
	def is_convex(self):
		return self._is_convex

	@property
	def bbox(self):
		return [list(r) for r in self._bbox]

	@property
	def vertices(self):
		return self._vertices[:]

	def copy(self, channels=None, vertices=None, **kwargs):
		if channels is None:
			channels = self._channels
		if vertices is None:
			vertices = self._vertices

		return PolyGate(channels, vertices, **kwargs)

	def _inside(self, array):

		# Convex polygon can use faster algorithm
		if self._is_convex:

			contains = np.full(array.shape[0], True, dtype=np.bool)

			# Loop over pairs of vertices (sides)
			for v1, v2 in util.cycle_adjacent(self._vertices, 2):

				# Vectors from vertex 1 to all test points
				dp = array - v1

				# Vector from vertex 1 to vertex 2
				dv = v2 - v1

				# Cross product between them
				cp = dp[:,0] * dv[1] - dp[:,1] * dv[0]

				# Reject where cp isn't positive (or negative if left-handed)
				contains &= (cp > 0) ^ self._is_rh

			return contains

		# Non-convex is slower (there is probably a faster way though)
		else:

			# Test against bounding box
			in_box = np.ones(array.shape[0], dtype=np.bool)
			in_box &= array[:,0] > self._bbox[0][0]
			in_box &= array[:,0] < self._bbox[0][1]
			in_box &= array[:,1] > self._bbox[1][0]
			in_box &= array[:,1] < self._bbox[1][1]

			box_points = array[in_box, :]

			# Will cast a ray from the point in the positive x direction,
			# if it crosses an odd number of times the point is inside.
			# Note - special cases arise when the ray intersects a side
			# just at a vertex. If we count this as a regular side
			# intersection it will be counted twice, which is the same as
			# not counting it at all. Technically this will never ever
			# happen enough to make a difference in an analysis but I'm
			# being pedantic here. The solution is to count all rays
			# passing through a vertex as actually going just above the
			# vertex, forcing it to be consistently counted one way or
			# the other.
			in_p = np.zeros(box_points.shape[0], dtype=np.bool)

			# Loop over 3-tuples of vertices (pairs of sides)
			# Will check the first side each loop but the 2nd is
			# sometimes needed if the ray intersects at a vertex.
			for v1, v2, v3 in util.cycle_adjacent(self._vertices, 3):

				# Indices of points currently looking at
				ci = np.arange(box_points.shape[0])

				# Translate to put v1 at origin
				t_points = box_points - v1
				v = v2 - v1

				# Bounding box of side in these coordinates
				left = min(v[0], 0)
				right = max(v[0], 0)
				bottom = min(v[1], 0)
				top = max(v[1], 0)

				# Skip if point entirely above or below side
				# Note we use >= on the top and < on the bottom, according
				# to the rule of counting exact intersection with a vertex
				# to being slightly above it. This also avoids counting
				# the case where the side is horizontal (v[1] == 0 and
				# top == bottom).
				which = (t_points[:, 1] >= top) |  (t_points[:, 1] < bottom)
				t_points = t_points[~which, :]
				ci = ci[~which]

				# Skip if point is to right of side's bounding box
				which = t_points[:, 0] >= right
				t_points = t_points[~which, :]
				ci = ci[~which]

				# Yes if point to left of side's bounding box
				which = t_points[:, 0] < left
				in_p[ci[which]] = ~in_p[ci[which]]
				t_points = t_points[~which, :]
				ci = ci[~which]

				# Point within bounding box - compare slopes
				which = (v[1] > 0) == (v[0] * t_points[:, 1] > v[1] * t_points[:, 0])
				in_p[ci[which]] = ~in_p[ci[which]]

			contains = np.zeros(array.shape[0], dtype=np.bool)
			contains[in_box] = in_p
			return contains


import argparse
import pandas as pd
import csv
import numpy as np
import json
import sys
sys.path.insert(0, '..')  # Add path from parent folder
sys.path.insert(0, '.')  # Add path from current folder
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.externals import joblib
from sklearn.grid_search import ParameterGrid
from shutil import move
import os

"""
 This class is similar to SKLearn_RandomForest but permutes the labels prior to training.
 The seed for np.random.seed(seed=process_num) is set to process_num.

 Note: Unlike the other models, this model was trained and evaluated on the prospective data
       AFTER the true prospective data was available. The goal is to see if scrambling the
       the labels would result in worse performance for RF_h (best model on prospective data
       according to top250hits).
"""
class RFH_YScrambled:
    def __init__(self, conf, process_num):
        self.conf = conf
        self.input_layer_dimension = 1024
        self.label_names = conf['label_names']
        self.EF_ratio_list = conf['enrichment_factor']['ratio_list']

        self.process_num = process_num

        self.param = conf['params']
        self.n_estimators = self.param['n_estimators']
        self.max_features = self.param['max_features']
        self.min_samples_leaf = self.param['min_samples_leaf']
        self.class_weight = self.param['class_weight']
        print('Testing set:', self.param)

        self.model_dict = {}
        return

    def get_prediction_info(self, X, y_true):
        y_pred = np.zeros(shape=y_true.shape)

        for i, label in zip(range(len(self.label_names)), self.label_names):
            model = self.model_dict[label]

            y_true[np.where(np.isnan(y_true[:,i]))[0],i] = -1
            if i in [0,1,2]:
                rf_pred = model.predict_proba(X)
                if rf_pred.shape[1] == 1:
                    y_pred[:,i] = 0
                else:
                    y_pred[:,i] = rf_pred[:,1]

        return y_true, y_pred

    def setup_model(self):
        for i in range(len(self.label_names)):
            self.model_dict[self.label_names[i]] = RandomForestClassifier(n_estimators=self.n_estimators,
                                           max_features=self.max_features,
                                           min_samples_leaf=self.min_samples_leaf,
                                           n_jobs=3,
                                           class_weight=self.class_weight,
                                           random_state=self.process_num,
                                           oob_score=False,
                                           verbose=1)
        return

    def train(self, X_train, y_train, model_file):
        self.setup_model()

        # perform random shuffling of training data (including X_train)
        p = np.random.permutation(len(X_train))
        X_train = X_train[p,:]
        y_train = y_train[p,:]

        for i, label in zip(range(len(self.label_names)), self.label_names):
            y = y_train[:,i]
            indexes = np.where(np.isnan(y))[0]

            y = np.delete(y, indexes, axis=0)
            X = np.delete(X_train, indexes, axis=0)
            self.model_dict[label].fit(X, y)

            joblib.dump(self.model_dict[label], model_file+'_'+label+'.pkl', compress = 1)
        return

    def predict_with_existing(self, X, y):
        y_true, y_pred = self.get_prediction_info(X, y)

        print
        print('precision: {}'.format(precision_auc_multi(y_true, y_pred, range(y_true.shape[1]), np.mean)))
        print('roc: {}'.format(roc_auc_multi(y_true, y_pred, range(y_true.shape[1]), np.mean)))
        print('bedroc: {}'.format(bedroc_auc_multi(y_true, y_pred, range(y_true.shape[1]), np.mean)))
        print

        label_list = self.label_names
        nef_auc_mean = np.mean(np.array(nef_auc(y_true, y_pred, self.EF_ratio_list, label_list)))
        print('nef auc: {}'.format(nef_auc_mean))
        return

    def save_model_params(self, config_csv_file):
        data = str(self.param)
        with open(config_csv_file, 'w') as csvfile:
            csvfile.write(data)
        return

'''
Note: Copied from function.py

Read the data from all files in input_file_list
And merged into one dataset
'''
def read_merged_data(input_file_list, usecols=None):
    whole_pd = pd.DataFrame()
    for input_file in input_file_list:
        data_pd = pd.read_csv(input_file, usecols=usecols)
        whole_pd = whole_pd.append(data_pd)
    return whole_pd

'''
Note: Copied from function.py

Reshape vector into 2-dimension matrix.
'''
def reshape_data_into_2_dim(data):
    if data.ndim == 1:
        n = data.shape[0]
        data = data.reshape(n, 1)
    return data

'''
Note: Copied from function.py

Get the fingerprints, with feature_name specified, and label_name specified
'''
def extract_feature_and_label(data_pd,
                              feature_name,
                              label_name_list):
    # By default, feature should be fingerprints
    X_data = data_pd[feature_name].tolist()
    X_data = [list(x) for x in X_data]
    X_data = np.array(X_data)

    y_data = data_pd[label_name_list].values.tolist()
    y_data = np.array(y_data)
    y_data = reshape_data_into_2_dim(y_data)

    X_data = X_data.astype(float)
    y_data = y_data.astype(float)

    return X_data, y_data

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--config_json_file', action="store", dest="config_json_file", required=True)
    parser.add_argument('--model_dir', action="store", dest="model_dir", required=True)
    parser.add_argument('--dataset_dir', action="store", dest="dataset_dir", required=True)
    parser.add_argument('--prospective_file', action="store", dest="prospective_file", required=True)
    parser.add_argument('--process_num', action="store", dest="process_num", required=True)
    #####
    given_args = parser.parse_args()
    config_json_file = given_args.config_json_file
    model_dir = given_args.model_dir
    dataset_dir = given_args.dataset_dir
    prospective_file = given_args.prospective_file
    process_num = int(given_args.process_num)
    #####
    config_csv_file = model_dir+'model_config.csv'
    #####
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)

    # set seed according to process_num
    np.random.seed(seed=process_num)

    # specify dataset
    directory = dataset_dir
    file_list = []
    k=5
    for i in range(k):
        file_list.append('file_{}.csv'.format(i))

    # merge training and test dataset
    dtype_list = {'Molecule': np.str,
                  'SMILES': np.str,
                  'Fingerprints': np.str,
                  'Keck_Pria_AS_Retest': np.int64,
                  'Keck_Pria_FP_data': np.int64,
                  'Keck_Pria_Continuous': np.float64,
                  'Keck_RMI_cdd': np.float64}
    output_file_list = [directory + f_ for f_ in file_list]

    with open(config_json_file, 'r') as f:
        conf = json.load(f)

    if not os.path.exists(model_dir+'process_'+str(process_num)):
        os.makedirs(model_dir+'process_'+str(process_num))
    model_file = model_dir+'process_'+str(process_num)+'/rf_clf'

    csv_file_list = output_file_list[:]
    train_pd = read_merged_data(csv_file_list)
    test_pd = pd.read_csv(prospective_file, compression='gzip')

    labels = ["Keck_Pria_AS_Retest"]

    # extract data
    X_train, y_train = extract_feature_and_label(train_pd,
                                                 feature_name='Fingerprints',
                                                 label_name_list=labels)
    X_test, y_test = extract_feature_and_label(test_pd,
                                               feature_name='Fingerprints',
                                               label_name_list=labels)

    # scramble y_train
    ind_permutation = np.random.permutation(y_train.shape[0])
    assert not np.array_equal(y_train, y_train[ind_permutation,:]) # check original labels and permuted labels not the same
    y_train = y_train[ind_permutation,:]
    print('done data preparation')

    task = RFH_YScrambled(conf=conf, process_num=process_num)
    task.train(X_train, y_train, model_file)
    task.save_model_params(config_csv_file)

    # save prediction arrays
    y_train, y_pred_on_train = task.get_prediction_info(X_train, y_train)
    y_test, y_pred_on_test = task.get_prediction_info(X_test, y_test)
    npz_file_name = model_dir+'process_'+str(process_num)
    np.savez_compressed(npz_file_name,
                        labels=labels, y_train=y_train, y_val=None, y_test=y_test,
                        y_pred_on_train=y_pred_on_train, y_pred_on_val=None, y_pred_on_test=y_pred_on_test)


from Logger.PlotLogger import PlotLogger
from Network.LstmAutoencoder import LstmAutoencoder
from TaskConfig.ITaskConfig import ITaskConfig
from Trainers.RAETrainer import RAETrainer
from DataNormalizer.DataNormalizer import DataNormalizer
from DataSeperator.NoSepDataSeperator import NoSepDataSeperator

import torch
import os.path as path

class RAETaskConfig(ITaskConfig):
    def __init__(self, modelFolderPath):
        self.modelFolderPath = modelFolderPath

    def getConfig(self, isCuda = False):
        feature_size = 1
        output_size = 1
        logger = PlotLogger()
        mlModel = LstmAutoencoder(feature_size,4,output_size,2)
        taskName = 'RegularTask'
        try:
            mlModel.load_state_dict(torch.load(path.join(self.modelFolderPath, taskName + ".pt")))
        except:
            pass
        if torch.cuda.is_available():
            mlModel.cuda()
        trainer = RAETrainer(mlModel, logger)
        datasetSeperator = NoSepDataSeperator()
        dataNormalizer = DataNormalizer()

        return mlModel, datasetSeperator, trainer, logger, dataNormalizer, taskName


import re
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

f = open('Nodes.txt')
lines = f.readlines()
i = 1
points = []
for line in lines:
    if i==1:
        R = float(line)
        i = i+1
    elif i==2:
        N = int(line)
        i = i+1
    else:
        nums = re.findall(r"\-?\d+\.?\d*",line)
        points.append([float(num) for num in nums])
        i = i+1
pp = np.array(points)
print(np.shape(pp))
# ax = plt.subplot(111,projection = '3d')
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(pp[:,0],pp[:,1],pp[:,2])
plt.show()


from typing import Optional, List, Dict

from fastapi import Request, FastAPI
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from .dense_classifier import predict_emotions_with_dense_model
from .embedding import get_embedding_for_list_of_texts
from .seq_classifier import predict_emotions_with_seq_model
from .conv_classifier import predict_emotions_with_conv_model
from copy import deepcopy

from pydantic import BaseModel

class Item(BaseModel):
    tweets: List[Dict[str, str]] = []

app = FastAPI()

origins = [
    "*",
    "https://smapwr.github.io",
    "http://127.0.0.1",
    "http://localhost",
    "http://localhost:3000",
    "http://localhost:3001",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def read_root():
    return RedirectResponse("/docs")

@app.get("/status")
def read_root():
    return {"status": True}

@app.post("/predictions/")
async def get_predictions(request: Item):
    """
    :param request:
        [
            {
                "text": str,
                "id": str
            }
        ]
    :return: {
        "data": [
            {
                "id": str,
                "text": str,
                "model1": {

                },
                "model2": {

                },
                "model3": {

                }
            }
        ]
    }
    """
    body = request.dict()
    print(body)

    response = {"data": []}

    texts = [item["text"] for item in body["tweets"]]

    (
        seq_embeddings_tensor,
        sentence_embeddings_tensor,
    ) = get_embedding_for_list_of_texts(texts)

    list_of_predicted_emotions_in_dense_model = predict_emotions_with_dense_model(
        sentence_embeddings_tensor
    )

    list_of_predicted_emotions_in_sequence_model = predict_emotions_with_seq_model(
        seq_embeddings_tensor
    )

    list_of_predicted_emotions_in_teacher_student_model = (
        predict_emotions_with_conv_model(sentence_embeddings_tensor)
    )

    for idx, item in enumerate(body["tweets"]):

        response["data"].append(
            {
                "id": item["id"],
                "text": item["text"],
                "model1": list_of_predicted_emotions_in_dense_model[idx],
                "model2": list_of_predicted_emotions_in_sequence_model[idx],
                "model3": list_of_predicted_emotions_in_teacher_student_model[idx],
            }
        )

    return response


from pathlib import Path
from contextlib import contextmanager
from functools import wraps
import csv
import pickle
import logging

@contextmanager
def dataset(name):
    path = Path(__file__) \
        .parent \
        .parent \
        .joinpath('datasets') \
        .joinpath('{}.csv'.format(name))

    print(path)
    with path.open() as f:
        yield csv.DictReader(f)

def cached(name):
    def decorator(fn):
        @wraps(fn)
        def wrapper():
            path = Path(__file__) \
                .parent \
                .parent \
                .joinpath('cached') \
                .joinpath('{}.pickle'.format(name))

            try:
                with path.open('rb') as f:
                    return pickle.load(f)
            except Exception as e:
                logging.info("Can't load cached object %s: %s", name, e)
                val = fn()

                with path.open('wb') as f:
                    pickle.dump(val, f)

                return val

        return wrapper

    return decorator


import yaml

def main():
    import argparse

    parser = argparse.ArgumentParser(description="Diff the MASTG test cases covered.")
    parser.add_argument("-o", "--old", required=True)
    parser.add_argument("-n", "--new", required=True)

    args = parser.parse_args()

    MASVS_OLD = yaml.safe_load(open(args.old))
    MASVS_NEW = yaml.safe_load(open(args.new))

    updated = 0
    added = 0
    removed = 0

    print("OWASP MAS Checklists Changes")

    for mstg_id, req in MASVS_NEW.items():
        old_links = MASVS_OLD[mstg_id].get("links")
        new_links = req.get("links")

        if old_links and new_links:
            diff = list(set(new_links) - set(old_links))
            updated += 1
            if diff:
                print(f"- [UPDATED] {mstg_id}:")
                for link in diff:
                    print(f"  - {link}")
                print("\n")
        elif old_links is None and new_links:
            added += 1
            print(f"- [ADDED] {mstg_id}:")
            for link in new_links:
                print(f"  - {link}")
            print("\n")
        elif old_links and new_links is None:
            removed += 1
            print(f"- [REMOVED] {mstg_id}\n")

    print(f"\nSUMMARY: removed ({removed}) added ({added}) updated ({updated})")

if __name__ == "__main__":
    main()


from __future__ import print_function, division

import odrive
from odrive.enums import *
import time
import math
import numpy as np
import readchar
import sys
testing = False # testing variable
command = ["Stopped", 0]
incrament = 0.5

config = {
	"wheel_radius" : 0.1,
	"drive_gearing" : 28.55, # 64 means a gearing of 64 to one
	"flipper_gearing" : 81.37
}

serial_list_raw = ["209137933548", "336631563536"]

odrv = None

############################################
#### CALIBRATION PROCESS ###################
############################################

# Calibrate one odrive
# Input: Serial id of the odrive as a string
# Output: ODrive object
def full_reset_and_calibrate(targetID, i=0):
	"""Completely resets the Odrive, calibrates axis0 and configures axis0 to only encoder index search on startup and be ready in AXIS_STATE_CLOSED_LOOP_CONTROL"""
	print("------------- CALIBRATION ODRV " + str(i) + " -------------\n")
	print("Searching for Serial number: " + targetID)
	odrv0 = odrive.find_any(serial_number=targetID)

	odrv0.erase_configuration()
	print("ODRV NO." + str(i) + "  " + "Erased [1/7]")
	try: # Reboot causes loss of connection, use try to supress errors
		odrv0.reboot()
	except:
		pass
	print("ODRV NO." + str(i) + "  " + "Rebooted [2/7]")
	odrv0 = odrive.find_any(serial_number = targetID) # Reconnect to the Odrive
	print("ODRV NO." + str(i) + "  " + "Connected [3/7]")
	odrv0.axis0.motor.config.pre_calibrated = True # Set all the flags required for pre calibration
	odrv0.axis0.encoder.config.pre_calibrated = True
	odrv0.axis0.encoder.config.use_index = True
	odrv0.axis0.config.startup_encoder_index_search = True # Change startup sequence
	odrv0.axis0.config.startup_closed_loop_control = True
	odrv0.axis0.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE # Calibrate
	print("ODRV NO." + str(i) + "  " + "Started calibration 1 [4/7]", end="")
	while odrv0.axis0.current_state != AXIS_STATE_IDLE: # Wait for calibration to be done
		time.sleep(0.1)
		print(".", end="")
	odrv0.save_configuration()

	print("\nODRV NO." + str(i) + "  " + "AXIS 1. Calibration complete [5/7]\n")
	print("now will begin calibration sequence for second axis for ODRV NO." + str(i))
	time.sleep(3)
	odrv0.axis1.motor.config.pre_calibrated = True # Set all the flags required for pre calibration
	odrv0.axis1.encoder.config.pre_calibrated = True
	odrv0.axis1.encoder.config.use_index = True
	odrv0.axis1.config.startup_encoder_index_search = True # Change startup sequence
	odrv0.axis1.config.startup_closed_loop_control = True
	odrv0.axis1.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE # Calibrate
	print("ODRV NO." + str(i) + "  " + "Started calibration 2 [6/7]", end="")
	while odrv0.axis1.current_state != AXIS_STATE_IDLE: # Wait for calibration to be done
		time.sleep(0.5)
		print(".", end="")

	print("\nODRV NO." + str(i) + "  " + "AXIS 2. Calibration 2 complete [7/7]")

	#closed loop control for both axis
	odrv0.save_configuration()
	odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
	odrv0.axis1.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL

	return odrv0

# Calibrate all odrives.
# Input: Serial ids of the odrives as a list of strings
# Output: List of all ODrive objects
def calibrateODrives(serialIDs):
	odrives = []
	for i, ID in enumerate(serialIDs):
		odrives.append(full_reset_and_calibrate(ID, i+1))

	return odrives

def obtainODriveObjects(serialIDs):
	print("No calibration Mode")
	odrives = []
	for i, ID in enumerate(serialIDs):
		print("Searching for ODrive with id = " + ID)
		odrives.append(odrive.find_any(serial_number=ID))
		print("Found an ODrive")

	return odrives

############################################
#### ODRIVE CONTROL COMMANDS ###############
############################################

# Set rps of the output shaft
# Input: axis as ODrive object, desired rps of the output, gear ratio of output to motor
def set_rps(axis, rps, gear_ratio):
	axis.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
	axis.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL
	axis.controller.vel_setpoint = rps * 2 * math.pi * gear_ratio * 8192

# Set current of a particular axis
# Input: axis as ODrive object, max current through that axis
def set_current_limit(axis, current_limit):
	axis.motor.config.current_lim = current_limit

# Set current of a particular axis
# Input: axis as ODrive object, max current through that axis
def set_velocity_limit(axis, velocity_limit):
	axis.controller.config.vel_limit = velocity_limit

############################################
#### PROGRAM NAVIGATION ####################
############################################

def initial_setting(debug = False):
	global odrv

	if not debug:
		print("-------------- ODRIVE SANITY CHECK --------------\n\n")
		print("Full Reset and Calibrate? (y/n)")

		while 1:
			value = input()
			if value == 'y':
				odrv = calibrateODrives(serial_list_raw)
				break
			elif value == 'n':
				odrv = obtainODriveObjects(serial_list_raw)
				break
			else:
				print("Enter either y or n to continue")

def control_loop(debug = False):
	global odrv

	print("Control Loop: Press Enter to Begin")
	input()

	previous = 0
	speed = 0
	incrament = 0.0002
	threshold = incrament * 10

	max_velocity = 500000

	if not debug:
		set_velocity_limit(odrv[0].axis0, max_velocity)
		set_velocity_limit(odrv[0].axis1, max_velocity)
		set_velocity_limit(odrv[1].axis0, max_velocity)
		set_velocity_limit(odrv[1].axis1, max_velocity)

		rightTrack = odrv[0].axis1
		leftTrack = odrv[0].axis0
		frontFlipper = odrv[1].axis0
		rearFlipper = odrv[1].axis1

	mode = 0

	select = ["Move FWD", "Move Left Track", "Move Right Track", "Move Front Flipper", "Move Rear Flipper"]
	while 1:

		input_val = readchar.readchar()

		if input_val == "1":
			mode = 1
			print("--  MODE : " + select[mode - 1])
		elif input_val == "2":
			mode = 2
			print("--  MODE : " + select[mode - 1])
		elif input_val == "3":
			mode = 3
			print("--  MODE : " + select[mode - 1])
		elif input_val == "4":
			mode = 4
			print("--  MODE : " + select[mode - 1])
		elif input_val == "5":
			mode = 5
			print("--  MODE : " + select[mode - 1])

		elif input_val == "w":
			speed += incrament
			print("speed INCREASE")
		elif input_val == "s":
			speed -= incrament
			print("speed DECREASE")
		elif input_val == "0":
			speed = 0
			print("speed ZERO")
		elif input_val == "q":
			break
		else:
			print("No command parsed")

		current = speed

		if current < 0:
			current = 0

		if abs(previous - current) > threshold:
			current = previous + np.sign(current - previous)*threshold

		if not debug:

			if mode == 1:
				set_rps(rightTrack, -1* current, config['drive_gearing'])
				set_rps(leftTrack, current, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 2:
				set_rps(rightTrack, current, config['drive_gearing'])
				set_rps(leftTrack, 0, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 3:
				set_rps(rightTrack, 0, config['drive_gearing'])
				set_rps(leftTrack, current, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 4:
				set_rps(rightTrack, 0, config['drive_gearing'])
				set_rps(leftTrack, 0, config['drive_gearing'])
				set_rps(frontFlipper, current, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 5:
				set_rps(rightTrack, 0, config['drive_gearing'])
				set_rps(leftTrack, 0, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, current, config['flipper_gearing'])

		print("speed is: " + str(current))

		previous = current

# MAIN PROGRAM
def main():
	initial_setting(debug = False)
	control_loop(debug = False)

if __name__ == "__main__":
	main()


from django.db import models
from django.urls import reverse
from django.conf import settings

class Analysis(models.Model):
    CONCENTRATION_UNIT_CHOICES = [
        ("mg/l", "mg/l"),
        ("g/l", "g/l"),
        ("mg/ml", "mg/ml"),
        ("mol/l", "mol/l"),
    ]

    analysis_name = models.CharField(
        "Nome da Análise",
        max_length=255,
        help_text="Digite um nome para identificar sua análise.",
    )
    substance_name = models.CharField(
        "Nome da Substância",
        max_length=255,
        help_text="Digite o nome da substância que você está analisando.",
    )
    absorbance = models.CharField(
        "Absorbâncias",
        max_length=255,
        help_text="Valores das absorbâncias lidas separados por vírgula.",
    )
    concentration = models.CharField(
        "Concentrações",
        max_length=255,
        help_text="Valores das concentrações para cada absorbância fornecida separados por vírgulas.",
    )

    concentration_units = models.CharField(
        "Unidades das Concentrações",
        max_length=255,
        help_text="Valores das unidades das concentrações.",
        choices=CONCENTRATION_UNIT_CHOICES,
        default="mg/l",
    )

    analyst = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        default=1,
        verbose_name="Analista",
    )

    created = models.DateTimeField("Data de Criação", auto_now_add=True, blank=True)

    updated = models.DateTimeField("Data de Atualização", auto_now=True, blank=True)

    class Meta:
        verbose_name_plural = "Analyses"

    def get_absolute_url(self):
        return reverse("analysis-detail", args=[str(self.id)])

    def __str__(self):
        return f"{self.analysis_name} -> {self.substance_name} -> {self.id}"


import os
import sys
import subprocess
from Bio import SeqIO
import multiprocessing

external_wrappers_dir = '/'.join(
    os.path.dirname(os.path.realpath(__file__)).split('/')[:-1] + ['external_wrappers']) + '/'

def run_nanopolish_variants(data):
    workspace, window, cmd = data
    stdout_file_1 = workspace + window.replace(':', '_') + '.log'
    stderr_file_1 = workspace + window.replace(':', '_') + '.err'
    stdout_handle_1 = open(stdout_file_1, 'w')
    stderr_handle_1 = open(stderr_file_1, 'w')
    proc = subprocess.Popen(cmd, stdout=stdout_handle_1, stderr=stderr_handle_1)
    proc.wait()
    stdout_handle_1.close();
    stderr_handle_1.close();

class Assembly():
    def __init__(self, assembly_fasta, sample_name, logObject, contig_fasta=None, assembly_graph=None):
        self.assembly_fasta = os.path.abspath(assembly_fasta)
        self.sample_name = sample_name
        self.logFile = logObject
        self.contig_fasta = contig_fasta
        self.assembly_graph = assembly_graph

    def filter_contigs_by_size(self, working_dir, size_filter=0, change_reference=True):
        """ Filter assembly contigs by size threshold. """
        try:
            working_dir = os.path.abspath(working_dir) + '/'
            assert(os.path.isdir(working_dir))
            self.logFile.info('-'*70)
            self.logFile.info("Filtering assembly %s for contigs shorter than %d." % (self.assembly_fasta, size_filter))
            resulting_assembly = working_dir + self.sample_name + '.filtered.fasta'
            assembly_handle = open(resulting_assembly, 'w')
            with open(self.assembly_fasta) as oaf:
                for rec in SeqIO.parse(oaf, 'fasta'):
                    if len(str(rec.seq)) >= size_filter:
                        assembly_handle.write('>' + rec.description + '\n' + str(rec.seq) + '\n')
            assembly_handle.close()
            assert(os.path.isfile(resulting_assembly))
            self.logFile.info("Successfully filtered assembly. Resulting assembly file can be found at: %s" % resulting_assembly)
            if change_reference:
                self.assembly_fasta = resulting_assembly
                self.assembly_graph = None
                self.contig_fasta = None
            return resulting_assembly
        except RuntimeError as e:
            self.logFile.error("Something went wrong when filtering assembly for short contigs. Raising exception ..." % e)
            raise RuntimeError

    def detect_adapters(self, working_dir, options='-r -e -f Directive -v --no_protection --adaptorsdb', cores=1):
        """ Use GAEMR to detect adapters in assembly. Returns commands for guinan analysis and adapter removal. """
        try:
            working_dir = os.path.abspath(working_dir) + '/'
            assert(os.path.isdir(working_dir) and os.path.isfile(self.contig_fasta))
            gaemr_version_info = open(external_wrappers_dir + "run_contamination_screen.txt").readlines()[0]
            self.logFile.info('-'*70)
            self.logFile.info("Screening for adapters with GAEMR version %s" % gaemr_version_info)
            result_txt = working_dir + self.sample_name + '.screen.out'
            gaemr_cmd = ['/bin/bash', external_wrappers_dir + 'run_contamination_screen.sh', '-t', str(cores), options, '-o',
                        result_txt, '-i', self.contig_fasta]
            self.logFile.info("Executing the command: %s" % ' '.join(gaemr_cmd))
            stdout_file = working_dir + self.sample_name + ".contamination_screen.log"
            stderr_file = working_dir + self.sample_name + ".contamination_screen.err"
            stdout_handle = open(result_txt, 'w'); stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(gaemr_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close(); stderr_handle.close()
            commands_result_file = working_dir + self.sample_name + '.screen.out.directives.txt'
            assert(os.path.isfile(result_txt) and os.path.isfile(commands_result_file))
            self.logFile.info("GAEMR adapter detection finished successfully!")
            return commands_result_file
        except RuntimeError as e:
            self.logFile.error("GAEMR adapter detection was not able to be run properly! Error message: %s. Raising exception ..." % e)
            raise RuntimeError

    def remove_adapters(self, command_file, working_dir, options='--sort_by_length --rename', change_reference=True):
        #  Use guinan suite to remove adapters in assembly.
        try:
            working_dir = os.path.abspath(working_dir) + '/'
            assert(os.path.isdir(working_dir) and os.path.isfile(command_file) and os.path.isfile(self.contig_fasta) and os.path.isfile(self.assembly_graph))
            guinan_version_info = open(external_wrappers_dir + "modify_assembly.txt").readlines()[0]
            self.logFile.info('-'*70)
            self.logFile.info("Removing adapters with guinan version %s" % guinan_version_info)
            result_header = working_dir +  self.sample_name + '.adapter-free'
            guinan_cmd = ['/bin/bash', external_wrappers_dir + 'modify_assembly.sh', options, '--agp', self.assembly_graph,
                          '--command_file', command_file, '--output_header', result_header, self.contig_fasta]
            self.logFile.info("Executing the command: %s" % ' '.join(guinan_cmd))
            stdout_file = working_dir + self.sample_name + ".contamination_screen.log"
            stderr_file = working_dir + self.sample_name + ".contamination_screen.err"
            stdout_handle = open(stdout_file, 'w'); stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(guinan_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close(); stderr_handle.close()
            # TODO: improve checksafe
            result_file = result_header + '.scaffolds.fasta'
            assert(os.path.isfile(result_file))
            self.logFile.info("Successfully filtered out any adapters with guianan, filtered assembly can be found at: %s" % result_file)
            if change_reference:
                self.assembly_fasta = result_file
                self.assembly_graph = None
                self.contig_fasta = None
            self.logFile.info("guinan adapter removal finished successfully!")
            return result_file
        except RuntimeError as e:
            self.logFile.error("guinan adapter removal was not able to be run properly! Error message: %s. Raising exception ..." % e)
            raise RuntimeError

    def run_mlst(self, working_dir):
        """ Performing mlst on assembly. """
        try:
            working_dir = os.path.abspath(working_dir) + '/'
            assert(os.path.isdir(working_dir))
            mlst_version_info = open(external_wrappers_dir + "mlst.txt").readlines()[0]
            self.logFile.info('-'*70)
            self.logFile.info("Performing MLST of %s" % mlst_version_info)
            result_txt = working_dir + self.sample_name + '.txt'
            mlst_cmd = ['/bin/bash', external_wrappers_dir + 'mlst.sh', self.assembly_fasta]
            self.logFile.info("Executing the command: %s" % ' '.join(mlst_cmd))
            stderr_file = working_dir + self.sample_name + ".mlst.err"
            stdout_handle = open(result_txt, 'w'); stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(mlst_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close(); stderr_handle.close()
            # TODO: improve checksafe
            assert(os.path.isfile(result_txt) and os.path.getsize(result_txt) > 100)
            self.logFile.info("mlst finished successfully!")
        except RuntimeError as e:
            self.logFile.error("mlst was not able to be run properly! Error message: %s. Raising exception ..." % e)
            raise RuntimeError

    def run_gaemr_formatter(self, working_dir, options='-g 1 -c 100 -r', reference_change=True):
        try:
            assert (os.path.isfile(self.assembly_fasta))
            workspace = os.path.abspath(working_dir) + '/'

            gaemr_version = open(external_wrappers_dir + 'make_standard_assembly_files.txt').readlines()[0]
            self.logFile.info("-" * 70)
            self.logFile.info("Formatting assemblies with GAEMR for QC and Vesper2, using %s" % gaemr_version)

            output_results = workspace + '/' + self.sample_name

            gaemr_cmd = ["sh", external_wrappers_dir + "make_standard_assembly_files.sh", options, '-S', self.assembly_fasta,
                         '-o', output_results]

            self.logFile.info("Executing the command: %s" % ' '.join(gaemr_cmd))
            stdout_file = workspace + "gaemr_formatting.log"
            stderr_file = workspace + "gaemr_formatting.err"
            stdout_handle = open(stdout_file, 'w')
            stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(gaemr_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close();
            stderr_handle.close()

            resulting_graph = output_results + '.agp'
            resulting_contig = output_results + '.contigs.fasta'
            resulting_scaffold = output_results + '.scaffolds.fasta'

            assert (os.path.isfile(resulting_graph) and os.path.isfile(resulting_contig) and os.path.isfile(resulting_scaffold))
            self.logFile.info("Results are available at %s" % workspace)

            if reference_change:
                self.assembly_fasta = resulting_scaffold
                self.contig_fasta = resulting_contig
                self.assembly_graph = resulting_graph
            return ([resulting_scaffold, resulting_contig, resulting_graph])
        except RuntimeError as e:
            self.logFile.error("Unable to run GAEMR formatting of assembly for QC. Raising exception ..." % e)
            raise RuntimeError

    def run_gaemr_qc(self, read_list, working_dir, options='--force --analyze_rna', cores=1):
        try:
            assert (os.path.isfile(self.assembly_fasta) and os.path.isfile(self.contig_fasta) and os.path.isfile(self.assembly_graph))
            workspace = os.path.abspath(working_dir) + '/'

            gaemr_version = open(external_wrappers_dir + 'gaemr.txt').readlines()[0]
            self.logFile.info("-" * 70)
            self.logFile.info("Running GAEMR for QC, using %s" % gaemr_version)

            os.chdir(workspace)

            gaemr_cmd = ["sh", external_wrappers_dir + "gaemr.sh", options, '-t', str(cores), '-a',
                         self.assembly_graph, '-c', self.contig_fasta, '-s', self.assembly_fasta, '--read_list=' + read_list]

            self.logFile.info("Executing the command: %s" % ' '.join(gaemr_cmd))
            stdout_file = workspace + "gaemr_qc.log"
            stderr_file = workspace + "gaemr_qc.err"
            stdout_handle = open(stdout_file, 'w')
            stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(gaemr_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close();
            stderr_handle.close()

            # Check GAEMR QC ran successfully
            assert(os.path.isdir(workspace + '/gaemr/'))
            result_files = ['assembly.basic_assembly_stats.table.txt', 'assembly.blast_hit_taxonomy.table.txt',
                            'assembly.contig_detail.table.txt', 'assembly.gap_analysis.table.txt',
                            'assembly.gap_ss_analysis.table.txt', 'assembly.scaffold_detail.table.txt',
                            'assembly.scaffolds.bam_phys_cvg_stats.table.txt',
                            'assembly.scaffolds.bam_seq_cvg_stats.table.txt',
                            'assembly.scaffolds.simple_bam_stats.table.txt', 'fragment.scaffolds.insert_size.table.txt']
            for f in result_files:
                fpath = workspace + '/gaemr/table/' + f
                assert(fpath)
                with open(fpath) as ofp:
                    file_line_count = len(ofp.readlines())
                    assert(file_line_count > 2)

            self.logFile.info("Results are available at %s" % workspace + '/gaemr/')
            return
        except RuntimeError as e:
            self.logFile.error("Unable to run GAEMR for QC. Raising exception ..." % e)
            raise RuntimeError

    def run_gaemr_qc_ont(self, read_list, working_dir, options='--force --analyze_rna', cores=1):
        try:
            assert (os.path.isfile(self.assembly_fasta) and os.path.isfile(self.contig_fasta) and os.path.isfile(self.assembly_graph))
            workspace = os.path.abspath(working_dir) + '/'

            gaemr_version = open(external_wrappers_dir + 'gaemr_ont.txt').readlines()[0]
            self.logFile.info("-" * 70)
            self.logFile.info("Running GAEMR for QC, using %s" % gaemr_version)

            os.chdir(workspace)

            gaemr_cmd = ["sh", external_wrappers_dir + "gaemr_ont.sh", options, '-t', str(cores), '-a',
                         self.assembly_graph, '-c', self.contig_fasta, '-s', self.assembly_fasta, '--read_list=' + read_list]

            self.logFile.info("Executing the command: %s" % ' '.join(gaemr_cmd))
            stdout_file = workspace + "gaemr_qc.log"
            stderr_file = workspace + "gaemr_qc.err"
            stdout_handle = open(stdout_file, 'w')
            stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(gaemr_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close();
            stderr_handle.close()

            # Check GAEMR QC ran successfully
            assert(os.path.isdir(workspace + '/gaemr/'))
            result_files = ['assembly.basic_assembly_stats.table.txt', 'assembly.blast_hit_taxonomy.table.txt',
                            'assembly.contig_detail.table.txt', 'assembly.gap_analysis.table.txt',
                            'assembly.gap_ss_analysis.table.txt', 'assembly.scaffold_detail.table.txt',
                            'assembly.scaffolds.bam_phys_cvg_stats.table.txt',
                            'assembly.scaffolds.bam_seq_cvg_stats.table.txt',
                            'assembly.scaffolds.simple_bam_stats.table.txt', 'fragment.scaffolds.insert_size.table.txt']
            for f in result_files:
                fpath = workspace + '/gaemr/table/' + f
                assert(fpath)
                with open(fpath) as ofp:
                    file_line_count = len(ofp.readlines())
                    assert(file_line_count > 2)

            self.logFile.info("Results are available at %s" % workspace + '/gaemr/')
            return
        except RuntimeError as e:
            self.logFile.error("Unable to run GAEMR for QC. Raising exception ..." % e)
            raise RuntimeError

    def run_pilon(self, working_dir, nanopore_bam=None, illumina_bam=None, options='', cores=1, reference_change=False):
        try:
            assert (os.path.isfile(illumina_bam) or os.path.isfile(nanopore_bam))
            workspace = os.path.abspath(working_dir) + '/'
            assert (os.path.isdir(working_dir))
            pilon_version = open(external_wrappers_dir + 'pilon.txt').readlines()[0]
            self.logFile.info("-" * 70)
            self.logFile.info("Running pilon for illumina and/or nanopore to refine assembly, using version %s" % pilon_version)

            result_assembly = workspace + self.sample_name + '.fasta'
            result_changes = workspace + self.sample_name + '.changes'
            pilon_cmd = ["/bin/bash", external_wrappers_dir + "pilon.sh", options, '--genome', self.assembly_fasta, '--changes',
                         '--threads', str(cores), '--output', self.sample_name, '--outdir', workspace]
            if nanopore_bam:
                pilon_cmd += ['--nanopore', nanopore_bam]
            if illumina_bam:
                pilon_cmd += ['--frags', illumina_bam]

            self.logFile.info("Executing the command: %s" % ' '.join(pilon_cmd))
            stdout_file = workspace + 'pilon.log'
            stderr_file = workspace + "pilon.err"
            stdout_handle = open(stdout_file, 'w')
            stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(pilon_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close();
            stderr_handle.close()

            assert (os.path.isfile(result_assembly))
            self.logFile.info("Results are available at %s" % result_assembly)

            if reference_change:
                self.assembly_fasta = result_assembly

            return [result_assembly, result_changes]
        except RuntimeError as e:
            self.logFile.error("Unable to run Pilon. Raising exception ..." % e)
            raise RuntimeError

    def run_nanopolish(self, nanopore_fastq, nanopore_bam, working_dir, nanopolish_variants_options='', cores=8, reference_change=True):
        try:
            workspace = os.path.abspath(working_dir) + '/'
            assert (os.path.isfile(nanopore_fastq) and os.path.isfile(nanopore_bam) and os.path.isdir(working_dir))

            workspace_vcf_dir = working_dir + 'vcfs/'
            workspace_log_dir = working_dir + 'logs/'

            os.system('mkdir %s' % workspace_vcf_dir)
            os.system('mkdir %s' % workspace_log_dir)
            nanopolish_version = open(external_wrappers_dir + 'nanopolish.txt').readlines()[0]
            self.logFile.info("-" * 70)
            self.logFile.info("Running nanopolish to refine assembly, using version %s" % nanopolish_version)
            result_assembly = workspace + self.sample_name + '.fasta'

            # run polishing.
            nanopolish_variants_cmd_1 = ["/bin/bash", external_wrappers_dir + "nanopolish_makerange.sh",
                                               self.assembly_fasta]

            self.logFile.info("Executing the command: %s" % ' '.join(nanopolish_variants_cmd_1))
            stdout_file_1 = workspace + 'nanopolish_variants_1.log'
            stderr_file_1 = workspace + 'nanopolish_variants_1.err'
            stdout_handle_1 = open(stdout_file_1, 'w')
            stderr_handle_1 = open(stderr_file_1, 'w')
            proc1 = subprocess.Popen(nanopolish_variants_cmd_1, stdout=stdout_handle_1, stderr=stderr_handle_1)
            proc1.wait()
            stdout_handle_1.close(); stderr_handle_1.close();

            cmds = []
            with open(stdout_file_1) as osf1:
                for line in osf1:
                    line = line.strip()
                    nanopolish_cmd = ['/bin/bash', external_wrappers_dir + "nanopolish.sh", "variants", "--consensus",
                                      nanopolish_variants_options,
                                      '-g', self.assembly_fasta, "-o",
                                      workspace_vcf_dir + 'polished.' + line.replace(':', '_') + '.vcf', '-w', line,
                                      '-r', nanopore_fastq, '-b', nanopore_bam, '-t', '2', '--min-candidate-frequency',
                                      '0.1']
                    self.logFile.info("Will be executing the command: %s" % ' '.join(nanopolish_cmd))
                    cmds.append([workspace_log_dir, line, nanopolish_cmd])

            p = multiprocessing.Pool(int(cores/2))
            try:
                p.map(run_nanopolish_variants, cmds)
            except:
                self.logFile.error("Problem with running nanopolish variants --consensus. Raising exception ...")
                p.close()
                raise RuntimeError
            else:
                p.close()

            vcflist_file = workspace + 'vcfs.list'
            vcflist_handle = open(vcflist_file, 'w')
            for f in os.listdir(workspace_vcf_dir):
                vcflist_handle.write(workspace_vcf_dir + f + '\n')
            vcflist_handle.close()

            # combine results across windows and convert to FASTA format.
            nanopolish_vcf2fasta_cmd = ["/bin/bash", external_wrappers_dir + "nanopolish.sh", "vcf2fasta",
                                        "--skip-checks", '-g', self.assembly_fasta, '-f', vcflist_file]
            self.logFile.info("-" * 70)
            self.logFile.info("Executing the command: %s" % ' '.join(nanopolish_vcf2fasta_cmd))
            stderr_file = workspace + 'nanopolish_vcf2fasta.err'
            stdout_handle = open(result_assembly, 'w')
            stderr_handle = open(stderr_file, 'w')
            proc = subprocess.Popen(nanopolish_vcf2fasta_cmd, stdout=stdout_handle, stderr=stderr_handle)
            proc.wait()
            stdout_handle.close(); stderr_handle.close()

            assert (os.path.isfile(result_assembly) and os.path.getsize(result_assembly) > 100)
            self.logFile.info("Polished assembly available at %s" % result_assembly)

            if reference_change:
                self.assembly_fasta = result_assembly
            return result_assembly
        except RuntimeError as e:
            self.logFile.error("Unable to run Nanopolish. Raising exception ..." % e)
            raise RuntimeError


import sys
from PyQt5.QtWidgets import QMainWindow, QGridLayout, QWidget, QDesktopWidget
from PyQt5.QtWidgets import (QApplication, QPushButton, QLabel)

from PyQt5 import QtGui, QtCore, Qt
from PyQt5.Qt import *
from PyQt5.QtCore import QRect, Qt, QTimer

from PyQt5.QtGui import QPixmap

#Other Win
sys.path.insert(1,'Process-Algorithms/First-Come-First-Serve')
import FCFS
sys.path.insert(1,'Process-Algorithms/Shortest-Remaining-Time-First')
import SRTF
sys.path.insert(1,'Process-Algorithms/Round-Robin')
import RR
sys.path.insert(1,'Process-Algorithms/Non-Preemtive-Priority')
import NPP
sys.path.insert(1,'Process-Algorithms/Preemtive-Priority')
import PP
sys.path.insert(1,'Process-Algorithms/Shortest-Job-First')
import SJF

class processSchedWin(QMainWindow):
    def __init__(self):
        super().__init__()

        self.title = "Simulation of Process Scheduling Algorithm"
        self.width = 1200
        self.height = 950

        self.initWindow()

    def initWindow(self):
        self.resize(self.width, self.height)
        self.setWindowTitle(self.title)

        self.Buttons()
        self.center()

        self.show()

    #move window to center
    def center(self):
        # geometry of the main window
        qr = self.frameGeometry()

        # center point of screen
        cp = QDesktopWidget().availableGeometry().center()

        # move rectangle's center point to screen's center point
        qr.moveCenter(cp)

        # top left of rectangle becomes top left of window centering it
        self.move(qr.topLeft())

    def Buttons(self):
        titleLabel = QLabel("Simulation of Process Scheduling", self)
        titleLabel.setGeometry(QRect(30+150,50, 900, 100))
        titleLabel.setStyleSheet("QWidget { color: Black}")
        titleLabel.setFont(QtGui.QFont('Sanserif', 30, QtGui.QFont.Bold))

        titleLabel = QLabel("Algorithm", self)
        titleLabel.setGeometry(QRect(30+150+290,50+50, 900, 100))
        titleLabel.setStyleSheet("QWidget { color: Black}")
        titleLabel.setFont(QtGui.QFont('Sanserif', 30, QtGui.QFont.Bold))

        FCFSButton = QPushButton('First Come First Serve', self)
        FCFSButton.setGeometry(QRect(180+50,250, 270, 100))
        #FCFSButton.setStyleSheet("QWidget {background-color: Blue}")
        FCFSButton.setFont(QtGui.QFont('Times New Roman',14))
        FCFSButton.clicked.connect(self.clickedFCFS)

        SRTFButton = QPushButton('Shortest Remaining Time First', self)
        SRTFButton.setGeometry(QRect(180+50,250 + 100+ 25, 270, 100))
        SRTFButton.setFont(QtGui.QFont('Times New Roman',12))
        SRTFButton.clicked.connect(self.clickedSRTF)

        RRButton = QPushButton('Round Robin', self)
        RRButton.setGeometry(QRect(180+50,250 + 100+ 25 + 125, 270, 100))
        RRButton.setFont(QtGui.QFont('Times New Roman',14))
        RRButton.clicked.connect(self.clickedRR)

        NPPButton = QPushButton('None Pre-emptive Priority', self)
        NPPButton.setGeometry(QRect(180+500,250, 270, 100))
        NPPButton.setFont(QtGui.QFont('Times New Roman',14))
        NPPButton.clicked.connect(self.clickedNPP)

        PPButton = QPushButton('Pre-emptive Priority', self)
        PPButton.setGeometry(QRect(180+500,250 + 125, 270, 100))
        PPButton.setFont(QtGui.QFont('Times New Roman',14))
        PPButton.clicked.connect(self.clickedPP)

        SJFButton = QPushButton('Shortest Job First', self)
        SJFButton.setGeometry(QRect(180+500,250 + 100+ 25 + 125, 270, 100))
        SJFButton.setFont(QtGui.QFont('Times New Roman',14))
        SJFButton.clicked.connect(self.clickedSJF)

    def clickedFCFS(self):
        self._FCFSWin = FCFS.FCFSWin()
        self._FCFSWin.show()
        self.hide()

    def clickedSRTF(self):
        self._SRTFWin = SRTF.SRTFWin()
        self._SRTFWin.show()
        self.hide()

    def clickedRR(self):
        self._RR_Win = RR.RR_Win()
        self._RR_Win.show()
        self.hide()

    def clickedNPP(self):
        self._NPPWin = NPP.NPPWin()
        self._NPPWin.show()
        self.hide()

    def clickedPP(self):
        self._PPWin = PP.PPWin()
        self._PPWin.show()
        self.hide()

    def clickedSJF(self):
        self._SJFWin = SJF.SJFWin()
        self._SJFWin.show()
        self.hide()

stylesheet = """
    processSchedWin {
        background-image: url(Icons/processes.jpg);
        background-repeat: no-repeat;
        background-position: center;
    }
"""

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyleSheet(stylesheet)
    ex = processSchedWin()
    sys.exit(app.exec_())


import pymongo

__author__ = 'Manos'

import csv
from optparse import OptionParser

from pymongo import MongoClient
from datetime import datetime
import time

mongo_client=MongoClient('mongodb://localhost:27017/')
db=mongo_client.mydb
db_col=db.things

result3a = db.things.find({},{"timestamp": 1} ).sort("timestamp",pymongo.DESCENDING).limit(1)

print("query 3")
for row in result3a:
    print("The last message published on:", end=" ")
    print(row["timestamp"])

result3b = db.things.find({"timestamp":{'$ne':None}},{"timestamp": 1} ).sort("timestamp",pymongo.ASCENDING).limit(1)

for rb in result3b:
    print("The earliest message published on:", end=" ")
    print(rb["timestamp"])

print("query 4")

tmax = row["timestamp"]
dmax = datetime.strptime(tmax, "%Y-%m-%d %H:%M:%S")
secondmax=time.mktime(dmax.timetuple())

tmin = rb["timestamp"]
dmin = datetime.strptime(tmin, "%Y-%m-%d %H:%M:%S")
secondmin=time.mktime(dmin.timetuple())

# print(secondmax)
all_plithos_msg=db.things.find().count()
deltatimemean = ((secondmax - secondmin) / (all_plithos_msg -1))
print("The mean time delta between all messages is :", end=" ")
print(deltatimemean)


from elasticsearch_dsl import (
    Document,
    Object,
    InnerDoc,
    Double,
    Text,
    Keyword,
    Boolean,
    RankFeature
)  # the data types needed
from elasticsearch_dsl.connections import connections

from config.settings import ELASTICSEARCH_DSL

# create a default connection to the host

es_client = connections.create_connection(hosts=ELASTICSEARCH_DSL['default']['hosts'])

class ChannelInnerDoc(InnerDoc):
    id = Keyword(required=True)
    subs = RankFeature()  # you won't really need this.
    lang_code = Keyword()

class VideoInnerDoc(InnerDoc):
    id = Keyword(required=True)
    views = RankFeature()
    # likes = RankFeature()
    # dislikes = RankFeature()
    # like_ratio = RankFeature()
    publish_date_int = RankFeature()
    category = Keyword()  # should be a keyword
    title = Text()  # might come in handy for context2def later
    channel = Object(ChannelInnerDoc)

class CaptionInnerDoc(InnerDoc):
    id = Keyword(required=True)
    is_auto = Boolean()
    lang_code = Keyword()
    video = Object(VideoInnerDoc)

class GeneralDoc(Document):
    # define the fields here
    start = Double()
    duration = Double()
    content = Text()
    prev_id = Keyword()
    next_id = Keyword()
    context = Text()
    caption = Object(CaptionInnerDoc)

    class Index:
        name = "general_idx"
        # using the default settings for now.

    def save(self, **kwargs):
        # do something before save here, if you wish
        return super().save(**kwargs)

class OpenSubDoc(Document):
    # define the fields here
    response = Text()
    # contexts. array of strings. might want to make it searchable..?
    # in elastic search, there is no dedicated "array" data type.
    # every datatype can have one or more values.
    # https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html
    contexts = Text()

    class Index:
        name = "opensub_idx"

    def save(self, **kwargs):
        # do something before saving here, if you wish.
        return super().save(**kwargs)


import requests
from unittest import TestCase
from GameDesignerAI import GameDesignerAI

base_url='http://localhost:8003'

class GameDesignerAIAPITests(TestCase):

    def test_extract(self):
        pass

    def test_get_conversation(self):
        print('---test_get_conversation---')
        response = requests.get(f"{base_url}/get_conversation", json={'world_name':'test_world_name'})
        if response.status_code == 200:
            print(response.json())
            print('Conversation:\n', response.json()['conversation'])
        else:
            print(f"Request failed with status {response.status_code}")

    def test_message_game_designer_ai_and_get_response(self):
        print('---test_message_game_designer_ai_and_get_response---')
        request_payload={
            'world_name':'test_world_name',
            'user_message':'are there any characters already made?'
        }
        response = requests.post(f"{base_url}/message_game_designer_ai_and_get_response", json=request_payload)
        if response.status_code == 200:
            print(response.json())
            print('Response:\n', response.json()['response'])
        else:
            print(f"Request failed with status {response.status_code}")

tc = GameDesignerAIAPITests()
# tc.test_get_conversation()
tc.test_message_game_designer_ai_and_get_response()


from multiprocessing import Process

def func1(counter: int):
    print("start func 1")
    for i in range(100):
        counter += 1
        print("func 1", counter)
    print("end func 1")
    return counter

def func2(counter: int):
    print("start func 2")
    for i in range(100):
        counter += 1
        print("func 2", counter)
    print("end func 2")
    return counter

if __name__ == "__main__":
    counter1 = 0
    counter2 = 0
    p1 = Process(target = func1, args=(counter1,))
    p2 = Process(target = func2, kwargs={"counter":counter2})
    p1.start()
    p2.start()
    p1.join()
    p2.join()


###Q4 final
# sc.stop()
from pyspark import SparkContext
sc = SparkContext(appName="exercise")
def max_temperature(a,b):
    if a>=b:
        return a
    else:
        return b

precipitation_file=sc.textFile("precipitation-readings-tiny.csv")
linesp = precipitation_file.map(lambda line:line.split(";"))

temperature_file=sc.textFile("temperature-readings-tiny.csv")
linest = temperature_file.map(lambda line:line.split(";"))

temperatures = linest.map(lambda x:((x[0],x[1][0:4]),float(x[3])))
precipitations = linesp.map(lambda x:((x[0],x[1][0:4]),float(x[3])))

max_temperatures = temperatures.reduceByKey(max_temperature)  # for each year
max_precipitations = precipitations.reduceByKey(max_temperature)
# print(*max_precipitations.take(10), sep="\n")

station = max_temperatures.join(max_precipitations)
# print(*station.take(10), sep="\n")

station = station.filter(lambda x: x[1][0]>=25 and x[1][0]<=30 and x[1][1]>=100 and x[1][1]<=200)
station = station.map(lambda x:(x[0],x[1][0],x[1][1]))
# print(*station.take(10), sep="\n")

station.saveAsTextFile("station")


from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
import urllib.request
from .scrapper import text_from_html
from .models import Literature

def html_view(request):
    if request.method == 'POST':
        url = request.POST.get('url')
        literature = Literature.objects.filter(url=url).first()
        if literature:
            return JsonResponse({"html": literature.data})
        else:
            html = urllib.request.urlopen(url).read()
            html = text_from_html(html)
            literature = Literature(url=url, data=html)
            literature.save()
            return JsonResponse({"html": html})
    return render(request, 'home/index.html')


#!/usr/bin/env python3 -tt
import sys
import os
from subprocess import Popen, PIPE
from time import sleep
import argparse

# Bin Paths
ssh_bin = "/usr/bin/ssh"
svnsync_bin = "/usr/local/bin/svnsync"
svnadmin_bin = "/usr/local/bin/svnadmin"
svnlook_bin = "/usr/local/bin/svnlook"

# SVN HTTP client (this MUST be set to 'serf' if using svn 1.8 or later)
# However, the 'svnsync sync' command seems to have issues communicating with SCM Manager
# using the 'serf' client.
svn_http_client = 'neon'

# Global Vars
server_name = "server.domain.tld"
user_name = "username"
pass_word = r"password"
ssh_user_name = "ssh_username"
local_repo_directory = "/repositories/svn/"
__verbose = False

# Functions
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Display more verbose output')
    parser.set_defaults(verbose=False)
    args = parser.parse_args()
    # Set verbose flag
    global __verbose
    __verbose = args.verbose

def check_paths():
    if not os.path.exists(ssh_bin):
        return "Can't find SSH client at {}".format(ssh_bin)
    if not os.path.exists(svnsync_bin):
        return "Can't find svnsync binary at {}".format(svnsync_bin)
    if not os.path.exists(svnadmin_bin):
        return "Can't find svnadmin binary at {}".format(svnadmin_bin)
    if not os.path.exists(svnlook_bin):
        return "Can't find svnlook binary at {}".format(svnlook_bin)
    if not os.path.isdir(local_repo_directory):
        return "Can't find local repository directory at {}".format(local_repo_directory)
    return None

def process_output(file):
    output = file.read().split()
    for x in range(0, len(output)):
        output[x] = output[x].decode()
    return output

def append_to_list(original_list, list_to_append):
    for i in list_to_append:
        original_list.append(i)

def get_remote_dir_names():
    p = Popen([ssh_bin,
               '{}@{}'.format(
                   ssh_user_name,
                   server_name
               ),
               'find',
               '/var/lib/scm/repositories/svn/',
               '-maxdepth', '1',
               '-type', 'd',
               '-exec', 'basename {} \;'], stdout=PIPE, stderr=PIPE)
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Failed to list remote directories with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return None
    elif __verbose:
        print(' '.join(output))
    return output

def sync_repo(path):
    # Sync existing mirror with new changes
    p = Popen([svnsync_bin,
               'sync',
               '--username',
               user_name,
               '--password',
               pass_word,
               '--config-option=servers:global:http-library={}'.format(svn_http_client),
               'file://{}'.format(path)], stdout=PIPE, stderr=PIPE)
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnsync sync failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    elif __verbose:
        print(' '.join(output))
    return True

def create_sync_repo(path, url):
    # Create new repository
    p = Popen([svnadmin_bin,
               'create',
               path], stdout=PIPE, stderr=PIPE)
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnadmin create failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    if __verbose:
        print(' '.join(output))
    revprop_path = None
    # Prep for svnsync
    try:
        # Make sure pre-revprop-change has content
        revprop_path = "{}/hooks/pre-revprop-change".format(path)
        with open(revprop_path, 'w') as out:
            out.write("#!/bin/sh")
        p = Popen(['chmod',
                   '755',
                   revprop_path], stdout=PIPE, stderr=PIPE)
        # Safely process the output
        output = []
        error = []
        while p.poll() is None:
            sleep(1)
            append_to_list(output, process_output(p.stdout))
            append_to_list(error, process_output(p.stderr))
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
        if not p.returncode == 0:
            print("chmod 755 failed with return code {}".format(p.returncode), file=sys.stderr)
            print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
            print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
            return False
        elif __verbose:
            print(' '.join(output))
    except IOError as err:
        print("An error occurred while writing to {} with error code {}: {}".format(
            revprop_path, err.errno, err.output), file=sys.stderr)
        return False
    # Now we're ready for svnsync init
    p = Popen([svnsync_bin,
               'init',
               '--username',
               user_name,
               '--password',
               pass_word,
               'file://{}'.format(path),
               url], stdout=PIPE, stderr=PIPE)
    # Safely process the output
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not (p.returncode == 0 or p.returncode == 1):
        print("Svnsync init failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    elif __verbose:
        print(p.stdout.readlines())
    p = Popen([svnlook_bin,
               'pg',
               '--revprop',
               '-r0',
               path,
               r'svn:sync-from-uuid'], stdout=PIPE, stderr=PIPE)
    # Safely process the output
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnlook pg failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    # Don't worry about displaying the output of svnlook pg, only contains a uuid if it succeeds
    # Speaking of which, set uuid to the output
    uuid = ''.join(output)
    p = Popen([svnadmin_bin,
               'setuuid',
               path,
               uuid], stdout=PIPE, stderr=PIPE)
    # Safely process the output
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnadmin setuuid failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    elif __verbose:
        print(' '.join(output))
    return True

# Main Function
def main():
    print("Enumerating directories from {}".format(server_name))
    names = get_remote_dir_names()
    if names is None:
        sys.exit(-2)
    names = [n for n in names if not n.startswith('svn')]
    print("Directory listing from {} succeeded!".format(server_name))
    for n in names:
        print()
        n_path = local_repo_directory + n + '/'
        url = "https://{}/scm/svn/{}".format(
            server_name,
            n
        )
        if os.path.isdir(n_path):
            print("Synchronizing {}...".format(n))
            print("Remote URL is {}. Starting svnsync sync...".format(url))
            ret = sync_repo(n_path)
            if not ret:
                print("Project {} failed to sync.".format(n))
                continue
        else:
            print("First time synchronization on new project {}".format(n))
            print(("Remote URL is {}. Initializing mirror repository if necessary"
                   " and performing initial sync...".format(url)))
            ret = create_sync_repo(n_path, url)
            if not ret:
                print("Project {} failed to sync.".format(n), file=sys.stderr)
                continue
            ret = sync_repo(n_path)
            if not ret:
                print("Project {} failed to sync.".format(n), file=sys.stderr)
                continue

# Entry Point
if __name__ == '__main__':
    check = check_paths()
    parse_args()
    if check is None:
        main()
    else:
        print(check)
        sys.exit(-1)


import os
import errno
from time import sleep
import shutil
import psutil

from PIL import Image

def main() -> None:
    # in_path = "images/initial_images_copied/"
    in_path = "images/"
    merge(in_path)

def merge(in_path: str) -> None:
    out_path = "../../images/moveLevels/"
    delete_directory(out_path)
    create_directory(out_path)
    img_list = []
    for img_path in sorted(os.listdir(in_path)):
        rgb_img = open_img_path(in_path + img_path)
        gray_img = fadeAlpha(rgb_img)
        img_list.append(gray_img)
    merged_img = merge_images(img_list)
    merged_img.save(out_path + 'a0.png')
    for i, img_path in enumerate(sorted(os.listdir(in_path))):
        rgb_img = open_img_path(in_path + img_path)
        new_list = img_list.copy()
        new_list[i] = rgb_img
        merged_img = merge_images(new_list)
        merged_img.save(out_path + 'a' + str(i+1) + ".png")

def merge_images(img_list: list) -> Image:
    image1 = img_list[0]
    i = 1
    while i < len(img_list):
        image2 = img_list[i]
        image1 = merge2images(image1, image2)
        i += 1
    return image1

def merge2images(image1: Image, image2: Image) -> Image:
    # resize, first image
    # image1 = image1.resize((426, 240))
    image1_size = image1.size
    image2_size = image2.size
    new_image = Image.new(
        'RGBA', (image1_size[0]+image2_size[0], image1_size[1]))
    new_image.paste(image1, (0, 0))
    new_image.paste(image2, (image1_size[0], 0))
    return new_image

def fadeAlpha(im2: Image):
    im = im2.copy()
    im.putalpha(255)
    width, height = im.size
    pixels = im.load()
    fade255 = 50
    # for y in range(int(height*.55), int(height*.75)):
    for y in range(height):
        # alpha = 255-int((y - height*.55)/height/.20 * 255)
        for x in range(width):
            pixels[x, y] = pixels[x, y][:3] + (fade255,)
    for y in range(y, height):
        for x in range(width):
            pixels[x, y] = pixels[x, y][:3] + (fade255,)
    return im

def open_img_path(img_path: str) -> Image:
    return Image.open(img_path)

def test_image_by_showing(rgb_img: Image) -> None:
    rgb_img.show()
    sleep(2)
    # hide image
    for proc in psutil.process_iter():
        if proc.name() == "display":
            proc.kill()

def delete_directory(out_path: str) -> None:
    try:
        shutil.rmtree(out_path)
    except OSError as e:
        print("Error: %s : %s" % (out_path, e.strerror))

def create_directory(out_path: str) -> None:
    try:
        os.makedirs(out_path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

if __name__ == "__main__":
    main()


#!/usr/bin/env python
from mpi4py import MPI
import numpy as np

# Example of implementing a custom reduction operation
# that identifies array indexes with odd numbers
# in any of the processors

def unpack_array(array_mem, dt):
    a = array_mem.tobytes()
    itemsize = dt.Get_size()
    array_len = int(array_mem.shape[0])
    unpack_array = []
    for i in range(int(array_len/itemsize)):
        unpack_array.append(int(np.frombuffer(a[itemsize*i:itemsize*(i+1)],
                            dtype=np.int64)))
    return np.array(unpack_array)

def myprod(xmem, ymem, dt):
    a = unpack_array(xmem, dt)
    b = unpack_array(ymem, dt)

    c = np.array([0] * len(a))
    for i in range(len(a)):
        c[i] = a[i] % 2 | b[i] % 2

    ymem[:] = memoryview(bytearray(c))[:]

comm = size = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

a = np.array([rank * (i + 1) for i in range(3)])
b = np.array([0] * 3)
print("Rank %d: Data %s" %(rank, a))

myop = MPI.Op.Create(myprod, True) # commute is True
comm.Reduce([a, MPI.DOUBLE], [b, MPI.DOUBLE], op=myop, root=0)

if rank==0:
    print("Contains odd numbers: %s" %(b))


import sqlite3
import sys

DB_NAME = "db/responses.db"

def add_to_db(table, response):
    '''
    Adds a response to the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Add the response to the database
    c.execute(f"INSERT INTO {table} VALUES (?)", (response,))

    # Commit the changes
    conn.commit()

    # Close the connection
    conn.close()

    print("Response added to database")

def delete_from_db(table, response):
    '''
    Deletes a response from the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Delete the response from the database
    c.execute(f"DELETE FROM {table} WHERE response = ?", (response,))

    # Commit the changes
    conn.commit()

    # Close the connection
    conn.close()

    print("Response deleted from database")

def list_responses(table):
    '''
    Lists all the responses in the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Get all the responses
    c.execute(f"SELECT * FROM {table}")
    responses = c.fetchall()

    # Close the connection
    conn.close()

    # Print the responses
    for response in responses:
        print(response[0])

def clear(table):
    '''
    Clears all the responses from the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Delete all the responses from the database
    c.execute(f"DELETE FROM {table}")

    # Commit the changes
    conn.commit()

    # Close the connection
    conn.close()

    print("Responses cleared from database")

def list_tables():
    '''
    Lists all the tables in the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Get all the tables
    c.execute("SELECT name FROM sqlite_master WHERE type='table'")
    tables = c.fetchall()

    # Close the connection
    conn.close()

    # Print the tables
    for table in tables:
        print(table[0])

def create_table(table):
    '''
    Creates a table in the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Create the table
    c.execute(f"CREATE TABLE {table} (response TEXT)")

    # Commit the changes
    conn.commit()

    # Close the connection
    conn.close()

    print(f"Created table {table}")

def delete_table(table):
    '''
    Deletes a table from the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Delete the table
    c.execute(f"DROP TABLE {table}")

    # Commit the changes
    conn.commit()

    # Close the connection
    conn.close()

    print(f"Deleted table {table}")

def get_random_response(table):
    '''
    Gets a random response from the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Get a random response
    c.execute(f"SELECT response FROM {table} ORDER BY RANDOM() LIMIT 1")
    response = c.fetchone()[0]

    # Close the connection
    conn.close()

    return response

def run_query(query):
    '''
    Runs a query on the database
    '''
    # Connect to the database
    conn = sqlite3.connect(DB_NAME)
    c = conn.cursor()

    # Run the query
    c.execute(query)
    result = c.fetchall()

    # Close the connection
    conn.close()

    return result

def help():
    print("add <table> <response> - Adds a response to the database")
    print("delete <table> <response> - Deletes a response from the database")
    print("list <table> - Lists all the responses in the database")
    print("clear <table> - Clears all the responses from the database")
    print("list_tables - Lists all the tables in the database")
    print("create_table <table> - Creates a table in the database")
    print("delete_table <table> - Deletes a table from the database")
    print("get_random_response <table> - Gets a random response from the database")
    print("run_query <query> - Runs a query on the database")
    print("help - Shows this help message")

# Run the script from the command line
if __name__ == "__main__":
    # Get the arguments
    args = sys.argv[1:]

    if len(args) == 0:
        help()
        sys.exit()

    # Check if the arguments are valid
    if len(args) < 2 and args[0] != "help" and args[0] != "list_tables":
        print("Invalid arguments")
        sys.exit()

    # Get the command and the response
    command = args[0]
    table = args[1] if len(args) > 1 else None
    response = " ".join(args[2:])

    # Check the command
    if command == "add":
        add_to_db(table, response)
    elif command == "delete":
        delete_from_db(table, response)
    elif command == "list":
        list_responses(table)
    elif command == "clear":
        clear(table)
    elif command == "list_tables":
        list_tables()
    elif command == "create_table":
        create_table(response)
    elif command == "delete_table":
        delete_table(table)
    elif command == "get_random_response":
        print(get_random_response(table))
    elif command == "run_query":
        print(run_query(response))
    elif command == "help":
        help()
    else:
        print("Invalid command")


from helper.warper import ImageMap
import cv2, numpy
from data_sources.csvsequence import CsvSequence
from helper import homography_func as hf
from data_sources.videofile import VideoFile
import time

surf = hf.SURF()

homographynet = hf.HomographyNet("../checkpoints/realdata2-vgg13.pth.tar")
video = CsvSequence("../datasets/example6.csv", base_path="../")

map_size = (1024,1000)
patch_location = (128,128)

imagemap = ImageMap(map_size)

stream1 = imagemap.create_stream(position=(0,200), func=homographynet, data=video, memory=1, distance=100, borders=False, indicate_centers=True) # homography func
stream2 = imagemap.create_stream(position=(0,600), func=surf, data=video, memory=1, distance=100) # sift

while not imagemap.finished():

    image = imagemap.warp()

    cv2.imshow("map", image)
    cv2.waitKey(1)

    last_matches = surf.draw_keypoints()
    if last_matches is not None:
        cv2.imshow("matches", last_matches)
        cv2.waitKey(1)

cv2.waitKey(0)


from django.conf.urls import url
from . import views

app_name = 'hostel'

urlpatterns = [

    url(r'^hostels', views.hostels, name="hostels"),
    url(r'^contact', views.contact, name="contact"),
    url(r'^about', views.about, name="about"),
    url(r'^boys_hostel', views.boys_hostel, name="boys_hostel"),
    url(r'^girls_hostel', views.girls_hostel, name="girls_hostel"),
    url(r'^signup', views.signup, name="signup"),
    # url(r'^login', views.loginUser, name="login"),
    # url(r'^logout', views.logoutUser, name="logout"),
    url(r'^user', views.userPage, name="userPage"),
]


# -*- coding: utf-8 -*-
'''
@createTool    : PyCharm-2020.3.2
@projectName   : pythonProjectPy3.9
@originalAuthor: Made in win10.Sys design by deHao.Zou
@createTime    : 2021/01/27 20:36
'''
# ============================================================================================
'''
目标：实现Excel全部功能
一、单元格样式简介
openpyxl处理Excel文件中单元格样式，总共有六个属性类。
font(字体类----可设置字号、字体颜色、下划线等)
fill(填充类----可设置单元格填充颜色等)
border（边框类----可以设置单元格各种类型的边框）
alignment(位置类----可以设置单元格内数据各种对齐方式)
number_format(格式类----可以设置单元格内各种类型的数据格式)
protection(保护类----可以设置单元格写保护等)
'''
# ============================================================================================
from openpyxl.comments import Comment
from openpyxl import Workbook, load_workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, NamedStyle, GradientFill, Color
from openpyxl.cell.cell import WriteOnlyCell
from openpyxl.chart import BarChart, Reference, Series
from openpyxl.utils.dataframe import dataframe_to_rows

'''表格创建'''
workbook = Workbook()  # write_only=True仅写模式,guess_types=True当读取单元格时,guess_types将启用或禁用（默认）类型推断(比如0.34 -> 34%)
worksheet = workbook.active

# 添加评论
# comment = worksheet["A1"].comment
# comment = Comment('This is the comment text', 'Comment Author')
# comment.text  # 'This is the comment text'
# comment.author  # 'Comment Author'

# 创建子页及子页名称
worksheet.title = 'Sheet.NO1'  # 子页名称（默认第一个）
worksheet = workbook.create_sheet('Create.Sheet2', 1)  # # 创建名为Sheet2的子页放在第2
worksheet = workbook.create_sheet('Create.Sheet1', 0)  # 创建名为Sheet1的子页放在第1

# worksheetcopy = workbook["New Title"]  # 给工作表命名后,即可将其作为工作簿的键
# print(workbook.sheetnames) # 打印全部子页名称

# 遍历工作表
# for sheet in workbook:
#     print(sheet.title)

# 在单个工作簿中创建工作表的副本
# 仅复制单元格（包括值,样式,超链接和注释）和某些工作表属性（包括尺寸,格式和属性）.不复制所有其他工作簿/工作表属性-例如图像,图表.
# 您不能在工作簿之间复制工作表.如果工作簿以只读或仅写模式打开,则您也无法复制工作表.
# source = workbook.active
# target = workbook.copy_worksheet(source)

# 工作簿保护
# workbook.security.workbookPassword = ''  # 防止其他用户查看隐藏的工作表,添加,移动,删除或隐藏工作表以及重命名工作表
# worksheet.security.revisionsPassword = ''  # 防止从共享工作簿中删除更改跟踪和更改历史记录
# workbook.security.set_workbook_password(hashed_password = '', already_hashed=True) # 如果需要在不使用默认哈希算法的情况下设置原始密码值，则提供特定的设置器功能

# 工作表保护
# 如果未指定密码,则用户无需指定密码即可禁用配置的工作表保护。否则,他们必须提供密码才能更改已配置的保护。
# workbook.protection.sheet = True # 设置属性来锁定工作表的各个方面
# workbook.protection.password = ''  # 属性设置密码

'''全局设置样式'''
# 子页全局背景颜色
worksheet.sheet_properties.tabColor = "1072BA"  # 默认白色

# 编辑页面设置
worksheet.page_setup.orientation = worksheet.ORIENTATION_LANDSCAPE
worksheet.page_setup.paperSize = worksheet.PAPERSIZE_TABLOID
worksheet.page_setup.fitToHeight = 0
worksheet.page_setup.fitToWidth = 1

highlight = NamedStyle(name="highlight")
number_format = 'General'

font = Font(name='Calibri',
            size=11,
            bold=True,
            italic=False,
            vertAlign=None,
            underline='none',
            strike=False,
            color='FF000000')
align = Alignment(horizontal='center', vertical='center')
# general bottom top left right center
alignment = Alignment(horizontal='general',
                      vertical='bottom',
                      text_rotation=0,
                      wrap_text=False,
                      shrink_to_fit=False,
                      indent=0)

protection = Protection(locked=True, hidden=False)
# thin 单线; double 双线
bottom_thin = Side(border_style="double", color="000000")  # 下
left_thin = Side(border_style="thin", color="000000")  # 左
top_double = Side(border_style="double", color="000000")  # 上
right_double = Side(border_style="thin", color="000000")  # 右
# border = Border(bottom=left_thin, left=left_thin, top=left_thin, right=left_thin)  # 边框样式
# thin double thick
border = Border(left=Side(border_style='thin', color='FF000000'),
                right=Side(border_style='thin', color='FF000000'),
                top=Side(border_style='thin', color='FF000000'),
                bottom=Side(border_style='thin', color='FF000000'),
                diagonal=Side(border_style='thin', color='FF000000'),
                diagonal_direction=0,
                outline=Side(border_style='thin', color='FF000000'),
                vertical=Side(border_style='thin', color='FF000000'),
                horizontal=Side(border_style='thin', color='FF000000')
                )
# title_font = Font(name='黑体', size=16, bold=True, color="000000")
title_font = Font(name='Calibri',  # 字体名称
                  size=11,  # 字体大小
                  bold=False,  # 加粗-True/False
                  italic=False,  # 斜体-True/False
                  vertAlign=None,  # 对其方向-
                  underline='none',  # 下划线-single/double
                  strike=False,
                  color='FF000000')

title_fill = PatternFill(fill_type='solid', fgColor="F8F8FF")
# fill = PatternFill(fill_type=None, start_color='FFFFFFFF', end_color='FF000000')
interval_fill = GradientFill(stop=("000000", "FFFFFF"))  # 连续区间底色填充
content_font = Font(name='宋体', size=14, bold=False, color="000000")
content_fill = PatternFill(fill_type='solid', fgColor="F8F8FF")

worksheet.merge_cells('A1:I2')  # 合并单元格,保留左上角值,其他删除
# worksheet.merge_cells(start_row=2, start_column=1, end_row=4, end_column=4)  # 合并单元格第二种方法
# worksheet.unmerge_cells('A1:I2')  # 拆分合并的单元格,值值保留在左上角
# worksheet.unmerge_cells(start_row=2, start_column=1, end_row=4, end_column=4) #拆分合并的单元格第二种方法
worksheet.cell(row=1, column=1).value = '合并的单元格内容'

'''内容填充及字段属性设置'''
# 注意：表格cell的最低行列值都是从1开始,不是0
for rowi in range(3, 8):
    for colj in range(1, 10):
        # _ = ws.cell(row=4, column=2, value=10)
        worksheet.cell(row=rowi, column=colj).value = rowi + colj

# 使用数学公式
# worksheet["A1"] = "=SUM(10, 10)"
# 插入图片
# image = Image('C:/Users/Zeus/Desktop/新logo.png')
# worksheet.add_image(imgage, 'A1')  # A1为插入图片起始单元格
# 折叠行列
# worksheet.row_dimensions.group(1, 3, outline_level=1, hidden=True) # outline_level显示折叠轮廓线(默认1),hidden直接隐藏掉折叠的行列(默认True)
# worksheet.column_dimensions.group('A', 'D', outline_level=1, hidden=False)

# ============================================================================================
'''数据可视化
1. 面积图
     ---2D区域图
     ---3D区域图
2. 条形图和柱形图---BarChart()
     ---垂直、水平和堆叠条形图
     ---3D条形图
3. 气泡图
4. 折线图
     ---折线图
     ---3D折线图
5. 散点图
6. 饼状图
     ---饼状图
     ---投影饼图
     ---3D饼图
7. 甜甜圈图
8. 雷达图
9. 股票图
10.表面图
'''

# ---条形图
# from openpyxl import Workbook
# from openpyxl.chart import BarChart, Reference, Series
#
# wb = Workbook()
# ws = wb.active
# for i in range(15):
#     ws.append([i])
#
# values = Reference(ws, min_row=1, max_row=10, min_col=1, max_col=1)  # 选取区域内容
# chart = BarChart()
# chart.add_data(values)
# ws.add_chart(chart, "E15")  # 大小为15 x 7.5厘米(约5列乘14行),可以通过设置图表的anchor,width和height属性来更改
# wb.save("C:/Users/Zeus/Desktop/SampleChart.xlsx")

# ---散点图
# 坐标-最小最大范围设置
# from openpyxl import Workbook
# from openpyxl.chart import ScatterChart, Reference, Series
#
# wb = Workbook()
# ws = wb.active
#
# ws.append(['X', '1/X'])
# for x in range(-10, 11):
#     if x:
#         ws.append([x, 1.0 / x])
#
# chart1 = ScatterChart()
# chart1.title = "Full Axes"
# chart1.x_axis.title = 'x'
# chart1.y_axis.title = '1/x'
# chart1.legend = None
#
# chart1.x_axis.scaling.min = -10
# chart1.x_axis.scaling.max = 10
# chart1.y_axis.scaling.min = -1.5
# chart1.y_axis.scaling.max = 1.5
#
# chart2 = ScatterChart()
# chart2.title = "Clipped Axes"
# chart2.x_axis.title = 'x'
# chart2.y_axis.title = '1/x'
# chart2.legend = None
#
# chart2.x_axis.scaling.min = 0
# chart2.x_axis.scaling.max = 11
# chart2.y_axis.scaling.min = 0
# chart2.y_axis.scaling.max = 1.5
#
# x = Reference(ws, min_col=1, min_row=2, max_row=22)
# y = Reference(ws, min_col=2, min_row=2, max_row=22)
# s = Series(y, xvalues=x)
# chart1.append(s)
# chart2.append(s)
#
# ws.add_chart(chart1, anchor="C1")
# ws.add_chart(chart2, anchor="C15")
#
# wb.save("C:/Users/Zeus/Desktop/minmax.xlsx")

# 坐标-对数缩放
# from openpyxl import Workbook
# from openpyxl.chart import ScatterChart, Reference, Series
# import math
#
# wb = Workbook()
# ws = wb.active
#
# ws.append(['X', 'Gaussian'])
# for i, x in enumerate(range(-10, 11)):
#     ws.append([x, "=EXP(-(($A${row}/6)^2))".format(row=i + 2)])
#
# chart1 = ScatterChart()
# chart1.title = "No Scaling"
# chart1.x_axis.title = 'x'
# chart1.y_axis.title = 'y'
# chart1.legend = None
#
# chart2 = ScatterChart()
# chart2.title = "X Log Scale"
# chart2.x_axis.title = 'x (log10)'
# chart2.y_axis.title = 'y'
# chart2.legend = None
# chart2.x_axis.scaling.logBase = 10
#
# chart3 = ScatterChart()
# chart3.title = "Y Log Scale"
# chart3.x_axis.title = 'x'
# chart3.y_axis.title = 'y (log10)'
# chart3.legend = None
# chart3.y_axis.scaling.logBase = 10
#
# chart4 = ScatterChart()
# chart4.title = "Both Log Scale"
# chart4.x_axis.title = 'x (log10)'
# chart4.y_axis.title = 'y (log10)'
# chart4.legend = None
# chart4.x_axis.scaling.logBase = 10
# chart4.y_axis.scaling.logBase = 10
#
# chart5 = ScatterChart()
# chart5.title = "Log Scale Base e"
# chart5.x_axis.title = 'x (ln)'
# chart5.y_axis.title = 'y (ln)'
# chart5.legend = None
# chart5.x_axis.scaling.logBase = math.e
# chart5.y_axis.scaling.logBase = math.e
#
# x = Reference(ws, min_col=1, min_row=2, max_row=22)
# y = Reference(ws, min_col=2, min_row=2, max_row=22)
# s = Series(y, xvalues=x)
# chart1.append(s)
# chart2.append(s)
# chart3.append(s)
# chart4.append(s)
# chart5.append(s)
#
# ws.add_chart(chart1, "C1")
# ws.add_chart(chart2, "I1")
# ws.add_chart(chart3, "C15")
# ws.add_chart(chart4, "I15")
# ws.add_chart(chart5, "F30")
#
# wb.save("C:/Users/Zeus/Desktop/log.xlsx")

# 坐标-方向设置
# from openpyxl import Workbook
# from openpyxl.chart import ScatterChart, Reference, Series
#
# wb = Workbook()
# ws = wb.active
#
# ws["A1"] = "Archimedean Spiral"
# ws.append(["T", "X", "Y"])
# for i, t in enumerate(range(100)):
#     ws.append([t / 16.0, "=$A${row}*COS($A${row})".format(row=i + 3),
#                "=$A${row}*SIN($A${row})".format(row=i + 3)])
#
# chart1 = ScatterChart()
# chart1.title = "Default Orientation"
# chart1.x_axis.title = 'x'
# chart1.y_axis.title = 'y'
# chart1.legend = None
#
# chart2 = ScatterChart()
# chart2.title = "Flip X"
# chart2.x_axis.title = 'x'
# chart2.y_axis.title = 'y'
# chart2.legend = None
# chart2.x_axis.scaling.orientation = "maxMin"
# chart2.y_axis.scaling.orientation = "minMax"
#
# chart3 = ScatterChart()
# chart3.title = "Flip Y"
# chart3.x_axis.title = 'x'
# chart3.y_axis.title = 'y'
# chart3.legend = None
# chart3.x_axis.scaling.orientation = "minMax"
# chart3.y_axis.scaling.orientation = "maxMin"
#
# chart4 = ScatterChart()
# chart4.title = "Flip Both"
# chart4.x_axis.title = 'x'
# chart4.y_axis.title = 'y'
# chart4.legend = None
# chart4.x_axis.scaling.orientation = "maxMin"
# chart4.y_axis.scaling.orientation = "maxMin"
#
# x = Reference(ws, min_col=2, min_row=2, max_row=102)
# y = Reference(ws, min_col=3, min_row=2, max_row=102)
# s = Series(y, xvalues=x)
# chart1.append(s)
# chart2.append(s)
# chart3.append(s)
# chart4.append(s)
#
# ws.add_chart(chart1, "D1")
# ws.add_chart(chart2, "J1")
# ws.add_chart(chart3, "D15")
# ws.add_chart(chart4, "J15")
#
# wb.save("C:/Users/Zeus/Desktop/orientation.xlsx")

# 坐标-添加第二坐标
# from openpyxl import Workbook
# from openpyxl.chart import LineChart, BarChart, Reference, Series
#
# wb = Workbook()
# ws = wb.active
#
# rows = [
#     ['Aliens', 2, 3, 4, 5, 6, 7],
#     ['Humans', 10, 40, 50, 20, 10, 50],
# ]
#
# for row in rows:
#     ws.append(row)
#
# c1 = BarChart()
# v1 = Reference(ws, min_col=1, min_row=1, max_col=7)
# c1.add_data(v1, titles_from_data=True, from_rows=True)
#
# c1.x_axis.title = 'Days'
# c1.y_axis.title = 'Aliens'
# c1.y_axis.majorGridlines = None
# c1.title = 'Survey results'
#
# # Create a second chart
# c2 = LineChart()
# v2 = Reference(ws, min_col=1, min_row=2, max_col=7)
# c2.add_data(v2, titles_from_data=True, from_rows=True)
# c2.y_axis.axId = 200
# c2.y_axis.title = "Humans"
#
# # Display y-axis of the second chart on the right by setting it to cross the x-axis at its maximum
# c1.y_axis.crosses = "max"
# c1 += c2
#
# ws.add_chart(c1, "D4")
#
# wb.save("C:/Users/Zeus/Desktop/secondary.xlsx")

# ============================================================================================
'''局部设置样式'''
for row in worksheet.rows:
    for cell in row:
        cell.alignment = align
        if cell.row == 1 or cell.row == 2:
            cell.border = border
            cell.font = title_font
            cell.fill = title_fill
        else:
            cell.border = border
            cell.font = content_font
            cell.fill = content_fill

# col = worksheet.column_dimensions['A']
# col.font = Font(bold=True)
# row = worksheet.row_dimensions[1]
# row.font = Font(underline="single")
# c = Color(indexed=32)  # 按索引选颜色
# c = Color(theme=6, tint=0.5)  # 使用主题
# workbook.add_named_style(highlight)

'''储存表格'''
workbook.save(filename='C:/Users/Zeus/Desktop/openpyxl.xlsx')  # 储存格式：xlsx、xlsm或zip

# # 读取与储存文件
# # workbook = load_workbook(filename='large_file.xlsx', read_only=True) # 仅读模式
# workbook = load_workbook('document.xlsx')
# # 当读取单元格时,guess_types将启用或禁用（默认）类型推断.
# # read_only仅读取模式,不可改动
# # data_only控制具有公式的单元格是否具有公式(默认值)或Excel上次读取工作表时存储的值.
# # keep_vba控制是否保留任何Visual Basic元素(默认).如果保留它们,则它们仍不可编辑.
# print(workbook.get_sheet_names())
# workbook.template = True  # template = True,以将工作簿另存为模板;
# workbook.save('document_template.xltx')  # 注意为xltx格式
# workbook = load_workbook('document_template.xltx')
# workbook.template = False  # 将此属性设置为 template =False(默认)以另存为文档
# workbook.save('document.xlsx', as_template=False)