import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import streamlit as st
import ptitprince as pt
def scatter_plot(df,fig):
    hobbies = []

    for col in df.columns:
        hobbies.append(col)
        print(col)
    st.title(" Scatter Plot")
    hobby = st.selectbox("X-axis: ", hobbies)
    # print the selected hobby
    st.write("You have selected X-axis: ", hobby)
    hobby1 = st.selectbox("Y-axis: ", hobbies)
    st.write("You have selected Y-axis: ", hobby1)
    if (st.button("scatter plot")):
        st.text("scatter plot")
        ax = sns.regplot(x=hobby, y=hobby1, data=df)
        st.pyplot(fig)
def group_histogram(df,fig):
    st.title("Grouped Histogram")

    if (st.button("Grouped Histogram")):
        st.text("Grouped Histogram")
        for condition in df.TrialType.unique():
            cond_data = df[(df.TrialType == condition)]
            ax = sns.distplot(cond_data.RT, kde=False)
        ax.set(xlabel='Response Time', ylabel='Frequency')
        st.pyplot(fig)
def bar_plot(df,fig,hobbies):
    st.title("Bar Plot")
    hobby = st.selectbox("X-axis for barplot: ", hobbies)
    # print the selected hobby
    st.write("You have selected X-axis: ", hobby)
    hobby1 = st.selectbox("Y-axis for barplot: ", hobbies)
    st.write("You have selected Y-axis: ", hobby1)
    if (st.button("Bar plot")):
        st.text("Bar plot")
        sns.barplot(x=hobby, y=hobby1, data=df)
        st.pyplot(fig)
def box_plot1(df,fig,hobbies):
    st.title("Box Plot")
    hobby = st.selectbox("X-axis for boxplot: ", hobbies)
    # print the selected hobby
    st.write("You have selected X-axis: ", hobby)
    hobby1 = st.selectbox("Y-axis for boxplot: ", hobbies)
    st.write("You have selected Y-axis: ", hobby1)
    if (st.button("Box plot")):
        st.text("Box plot")
        sns.boxplot(x=hobby, y=hobby,data=df)
        st.pyplot(fig)
def heat_map(df,fig,hobbies):
    st.title("Heatmap Plot")
    col2 = st.multiselect(
        "Blah:", sorted(list(hobbies)), sorted(list(hobbies))
    )
    if (st.button("Heatmap plot")):
        st.text("Heatmap plot")
        ax = sns.heatmap(df[col2])
        st.pyplot(fig)
def violine_plot(df,fig,hobbies):
    st.title("Violin Plot")
    hobby = st.selectbox("X-axis for Violinplot: ", hobbies)
    # print the selected hobby
    st.write("X-axis for Violinplot: ", hobby)
    hobby1 = st.selectbox("Y-axis for Violinplot: ", hobbies)
    st.write("You have selected Y-axis: ", hobby1)
    if (st.button("Violin plot")):
        st.text("Violin plot")
        sns.violinplot(x=hobby, y=hobby1, data=df)
        st.pyplot(fig)
def rain_cloudplot(df,fig,hobbies):
    st.title("Raincloud Plot")
    hobby = st.selectbox("X-axis for Raincloudplot: ", hobbies)
    # print the selected hobby
    st.write("You have selected X-axis: ", hobby)
    hobby1 = st.selectbox("Y-axis for Raincloudplot: ", hobbies)
    st.write("You have selected Y-axis: ", hobby1)
    if (st.button("Raincloud plot")):
        st.text("Raincloud plot")
        ax = pt.RainCloud(x=hobby, y=hobby1,
                          data=df,
                          width_viol=.8,
                          width_box=.4,
                          figsize=(12, 8), orient='h',
                          move=.0)
        st.pyplot(fig)
def app():
    filename = st.text_input('Enter a file path:')

    try:
        df = pd.read_csv(filename)
    except:
        None
    uploaded_files = st.file_uploader("Upload CSV", type="csv", accept_multiple_files=True)
    if uploaded_files:
        for file in uploaded_files:
            file.seek(0)
        uploaded_data_read = [pd.read_csv(file) for file in uploaded_files]
        df = pd.concat(uploaded_data_read)
    hobbies=[]
    try:
        fig = plt.figure(figsize=(12, 8))
        for col in df.columns:
            hobbies.append(col)
            print(col)

        fig = plt.figure(figsize=(12, 8))
        st.dataframe(data=df, width=None, height=None)

        scatter_plot(df,fig)
        group_histogram(df, fig)
        bar_plot(df, fig, hobbies)
        heat_map(df, fig, hobbies)
        violine_plot(df, fig, hobbies)
        box_plot1(df, fig, hobbies)
        rain_cloudplot(df, fig, hobbies)
    except:
        None


# 0 1 2 3 4
# 5 6 7 8 9
# 0 1 2 3 4
# 5 6 7 8 9

def rosy():
    counter=0
    for rows in range(1,3):
        for col in range(0, 5):
            print(counter,end=' ')
            counter += 1
        print()
rosy()
rosy()


#숫자 카드 2
from bisect import bisect_left, bisect_right

def binary(x):
    start = bisect_left(data_n, x)
    end = bisect_right(data_n, x)

    return end - start

n = int(input())
data_n = sorted(list(map(int, input().split())))
m = int(input())
data_m = list(map(int, input().split()))
for x in data_m:
    print(binary(x), end= ' ')


#coding=utf8
import config
import json
import sys, time
py3k = sys.version_info.major > 2
import os.path
import urllib

if py3k:
	from urllib import parse as urlparse
else:
	import urlparse

def get_one():
	return config.dbconn().fetch_rows('http', condition={'checked': 0}, order="id asc", limit="1", fetchone=True)

def check_key(key):
	'''
	是否需要保留这个key
	'''
	blacklist = ['t', 'r', 'submit']
	if key.lower() in blacklist:
		return False
	return True

def check_value(value, vtype):
	'''
	是否需要保留这个value
	'''
	if vtype == 'array': return False
	return True

def get_type(key, value):
	if type(value) == type([]):	return 'array'
	value = value[0]
	if value.isdigit(): return 'int'
	try:
		float(value)
		return 'float'
	except:
		pass
	# url check
	u = urlparse.urlparse(value)
	if u.scheme and u.netloc:
		return 'url'
	try:
		j = json.loads(value)
		if type(j) == type([]) or type(j) == type({}):
			return 'json'
	except:
		pass
	return 'str'

while True:
	http = get_one()
	if not http:
		time.sleep(3)
		continue
	req = json.loads(http['req'])

	if req['rtype'] not in ['qs', 'rewrite']:
		config.dbconn().insert('requests', {'requestid': http['id'], 'method': req['method'], 'key': '', 'type': 'special|'+req['rtype']})
	else:
		# support array like a[]=1&a[]=2
		parsed = urlparse.urlparse(req['uri'])
		get_parts = urlparse.parse_qs(parsed.query)
		if get_parts:
			for k,v in get_parts.items():
				v = v[0] if len(v) == 1 else v
				vtype = get_type(k, v)
				if check_key(k) and check_value(v, vtype):
					config.dbconn().insert('requests', {'requestid': http['id'], 'method': "GET", 'key': k, 'type': vtype})

		if not parsed.query and not os.path.splitext(parsed.path)[1] and len(parsed.path.split('/')) > 3:
			path_parts = parsed.path.split('/')
			for i in range(3, len(path_parts)):
				vtype = 'rewrite|'+get_type('rewrite', path_parts[i])
				config.dbconn().insert('requests', {'requestid': http['id'], 'method': "GET", 'key': str(i), 'type': vtype})

		if req['method'] == "POST":
			post_parts = urlparse.parse_qs(urlparse.urlparse(req['body']).query)
			if post_parts:
				for k,v in post_parts.items():
					v = v[0] if len(v) == 1 else v
					vtype = get_type(k, v)
					if check_key(k) and check_value(v, vtype):
						config.dbconn().insert('requests', {'requestid': http['id'], 'method': "POST", 'key': k, 'type': vtype})

	config.dbconn().update('http', {'checked': 1}, {'id': http['id']})


#######################################
# This file computes several characteristics of the portage graph
#######################################

import math
import sys

import core_data
import hyportage_constraint_ast
import hyportage_data
import utils

import graphs
import host.scripts.utils
from host.scripts import hyportage_db

data = {}

######################################################################
# GENERIC STATISTICS EXTRACTION FUNCTION
######################################################################

def map_base_statistics(value_number, total_size, map_size):
	average = total_size / float(value_number)
	variance = 0
	for key, value in map_size.iteritems():
		tmp = average - key
		tmp = tmp * tmp * len(value)
		variance = variance + tmp
	variance = math.sqrt(variance / value_number)
	return average, variance

def generics(input_iterator, extract_data, extract_key, filter_function=host.scripts.utils.filter_function_simple, store_data_map=False):
	value_number = 0

	map_data = {}
	map_size = {}

	total_size = 0
	max_size = 0
	min_size = sys.maxint

	for element in input_iterator:
		if filter_function(element):
			value_number = value_number + 1

			data = extract_data(element)
			key = extract_key(element)
			size = len(data)
			if store_data_map:
				if data in map_data: map_data[data].add(key)
				else: map_data[data] = {key}
			if size in map_size: map_size[size].add(key)
			else: map_size[size] = {key}

			total_size = total_size + size
			if size > max_size: max_size = size
			if size < min_size: min_size = size

	average, variance = map_base_statistics(value_number, total_size, map_size)

	return {
		'number': value_number,
		'map_data': map_data,
		'map_size': map_size,
		'total_size': total_size,
		'average': average,
		'variance': variance,
		'max_size': max_size,
		'min_size': min_size
	}

def generic_map(input_map, extraction_function, filter_function=host.scripts.utils.filter_function_simple, store_data_map=False):
	return generics(input_map.iteritems(), lambda el: extraction_function(el[1]), lambda el: el[0], filter_function, store_data_map)

def generic_list(input_list, extraction_function, filter_function=host.scripts.utils.filter_function_simple, store_data_map=False):
	return generics(input_list, extraction_function, lambda el: tuple(el), filter_function, store_data_map)

######################################################################
# FEATURES
######################################################################

def features(filter_function=host.scripts.utils.filter_function_simple):
	utils.phase_start("Computing the USE Flags Core Statistics.")

	required = sum([len(spl.required_iuses) for spl in hyportage_db.mspl.itervalues() if filter_function(spl)])
	local = sum([len(spl.iuses_default) for spl in hyportage_db.mspl.itervalues() if filter_function(spl)])

	global data
	data['features'] = generic_map(hyportage_db.mspl, hyportage_data.spl_get_iuses_full, filter_function)
	data['features']['average_required'] = required / float(data['features']['number'])
	data['features']['average_local'] = local / float(data['features']['number'])
	utils.phase_end("Computation Completed")

def features_usage(filter_function=host.scripts.utils.filter_function_simple):
	utils.phase_start("Computing the USE Flags Core Statistics.")
	map_features = {}
	for key, value in hyportage_db.mspl.iteritems():
		if filter_function(value):
			for feature in hyportage_data.spl_get_required_iuses(value):
				if feature in map_features: map_features[feature].add(key)
				else: map_features[feature] = {key}

	global data
	data['feature_usage'] = generic_map(map_features, core_data.identity, filter_function)
	data['feature_usage']['map_data'] = map_features
	utils.phase_end("Computation Completed")

"""
def statistics_features(filter_function=db.filter_function_simple):
	utils.phase_start("Computing the USE Flags Core Statistics.")

	features_number = 0
	features_max = 0
	features_min = 100
	spl_min = []
	spl_max = []
	spl_number = 0
	for spl in db.mspl.itervalues():
		if filter_function(spl):
			spl_number = spl_number + 1
			use_flag_size = len(hyportage_data.spl_get_required_iuses(spl))
			if use_flag_size < features_min:
				features_min = use_flag_size
				spl_min = [spl.name]
			elif use_flag_size == features_min: spl_min.append(spl.name)
			if use_flag_size > features_max:
				features_max = use_flag_size
				spl_max = [spl.name]
			elif use_flag_size == features_max: spl_max.append(spl.name)
			features_number = features_number + use_flag_size
	res = {
		'min': features_min,
		'min_spl_list': sorted(spl_min),
		'max': features_max,
		'max_spl_list': sorted(spl_max),
		'number': features_number,
		'spl_number': spl_number,
		'average': features_number / spl_number
	}
	global statistics
	statistics['features'] = res
	utils.phase_end("Computation Completed")
"""

######################################################################
# DEPENDENCIES
######################################################################

class GETGuardedDependenciesVisitor(hyportage_constraint_ast.ASTVisitor):
	def __init__(self):
		super(hyportage_constraint_ast.ASTVisitor, self).__init__()
		self.res = {}
		self.guards = 0

	def visitDependCONDITION(self, ctx):
		self.guards = self.guards + 1
		map(self.visitDependEL, ctx['els'])
		self.guards = self.guards - 1

	def visitDependSIMPLE(self, ctx):
		pattern = ctx['atom']
		if pattern in self.res:
			if self.guards == 0: self.res[pattern]['guarded'] = False
			if "selection" in ctx: self.res[pattern]['selects'] = True
		else: self.res[pattern] = {'guarded': self.guards > 0, 'selects': "selection" in ctx}

	def visitSPL(self, spl):
		self.visitDepend(spl.fm_combined)
		res = self.res
		self.res = {}
		self.guards = 0
		return res

def dependencies(filter_function=host.scripts.utils.filter_function_simple):
	utils.phase_start("Computing the Dependencies Statistics.")
	visitor = GETGuardedDependenciesVisitor()
	local_map = {spl.name: visitor.visitSPL(spl) for spl in hyportage_db.mspl.itervalues()}
	def extraction_function_all(data): return data.keys()
	def extraction_function_guarded(data): return {pattern for pattern in data.iterkeys() if data[pattern]['guarded']}
	def extraction_function_selects(data): return {pattern for pattern in data.iterkeys() if data[pattern]['selects']}

	global data
	data['dependencies_all'] = generic_map(local_map, extraction_function_all, filter_function)
	data['dependencies_guarded'] = generic_map(local_map, extraction_function_guarded, filter_function)
	data['dependencies_selects'] = generic_map(local_map, extraction_function_selects, filter_function)
	utils.phase_end("Computation Completed")

def lone_packages(filter_function=host.scripts.utils.filter_function_simple):
	referenced_spls = {
		spl
		for el in hyportage_db.flat_pattern_repository.itervalues()
		for spl in el.__generate_matched_spls(hyportage_db.mspl, hyportage_db.spl_groups)
	}

	spls = filter(filter_function, hyportage_db.mspl.itervalues())
	spls = filter(lambda spl: len(spl.dependencies) == 0, spls)
	spls = filter(lambda spl: spl not in referenced_spls, spls)

	global data
	data['lone_packages'] = spls

"""
def statistics_dependencies(filter_function=db.filter_function_simple):
	utils.phase_start("Computing the Dependencies Core Statistics.")
	dependencies_number = 0
	dependencies_max = 0
	dependencies_min = 100
	dependencies_guarded_number = 0
	dependencies_guarded_max = 0
	dependencies_guarded_min = 100
	spl_number = 0
	spl_max = []
	spl_min = []
	spl_guarded_number = 0
	spl_guarded_max = []
	spl_guarded_min = []

	visitor = GETDependenciesVisitor()
	for spl in db.mspl.itervalues():
		if filter_function(spl):
			spl_number = spl_number + 1
			deps = visitor.visitSPL(spl)
			#print("  " + spl.name + ": " + str(deps))
			dependencies_size = len(deps)
			if dependencies_size < dependencies_min:
				dependencies_min = dependencies_size
				spl_min = [spl.name]
			elif dependencies_size == dependencies_min:
				spl_min.append(spl.name)
			if dependencies_size > dependencies_max:
				dependencies_max = dependencies_size
				spl_max = [spl.name]
			elif dependencies_size == dependencies_max:
				spl_max.append(spl.name)
			dependencies_number = dependencies_number + dependencies_size

			deps_guarded = {k for k, v in deps.iteritems() if v}
			dependencies_guarded_size = len(deps_guarded)
			if dependencies_guarded_size < dependencies_guarded_min:
				dependencies_guarded_min = dependencies_guarded_size
				spl_guarded_min = [spl.name]
			elif dependencies_guarded_size == dependencies_guarded_min:
				spl_guarded_min.append(spl.name)
			if dependencies_guarded_size > dependencies_guarded_max:
				dependencies_max = dependencies_guarded_size
				dependencies_guarded_max = [spl.name]
			elif dependencies_guarded_size == dependencies_guarded_max:
				spl_guarded_max.append(spl.name)
			dependencies_guarded_number = dependencies_guarded_number + dependencies_guarded_size
			if dependencies_guarded_size > 0: spl_guarded_number = spl_guarded_number + 1

	res = {
		'min': dependencies_min,
		'min_spl_list': sorted(spl_min),
		'max': dependencies_max,
		'max_spl_list': sorted(spl_max),
		'number': dependencies_number,
		'spl_number': spl_number,
		'average': dependencies_number / spl_number,
		'guarded_min': dependencies_guarded_min,
		'guarded_min_spl_list': sorted(spl_guarded_min),
		'guarded_max': dependencies_guarded_max,
		'guarded_max_spl_list': sorted(spl_guarded_max),
		'guarded_number': dependencies_guarded_number,
		'guarded_spl_number': spl_guarded_number,
		'guarded_average': dependencies_guarded_number / spl_guarded_number
	}
	global statistics
	statistics['dependencies'] = res
	utils.phase_end("Computation Completed")
"""

######################################################################
# PATTERNS (ABSTRACT SPL)
######################################################################

def pattern_refinement(filter_function=host.scripts.utils.filter_function_simple):
	utils.phase_start("Computing the Pattern (refinement) Statistics.")
	def extraction_function(element): return element.matched_spls(hyportage_db.mspl, hyportage_db.spl_groups)
	global data
	data['pattern_refinement'] = generic_map(hyportage_db.flat_pattern_repository, extraction_function, filter_function)
	utils.phase_end("Computation Completed")

def statistics_pattern(filter_function=host.scripts.utils.filter_function_simple):
	utils.phase_start("Computing the Pattern Core Statistics.")
	pattern_number = 0
	pattern_usage = {}
	pattern_usage_max = 0
	for pattern_element in hyportage_db.flat_pattern_repository.itervalues():
		if filter_function(pattern_element):
			pattern_number = pattern_number + 1
			size = len(pattern_element.containing_spl)
			if pattern_usage_max < size:
				pattern_usage_max = size
			if size in pattern_usage:
				pattern_usage[size].extend(pattern_element)
			else: pattern_usage[size] = [pattern_element]
	pattern_abstraction_number = 0
	pattern_abstraction_max = [0, []]
	pattern_abstraction_min = [100, []]
	for pattern_element in hyportage_db.flat_pattern_repository.itervalues():
		if filter_function(pattern_element):
			pattern_abstraction_size = len(pattern_element.matched_spls(hyportage_db.mspl, hyportage_db.spl_groups))
			if pattern_abstraction_size < pattern_abstraction_min[0]:
				pattern_abstraction_min[0] = pattern_abstraction_size
				pattern_abstraction_min[1] = [pattern_element]
			elif pattern_abstraction_size == pattern_abstraction_min[0]:
				pattern_abstraction_min[1].append(pattern_element)
			if pattern_abstraction_size > pattern_abstraction_max[0]:
				pattern_abstraction_max[0] = pattern_abstraction_size
				pattern_abstraction_max[1] = [pattern_element]
			elif pattern_abstraction_size == pattern_abstraction_max[0]:
				pattern_abstraction_max[1].append(pattern_element)
			pattern_abstraction_number = pattern_abstraction_number + pattern_abstraction_size

	res = {
		'number': pattern_number,
		'usage': pattern_usage,
		'usage_max': pattern_usage_max,
		'usage_average': pattern_usage_max / pattern_number,
		'total_abstraction_number': pattern_abstraction_number,
		'abstraction_min': pattern_abstraction_min[0],
		'abstraction_min_list': pattern_abstraction_min[1],
		'abstraction_max': pattern_abstraction_max[0],
		'abstraction_max_list': pattern_abstraction_max[1]
	}
	global data
	data['patterns'] = res
	utils.phase_end("Computation Completed")

######################################################################
# CYCLES
######################################################################

def graph(filter_function=host.scripts.utils.filter_function_simple):
	utils.phase_start("Computing the Graph Core Statistics.")
	graph_mspl, spl_nodes = graphs.mspl(filter_function, keep_self_loop=True)
	nodes_spl = {node: spl for spl, node in spl_nodes.iteritems()}
	visited = graph_mspl.getBooleanProperty("visited")

	for n in graph_mspl.getNodes():
		visited.setNodeValue(n, False)

	shairplay_len = sys.maxint

	cycles = []
	for n in graph_mspl.getNodes():
		if not visited.getNodeValue(n):
			visited.setNodeValue(n, True)
			path = [n]
			branches = [graph_mspl.getOutNodes(n)]
			if "shairplay" in nodes_spl[n].name: shairplay_len = 1
			while path:
				if len(path) >= shairplay_len: print(str([nodes_spl[node].name for node in path]))
				if branches[-1].hasNext():
					succ = branches[-1].next()
					if len(path) >= shairplay_len: print("  found: " + nodes_spl[succ].name)
					if succ in path:
						if len(path) >= shairplay_len: print("  loop found: " + str([nodes_spl[node].name for node in path[path.index(succ):]]))
						cycles.append([nodes_spl[node].name for node in path[path.index(succ):]])
					elif not visited.getNodeValue(succ):
						visited.setNodeValue(succ, True)
						path.append(succ)
						branches.append(graph_mspl.getOutNodes(succ))
						if "shairplay" in nodes_spl[succ].name: shairplay_len = len(path)
				else:
					path.pop()
					branches.pop()
					if len(path) < shairplay_len: shairplay_len = sys.maxint

	res = generic_map({tuple(v): v for v in cycles}, core_data.identity, host.scripts.utils.filter_function_simple)
	res['cycles'] = cycles
	global data
	data['graph'] = res
	utils.phase_end("Computation Completed")


#!/usr/bin/python

class NodeVisitor(object):

    def visit(self, node):
        method = 'visit_' + node.__class__.__name__
        visitor = getattr(self, method, self.generic_visit)
        return visitor(node)

    def generic_visit(self, node):        # Called if no explicit visitor function exists for a node.
        print("(%d,%d): %s(%s)" %(node.lineno, node.lineno, node.type, node.value))
        print("UNKNOWN STRUCTURE")
        error_found = 1

class TypeChecker(NodeVisitor):

    error_found = 0;

    returnedType = {'int' : {}, 'float' : {}, 'string' : {}}
    for i in returnedType.keys():
        returnedType[i] = {}
        for j in returnedType.keys():
            returnedType[i][j] = {}
            for k in ['+','-','/','*','%']:
                returnedType[i][j][k] = []

    returnedType['int']['float']['+'] = 'float'
    returnedType['int']['int']['+'] = 'int'
    returnedType['float']['float']['+'] = 'float'
    returnedType['float']['int']['+'] = 'float'
    returnedType['string']['string']['+'] = 'string'
    returnedType['int']['float']['-'] = 'float'
    returnedType['int']['int']['-'] = 'int'
    returnedType['float']['float']['-'] = 'float'
    returnedType['float']['int']['-'] = 'float'
    returnedType['int']['float']['*'] = 'float'
    returnedType['int']['int']['*'] = 'int'
    returnedType['float']['float']['*'] = 'float'
    returnedType['float']['int']['*'] = 'float'
    returnedType['string']['int']['*'] = 'string'
    returnedType['int']['float']['/'] = 'float'
    returnedType['int']['int']['/'] = 'int'
    returnedType['float']['float']['/'] = 'float'
    returnedType['float']['int']['/'] = 'float'
    returnedType['int']['int']['%'] = 'int'

    returnedTypeRelative = {'int' : {}, 'float' : {}, 'string' : {}}
    for i in returnedTypeRelative.keys():
        returnedTypeRelative[i] = {}
        for j in returnedTypeRelative.keys():
            returnedTypeRelative[i][j] = 'err'

    returnedTypeRelative['int']['float'] = 'int'
    returnedTypeRelative['int']['int'] = 'int'
    returnedTypeRelative['float']['float'] = 'int'
    returnedTypeRelative['float']['int'] = 'int'
    returnedTypeRelative['string']['string'] = 'int'

    funs = {}

    where_declared = {}

    current_function = '*'

    scope_number = 0

    loop_number = 0

    comp = 0

    tmp_dec = {}

    def visit_Program(self, node):
        self.visit(node.insts)
        return self.where_declared

    def visit_Instructions(self, node):
        for ins in node.instrs:
            self.visit(ins)

    def visit_Print(self, node):
        if self.visit(node.expr) not in ('int','float','string'):
            print("CANNOT PRINT", node.expr)
            self.error_found = 1

    def visit_Assignment(self, node):
        if node.id in self.where_declared.keys():
            if self.current_function in self.where_declared[node.id].keys():
                type1 = self.where_declared[node.id][self.current_function]
            elif '*' in self.where_declared[node.id].keys():
                type1 = self.where_declared[node.id]['*']
            else:
                type1 = 'undeclared'
                # print("UNDECLARED VARIABLE",node.id)
                # self.error_found = 1
        else:
            type1 = 'undeclared'
            # print("UNDECLARED VARIABLE",node.id)
            # self.error_found = 1
        type2 = self.visit(node.expr)
        if type1 != 'undeclared' and type1 != type2:
            print("TYPE MISMATCH IN ASSIGNMENT\n")
            self.error_found = 1

    def visit_Expressions(self, node):
        tmp = []
        for expr in node.exprs:
            tmp.append(self.visit(expr))
        return tmp

    def visit_Matrix_function(self, node):
        if self.visit(node.arg) != 'int':
            print("Matrix function takes not int")
            self.error_found = 1

    def visit_Const(self, node):
        if type(node.value) == str:
            return 'string'
        if type(node.value) == int:
            return 'int'
        if type(node.value) == float:
            return 'float'

    def visit_While(self, node):
        self.visit(node.cond)
        self.loop_number += 1
        self.visit(node.stmt)
        self.loop_number -= 1

    def visit_For(self, node):
        self.visit(node.id)
        self.visit(node.range)
        self.loop_number += 1
        self.visit(node.inst)
        self.loop_number -= 1

    def visit_Range(self, node):
        from_type = self.visit(node.range_from)
        if from_type != 'undeclared' and from_type != 'int':
            print("Range from should evaluate to int")
            self.error_found = 1
        to_type = self.visit(node.range_to)
        if to_type != 'undeclared' and to_type != 'int':
            print("Range to should evaluate to int")
            self.error_found = 1

    def visit_Continue(self, node):
        if self.loop_number <= 0:
            print("Continue used outside loop")
            self.error_found = 1

    def visit_Break(self, node):
        if self.loop_number <= 0:
            print("Break used outside loop")
            self.error_found = 1

    def visit_Condition(self, node):
        if self.returnedTypeRelative[self.visit(node.left)][self.visit(node.right)] == 'err':
            print("TYPE MISMATCH IN CONDITION\n")
            self.error_found = 1
        if self.returnedTypeRelative[self.visit(node.left)][self.visit(node.right)] != 'int':
            print("CONDITION MUST BE INT")
            self.error_found = 1

    def visit_ComInstructions(self, node):
        # self.tmp_dec = self.where_declared
        # self.comp = 1
        self.visit(node.instrs)
        # self.comp = 0
        # self.where_declared = self.tmp_dec

    def visit_BinExpr(self, node):
        type1 = self.visit(node.left)
        type2 = self.visit(node.right)
        op    = node.op;
        if self.returnedType[type1][type2][op] == 'err':
            print("TYPE MISMATCH IN BIN EXPR\n")
            self.error_found = 1;
        return self.returnedType[type1][type2][op]

    # def visit_Variable(self, node):
    #     if node.ID in self.where_declared.keys():
    #         if self.current_function in self.where_declared[node.ID].keys():
    #             return self.where_declared[node.ID][self.current_function]
    #         elif '*' in self.where_declared[node.ID].keys():
    #             return self.where_declared[node.ID]['*']
    #         else:
    #             print("UNDECLARED VARIABLE IN THIS FUN")
    #             self.error_found = 1
    #     else:
    #         print("UNDECLARED VARIABLE", node.ID)
    #         self.error_found = 1

# to jest do poprawy
    def visit_Variable(self, node):
        return 'int'

    def visit_Return(self, node):
        self.visit(node.ret)


import numpy as np
from numpy import dtype, uint8

class lena(object):
    def __init__(self, pallete):
        self.pallete = pallete

    # Open the file for reading
    def read_file(self, my_file):
        stream = open(my_file, 'rb')
        img = np.fromfile(stream, dtype=(uint8, 3))
        return img

    # Create a dither matrix
    def dither_matrix_row(self, row, mat):
        my_row = []
        for i in range(128):
            for j in mat[row]:
                my_row.append(j)
        return my_row

    # Span the 4 X 4 dither matrix from the book in 512 X 512
    def dither_matrix(self, first, second, third, fourth):
        dither = []
        total_length = 0
        all_rows = [first, second, third, fourth]
        for i in range(len(all_rows)):
            total_length += len(all_rows[i])

        for i in range(128):
            dither.append(first)
            dither.append(second)
            dither.append(third)
            dither.append(fourth)
        dither = np.reshape(dither, (-1, 512))
        return dither

    # Transform the image
    def quantize(self, img_array, dither_array):
        quantized_array = []
        for i in range(512):
            for j in range(512):
                if img_array[i][j] > dither_array[i][j]:
                    quantized_array.append(255)
                else:
                    quantized_array.append(0)
        temp_array = np.zeros(len(quantized_array), dtype=(uint8))
        for i in range(len(quantized_array)):
            temp_array[i] = quantized_array[i]
        return temp_array

    # Combine all three red, green and blue
    def combine_rgb(self, r, g, b):
        rgb = np.zeros(len(r), dtype=(uint8, 3))
        for i in range(len(r)):
            rgb[i][0] = r[i]
            rgb[i][1] = g[i]
            rgb[i][2] = b[i]
        return rgb

    # Scale dither matrix from 0 to 255 (8 bit) red and green
    # Add 1 to the dither multiply by 16 and subtract 1
    def scaled_dither(self, dither):
        dither = np.add(dither, 1)
        dither = np.dot(dither, 16)
        dither = np.add(dither, -1)
        return dither

if __name__ == '__main__':
    # 4 X 4 dither matrix from the book
    pallete = np.array([
        [0, 8, 2, 10],
        [12, 4, 14, 6],
        [3, 11, 1, 9],
        [15, 7, 13, 5]]
        )
    # Instantiate lena with the pallete
    lena = lena(pallete)

    # File must be a .data extension
    file_to_open = raw_input('Enter a .data file: ')

    #img = lena.read_file('LennaRGB512.data')
    img = lena.read_file(file_to_open)

    # Initialize red, green, blue arrays with zeros
    red = np.zeros(len(img), dtype=(uint8))
    green = np.zeros(len(img), dtype=(uint8))
    blue = np.zeros(len(img), dtype=(uint8))

    # Create the three channels for red, green and blue
    for i in range(len(img)):
        red[i] = img[i][0]    # Red channel
        green[i] = img[i][1]  # Green channel
        blue[i] = img[i][2]   # Blue channel

    # Convert linear arrays to 512 X 512
    red = np.reshape(red, (-1, 512))
    green = np.reshape(green, (-1, 512))
    blue = np.reshape(blue, (-1, 512))

    # Populate pallete along rows
    first = lena.dither_matrix_row(0, pallete)
    second = lena.dither_matrix_row(1, pallete)
    third = lena.dither_matrix_row(2, pallete)
    fourth = lena.dither_matrix_row(3, pallete)

    # Populate entire matrix using rows
    my_dither = lena.dither_matrix(first, second, third, fourth)

    # Scale dither matrix from 0 to 255 (8 bit)
    my_scaled_dither = lena.scaled_dither(my_dither)

    # Quantize the arrays
    quantized_lena_red = lena.quantize(red, my_scaled_dither)
    quantized_lena_green = lena.quantize(green, my_scaled_dither)
    quantized_lena_blue = lena.quantize(blue, my_scaled_dither)

    # Combine three layers of red, green and blue
    quantized_lena_rgb = lena.combine_rgb(
        quantized_lena_red,
        quantized_lena_green,
        quantized_lena_blue
        )

    # Save the new file
    quantized_lena_rgb.tofile(file_to_open + '_3bit.data')


from jwst.stpipe import Step
from jwst import datamodels
from ..datamodels import TMTDarkModel
from . import dark_sub

from ..utils.subarray import get_subarray_model

__all__ = ["DarkCurrentStep"]

class DarkCurrentStep(Step):
    """
    DarkCurrentStep: Performs dark current correction by subtracting
    dark current reference data from the input science data model.
    """

    spec = """
        dark_output = output_file(default = None) # Dark model subtracted
    """

    reference_file_types = ["dark"]

    def process(self, input):

        # Open the input data model
        with datamodels.open(input) as input_model:

            # Get the name of the dark reference file to use
            self.dark_name = self.get_reference_file(input_model, "dark")
            self.log.info("Using DARK reference file %s", self.dark_name)

            # Check for a valid reference file
            if self.dark_name == "N/A":
                self.log.warning("No DARK reference file found")
                self.log.warning("Dark current step will be skipped")
                result = input_model.copy()
                result.meta.cal_step.dark = "SKIPPED"
                return result

            # Create name for the intermediate dark, if desired.
            dark_output = self.dark_output
            if dark_output is not None:
                dark_output = self.make_output_path(
                    None, basepath=dark_output, ignore_use_model=True
                )

            # Open the dark ref file data model - based on Instrument
            dark_model = TMTDarkModel(self.dark_name)

            dark_model = get_subarray_model(input_model, dark_model)

            # Do the dark correction
            result = dark_sub.do_correction(input_model, dark_model, dark_output)
            dark_model.close()

        return result


# Import the libraries
import cv2
import numpy as np
import math as m
from matplotlib import pyplot as plt

#-- PRE-PROCESSING --
# Read the image
nimg = 'image1' # Change 'image1' for the name of your image
image = cv2.imread(nimg + '.jpg')
# Extract the RGB layers of the image
rgB = np.matrix(image[:,:,0]) # Blue
rGb = np.matrix(image[:,:,1]) # Green
Rgb = np.matrix(image[:,:,2]) # Red
# Define the combination RGB
II = cv2.absdiff(rGb,rgB)
I = II*255
cv2.imshow('Images with layers extracted', I)
cv2.waitKey(0)
# Initial binarization of the image
[fil, col] = I.shape
for o in range(0,fil):
    for oo in range(0,col):
        if I[o, oo]<80:      # Pixel less than 80 will be 0
            I[o,oo]=0
for o in range(0,fil):
    for oo in range(0,col):
        if I[o, oo]>0:       # Pixel more than 0 will be 1
            I[o,oo]=1
# Morphological transformations
# Create square streel: se for closing and se2 for dilation
se = np.ones((50, 50), np.uint8)
se2 = np.ones((10, 10), np.uint8)
closing = cv2.morphologyEx(I,cv2.MORPH_CLOSE,se) # Closing
dilation = cv2.dilate(closing,se2,1)             # Dilation
# Find the contours
contours,hierarchy=cv2.findContours(dilation,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Extract the contours
cnt = contours[:]
num = len(cnt)
# print(num)
# print(contours)
# print(hierarchy)
# Calculate the bigger contour
box = np.zeros((num,4))
for j in range(0, num):
    box[j,:]=cv2.boundingRect(cnt[j])
L = np.zeros((num,4))
Max=[0,0]
for j in range(0, num):
    L[j,:]=box[j]
    if L[j,2]>Max[1]:
        Max=[j,L[j,2]]
BOX = box[Max[0],:]
# Mask
b = image[int(BOX[1]):int(BOX[1]+BOX[3]),int(BOX[0]):int(BOX[0]+BOX[2]),:]

#-- SEGMENTATION --
[fil,col,cap] = b.shape
# Extract the RGB layers of the image with the mask
rgB = b[:,:,0] # Blue
rGb = b[:,:,1] # Green
Rgb = b[:,:,2] # Red
# Normalizate the layers
R = Rgb/255.0
G = rGb/255.0
B = rgB/255.0
# Build the color K space
K = np.zeros((fil,col))                    # Black layer
for o in range(0,fil):
    for oo in range(0,col):
        MAX = max(R[o,oo],G[o,oo],B[o,oo]) # Calculate the maximum value R-G-B
        K[o,oo] = 1-MAX
# Save the image in .bmp format
cv2.imwrite('imgbmp_' + nimg + '.bmp', K)
# Read the image
k = cv2.imread('imgbmp_' + nimg + '.bmp')
# Apply Canny
BW1 = cv2.Laplacian(k, cv2.CV_8UC1)
# Extract layers
imgk = BW1[:,:,0]+BW1[:,:,1]+BW1[:,:,2]
# Save the image
cv2.imwrite('result_' + nimg + '.png', imgk*255)


"""
递归动态规划
"""
class Solution(object):
    def canJump(self,nums):
        if len(nums) == 1:
            return True
        for i in range(1,nums[0]+1):
            if i <= len(nums):
                if self.canJump(nums[i:]):
                    return True
            else:
                return True
        return False

"""
备忘录递归动态规划
"""
class Solution(object):
    def canJump(self,nums):
        n = len(nums)
        if n == 1:
            return True
        a = [0] * n
        a[n-1] = 1
        position = 0
        return self.canJumps(nums,a,position)

    def canJumps(self,nums,a,position):
        print(nums[position])
        if a[position] != 0:
            print(1)
            if a[position] == 1:
                return True
            else:
                return False
        print(position)
        furjump = min(nums[position]+position,len(nums)-1)
        for i in range(position+1,furjump+1):
            if i <= len(nums):
                if self.canJumps(nums,a,i):
                    a[i] = 1
                    return True
            else:
                return True
        a[position]=-1
        return False
"""
自底向上动态规划
"""
class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        n = len(nums)
        if n == 1:
            return True
        memo = [0] * n
        memo[n-1] = 1
        m = n - 2
        while m>=  0:
            furjump = min(m+nums[m],n)
            print(m,furjump)
            for j in range(m+1,furjump+1):
                if (memo[j] == 1):
                    memo[m]= 1
                    break
            m = m -1
        print(memo)
        return memo[0] == 1
"""
贪心算法
"""
class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        n = len(nums) - 2
        lasted = len(nums)- 1
        while n >= 0:
            if (n + nums[n]>= lasted):
                lasted= n
            n = n - 1
        return lasted== 0


from fastapi import status, HTTPException, Depends
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from datetime import datetime, timedelta
from sqlalchemy.orm import Session

from .schema import TokenData
from app import database, models
from .config import env

SECRET_KEYS = env.SECRET_KEY
ALGORITHM = env.ALGORITHM
ACCESS_TOKEN_EXPIRE_MINUTES = env.ACCESS_TOKEN_EXPIRE_MINUTES

oauth2_schema = OAuth2PasswordBearer(tokenUrl="users/login")

def create_access_token(payload: dict):
    to_encode = payload.copy()
    expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    to_encode.update({"exp": expire})
    token = jwt.encode(to_encode, SECRET_KEYS, algorithm=ALGORITHM)
    return token

def verify_access_token(token: str, credentials_exception):
    try:
        payload = jwt.decode(token, SECRET_KEYS, algorithms=[ALGORITHM])
        id: str = payload.get("user_id")
        if id is None:
            raise credentials_exception
        token_data = TokenData(id=id)
    except JWTError:
        raise credentials_exception
    return token_data

def get_current_user(
        token: str = Depends(oauth2_schema),
        db: Session = Depends(database.get_db)
):

    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Auth": "Bearer"}
    )
    token = verify_access_token(token, credentials_exception)
    user = db.query(models.User).filter(models.User.id == token.id).first()

    return user


from typing import List
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        new_array = [1]*len(nums)
        for i in range(len(nums)):
            new_p = (i - k)%len(nums)
            new_array[i] = nums[new_p]
        return new_array

s = Solution()

l = [1,2,3,4,5,6,7]
x = s.rotate(l,3)
print(x)


#!/usr/bin/env python3

import os
import configparser
from mongoengine.connection import connect
from .data_model import Post
from .render_template import render
from .mailgun_emailer import send_email

def email_last_scraped_date():
    ## mongodb params (using configparser)
    config = configparser.ConfigParser()
    config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'settings.cfg'))
    mlab_uri = config.get('MongoDB', 'mlab_uri')

    # connect to db
    MONGO_URI = mlab_uri
    connect('sivji-sandbox', host=MONGO_URI)

    ## get the last date the webscraper was run
    for post in Post.objects().fields(date_str=1).order_by('-date_str').limit(1):
        day_to_pull = post.date_str

    ## pass in variables, render template, and send
    context = {
        'day_to_pull': day_to_pull,
        'Post': Post,
    }
    html = render("template.html", context)
    send_email(html)


#!/usr/bin/env python3
'''
This script will incement the major version number of the specified products.
It is assumed that the version number in the label itself is correct, and the version
just needs to be added on to the filename.

Usage:

versioning.py <label_file>...
'''

import re
import os
import sys
from bs4 import BeautifulSoup

LABELFILE_PARSE_VERSIONED_REGEX = r'(.+)_(\d+)_(\d+)\.xml'
LABELFILE_PARSE_UNVERSIONED_REGEX = r'(.+)\.xml'

DATAFILE_PARSE_VERSIONED_REGEX = r'(.+)_(\d+)\.([a-z0-9]+)'
DATAFILE_PARSE_UNVERSIONED_REGEX = r'(.+)\.([a-z0-9]+)'

def main(argv=None):
    ''' Entry point into the script '''
    if argv is None:
        argv = sys.argv
    filepaths = argv[1:]

    for filepath in filepaths:
        dirname, filename = os.path.split(filepath)
        increment_product(dirname, filename)

def increment_product(path, labelfile):
    '''
    Increments the version number of the specified product
    '''
    label = read_label(path, labelfile)
    datafile = extract_datafile(label)

    new_labelfile = increment_labelfile(labelfile)

    if datafile:
        new_datafile = increment_datafile(datafile)
        contents = inject_datafile(label, datafile, new_datafile)
        with open(new_labelfile, "w") as outfile:
            outfile.write(contents)
        rename(path, datafile, new_datafile)
    else:
        rename(path, labelfile, new_labelfile)

def read_label(path, labelfile):
    '''
    Reads in a product label file as a string
    '''
    with open(os.path.join(path, labelfile)) as infile:
        return infile.read()

def extract_datafile(label):
    ''' Finds the data filename referenced in a product '''
    soup = BeautifulSoup(label, 'lxml-xml')
    if soup.Product_Observational:
        return extract_observational_datafile(soup.Product_Observational)
    if soup.Product_Collection:
        return extract_collection_datafile(soup.Product_Collection)
    if soup.Product_Document:
        return extract_document_datafile(soup.Product_Document)
    return None

def extract_collection_datafile(product):
    ''' Finds the inventory filename referenced in a collection product '''
    file_area = product.File_Area_Inventory if product else None
    file_element = file_area.File if file_area else None
    file_name = file_element.file_name if file_element else None
    return file_name.string

def extract_observational_datafile(product):
    ''' Finds the data filename referenced in a product '''
    file_area = product.File_Area_Observational if product else None
    file_element = file_area.File if file_area else None
    file_name = file_element.file_name if file_element else None
    return file_name.string

def extract_document_datafile(product):
    ''' Finds the document filename referenced in a document product. '''
    document = product.Document if product else None
    document_edition = document.Document_Edition if document else None
    document_file = document_edition.Document_File if document_edition else None
    file_name = document_file.document_file if document_file else None
    return file_name.string

def increment_labelfile(labelfile):
    ''' Creates the filename for a label file with a new version number '''

    (filebase, major, _) = parse_labelfile_name(labelfile)
    newmajor, newminor = major + 1, 0
    return "{}_{}_{}.xml".format(filebase, newmajor, newminor)

def increment_datafile(datafile):
    ''' Creates the filename for a data file with the new version number '''
    (filebase, major, extension) = parse_datafile_name(datafile)
    newmajor = major + 1
    return "{}_{}.{}".format(filebase, newmajor, extension)

def inject_datafile(label, datafile, new_datafile):
    ''' Replaces the filename reference in a label with the specified file '''
    return label.replace(datafile, new_datafile)

def rename(dirname, filename, newfilename):
    ''' Renames a file '''
    src = os.path.join(dirname, filename)
    dst = os.path.join(dirname, newfilename)

    if os.path.exists(newfilename):
        print("File already exists: " + newfilename)
    else:
        os.rename(src, dst)

def parse_datafile_name(name):
    ''' Extract the version number from a data file, if available '''
    versioned_match = re.match(DATAFILE_PARSE_VERSIONED_REGEX, name)
    if versioned_match:
        (filebase, major, extension) = versioned_match.groups()
        return (filebase, int(major), extension)

    unversioned_match = re.match(DATAFILE_PARSE_UNVERSIONED_REGEX, name)
    (filebase, extension) = unversioned_match.groups()
    return (filebase, 1, extension)

def parse_labelfile_name(name):
    ''' Extract the version number from a label file, if available '''
    versioned_match = re.match(LABELFILE_PARSE_VERSIONED_REGEX, name)
    if versioned_match:
        (filebase, major, minor) = versioned_match.groups()
        return (filebase, int(major), int(minor))

    unversioned_match = re.match(LABELFILE_PARSE_UNVERSIONED_REGEX, name)
    filebase = unversioned_match.groups()[0]
    return (filebase, 1, 0)

def increment_major(major, _):
    '''
    Returns the version number with the major version incremented,
    and the minor version reset to 1
    '''
    return (major + 1, 0)

def increment_minor(major, minor):
    ''' Returns the version number with the minor version incremented '''
    return (major, minor + 1)

def attach_version_to_datafile(filebase, extension, major):
    ''' Creates a version of a filename that includes the version number '''
    return '{filebase}_{major}.{extension}'.format(
        major=major,
        filebase=filebase,
        extension=extension
    )

def attach_version_to_labelfile(filebase, major, minor):
    ''' Creates a version of a label filename that includes the version number '''
    return '{filebase}_{major}_{minor}.xml'.format(
        filebase=filebase,
        major=major,
        minor=minor
    )

if __name__ == '__main__':
    sys.exit(main())


import os
import string

def file_check(filepath, mode):
    if os.path.exists(filepath):
        if os.path.isfile(filepath):
            f = open("%s" % filepath, "%s" % mode)
            return f
        else:
            return "Incorrect file"
    else:
        return "Incorrect file"

print(file_check("kivy_test.py", "r"))

def decorator(func_to_decorate):
    def f():
        print(func_to_decorate.__name__)
        os.uname()
        return func_to_decorate()

    return f

@decorator
def adding():
    return 5 + 9

print(adding())

def formatting(dir_path, exit_format):
    a = exit_format.replace(str(k for k in string.digits), "").split(".")
    if os.path.isdir(dir_path):
        arr = os.listdir(dir_path)
        for i in range(len(arr)):
            if arr[i].replace(str(k for k in string.digits), "").split(".") != \
                    exit_format.replace(str(k for k in string.digits), "").split("."):
                if 99 > len(arr) > 9:
                    if i < 9:
                        os.rename(arr[i], "%s" % a[0] + "0" + "%s" % (i + 1) + "." + "%s" % a[1])
                    else:
                        os.rename(arr[i], "%s" % a[0] + "0" + "%s" % (i + 1) + "." + "%s" % a[1])
                if 999 > len(arr) > 99:
                    if i < 99:
                        os.rename(arr[i], "%s" % a[0] + "00" + "%s" % (i + 1) + "." + "%s" % a[1])
                    else:
                        os.rename(arr[i], "%s" % a[0] + "%s" % (i + 1) + "." + "%s" % a[1])
                if len(arr) > 999:
                    if i < 999:
                        os.rename(arr[i], "%s" % a[0] + "000" + "%s" % (i + 1) + "." + "%s" % a[1])
                    else:
                        os.rename(arr[i], "%s" % a[0] + "%s" % (i + 1) + "." + "%s" % a[1])

formatting("/home/sirius/fotos", "image000.jpg")


'''A module for demo-ing exceptions'''
import sys
from math import log

def convert_to_int(s):
	x = -1
	try:
		return int(s)
		print("Conversion succeeded! x =", x)
	except (ValueError,TypeError) as e:
		print("Conversion error: {}".format(str(e)), file=sys.stderr)
		return -1

def string_log(s):
	v = convert_to_int(s)
	return log(v)

def square_root(x):
	guess = x
	i = 0

	while guess * guess != x and i < 20:
		guess = (guess + x / guess) / 2.0
		i += 1
	return guess

def main():
	print(square_root(9))
	print(square_root(2))
	print(square_root(64))
	try:
		print(square_root(-1))
	except ZeroDivisionError:
		print("Cannot compute the square_root of a negative numero")
	print("Program execution continues normally here")

if __name__ == '__main__':
	main()


from src.components import summarizer
from celery import Celery
from celery.utils.log import get_task_logger
from EmailSender import send_email

logger = get_task_logger(__name__)
celery = Celery(
    __name__, backend="redis://127.0.0.1:6379", broker="redis://127.0.0.1:6379"
)

@celery.task(name="summarizer")
def GmailSummarizer(gmails, email_address):
    responses = []
    for gmail in gmails:
        gmail_summary = summarizer.summarize(gmail)
        responses.append(gmail_summary)
    send_email(responses, email_address)
    return True

"""
 run celery and also redis
# celery -A flask_celery.celery worker -l info --pool=solo
Compile Celery with --pool=solo argument. #IMP
# celery -A flask_celery.celery worker -l info --pool=solo
Example: celery -A your-application worker -l info --pool=solo
"""


#!/usr/bin/env python3

import logging
import sys
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, MediumMotor
from ev3dev2.control.rc_tank import RemoteControlledTank

log = logging.getLogger(__name__)

class TRACK3R(RemoteControlledTank):
    """
    Base class for all TRACK3R variations. The only difference in the child
    classes are in how the medium motor is handled.

    To enable the medium motor toggle the beacon button on the EV3 remote.
    """

    def __init__(self, medium_motor, left_motor, right_motor):
        RemoteControlledTank.__init__(self, left_motor, right_motor)
        self.medium_motor = MediumMotor(medium_motor)
        self.medium_motor.reset()

class TRACK3RWithBallShooter(TRACK3R):

    def __init__(self, medium_motor=OUTPUT_A, left_motor=OUTPUT_B, right_motor=OUTPUT_C):
        TRACK3R.__init__(self, medium_motor, left_motor, right_motor)
        self.remote.on_channel1_beacon = self.fire_ball

    def fire_ball(self, state):
        if state:
            self.medium_motor.run_to_rel_pos(speed_sp=400, position_sp=3*360)
        else:
            self.medium_motor.stop()

class TRACK3RWithSpinner(TRACK3R):

    def __init__(self, medium_motor=OUTPUT_A, left_motor=OUTPUT_B, right_motor=OUTPUT_C):
        TRACK3R.__init__(self, medium_motor, left_motor, right_motor)
        self.remote.on_channel1_beacon = self.spinner

    def spinner(self, state):
        if state:
            self.medium_motor.run_forever(speed_sp=50)
        else:
            self.medium_motor.stop()

class TRACK3RWithClaw(TRACK3R):

    def __init__(self, medium_motor=OUTPUT_A, left_motor=OUTPUT_B, right_motor=OUTPUT_C):
        TRACK3R.__init__(self, medium_motor, left_motor, right_motor)
        self.remote.on_channel1_beacon = self.move_claw

    def move_claw(self, state):
        if state:
            self.medium_motor.run_to_rel_pos(speed_sp=200, position_sp=-75)
        else:
            self.medium_motor.run_to_rel_pos(speed_sp=200, position_sp=75)


import unittest

from task import fix_encoding

expected_content = """Roses are räd.
Violets aren't blüe.
It's literally in the name.
They're called violets.
"""

filename = "example.txt"
output = "output.txt"

class TestCase(unittest.TestCase):
    def setUp(self) -> None:
        with open(filename, "w") as f:
            f.write(expected_content)

    def test_fix_encoding(self):
        fix_encoding(filename, output)
        with open(filename, "r", encoding="utf-8") as file:
            actual_content = file.read()

        self.assertEqual(actual_content, expected_content, "wrong answer")

Strings, inputs and files/file encoding/tests/test_task.py


#Inicio do While
opcao = -1

#Variaveis do Saque, máximo do saque 500 por saque e até 3x por dia
limiteSaque = 500
saqueDia = 3
valorSacado = 0

#Variaveis do Saldo
saldo = float(0)

#Variaveis Deposito
deposito = float(0)
while opcao != 0:
    opcao = int(input(" [1] Para sacar \n [2] Para depositar \n [3] Para ver saldo \n [0] Para sair \n :"))
    #Opção de Saque
    if opcao == 1 :
      saque = float(input("Digite o valor a ser sacado: "))
      if saldo <= 0 :
        print ("Conta zerado, não é possivel realizar o saque")
        continue
      if saldo < saque:
         print("Não há fundos suficientes para realizar o saque!!!")
         continue
      if saque > 500:
         print ("Valor indisponivel para saque, somente saques abaixo de 500")
         continue
      if saque <= 0:
         print("Erro, valor impossivel de sacar")
      if   saqueDia <= 0:
         print("Sem saques diarios restantes")
         continue
      else:
         saqueDia -= 1
         saldo -= saque
         print("Valor sacado com sucesso.")
    #Opção de Deposito
    elif opcao == 2 :
        deposito = float(input("Digite o valor do deposito: "))
        if deposito > 0:
           saldo += deposito
           print("Deposito realizado com sucesso.")
        else:
           print("Deposito falhou, valor invalido!!!")
    #Opção de Extrato
    elif opcao == 3 :
        print("Imprimindo extrato, só um momento:")
        print(saldo)
    else:
        print("Obrigado por usar nosso sistema, operação concluida")


import matplotlib.pyplot as plt
import time
import numpy as np
from PIL import Image

class graphic_display():
    def __init__(self):
        self.um_per_pixel = 0.5
        self.cm_hot       = plt.get_cmap('hot')
        self.cm_jet       = plt.get_cmap('jet')
        self.cm_vir       = plt.get_cmap('viridis')
        self.cm_mag       = plt.get_cmap('magma')
        # self.cm_grn       = plt.get_cmap('Greens')
        self.cm_raw       = plt.get_cmap('gray')
        self.fps_counter  = np.array([time.time(),time.time(),time.time()])
        self.img_rs       = None
        self.img_norm     = None
        self.img_gamma    = None
        self.img_p        = None
        self.img_cm       = None
        self.img_sb       = None
        self.img_fin      = None
        self.win          = None
        self.cam          = None
        return

    def update_win(self, win):
        self.win = win

    def update_cam(self, cam):
        self.cam = cam

    def update_image(self):
        print('update image function')
        self.img_rs = np.array(Image.fromarray(self.cam.img_raw).resize(size=(958, 638)),dtype = 'float64')/255
        if self.win.zoom_factor > 1:
            r1 = self.img_rs.shape[0]
            c1 = self.img_rs.shape[1]
            r2 = int(np.round(r1/self.win.zoom_factor))
            c2 = int(np.round(c1/self.win.zoom_factor))
            self.img_rs = self.img_rs[int((r1-r2)/2):int((r1-r2)/2)+r2, int((c1-c2)/2):int((c1-c2)/2)+c2]

        # update and process the image for display from the camera
        self.update_image_gamma()
        self.normalise_img()
        self.update_colormap()
        self.display_saturated_pixels_purple() ### error
        self.burn_scalebar_into_image()

        # gui functions
        self.win.repaint_image() ### may zoom in twice for raw image, need double check
        self.win.update_hist()
        # self.win.image_histogram.update_histogram() # method in histogram_canvas class
        self.win.status_text_update_image()

        # fps counter
        self.fps_counter = np.append(self.fps_counter,time.time())
        self.fps_counter = np.delete(self.fps_counter, 0)
        self.win.status_fps_number.setText(str(np.round(1/np.mean(np.diff(self.fps_counter)),5)))
        print('current saved value for fps is: ' + str(self.cam.fps) + ' current timer value is: ' + str(self.cam.timer_value))
        return

    def update_image_gamma(self):

        if self.win.gamma == 1:
            self.img_gamma = self.img_rs
        else:
            self.img_gamma = self.img_rs**self.win.gamma
        return

    def normalise_img(self):
        print('normalise function')
        if self.win.cbox_normalise.isChecked():
            imgnormmin = np.min(np.nonzero(self.img_gamma))
            imgnormmax = np.max(self.img_gamma)
            self.img_norm = (self.img_gamma-imgnormmin)/(imgnormmax--imgnormmin)
            self.img_norm = self.img_norm
        else:
            self.img_norm = self.img_gamma
        return

    def update_colormap(self):
        print('update colormap function')
        # convert from gray to colormap magma selection
        if self.win.combobox_colourmap.currentIndex() == 0:
            self.img_cm = self.cm_mag(self.img_norm)
        # convert from gray to colormap green selection
        elif self.win.combobox_colourmap.currentIndex() == 1:
            self.img_cm = np.zeros(np.hstack([np.shape(self.img_norm),4]))
            self.img_cm[:,:,1] = self.img_norm
            self.img_cm[:,:,3] = 255
            ## or use Greens colormap directly
            # self.img_cm = self.cm_grn(self.img_norm)
        # convert from gray to colormap viridis (3 channel) selection
        elif self.win.combobox_colourmap.currentIndex() == 2:
            self.img_cm = self.cm_vir(self.img_norm)
        # convert from gray to colormap jet selection
        elif self.win.combobox_colourmap.currentIndex() == 3:
            self.img_cm = self.cm_jet(self.img_norm)
        elif self.win.combobox_colourmap.currentIndex() == 4:
            # self.img_cm = np.zeros(np.hstack([np.shape(self.img_norm),4]))
            # self.img_cm[:,:,0] = self.img_norm
            # self.img_cm[:,:,1] = self.img_norm
            # self.img_cm[:,:,2] = self.img_norm
            # self.img_cm[:,:,3] = 1
            # print(self.img_cm)
            # print(self.cam.img_raw)
            ## or use gray colormap directly
            self.img_cm = self.cm_raw(self.img_norm)
        return

    def display_saturated_pixels_purple(self):
        print('saturated pxls purple function')
        # saturated pixels show up purple if check box is selected
        # if self.win.combobox_colourmap.currentIndex() != 4:
        self.img_p = self.img_cm
        if self.win.cbox_saturated.isChecked():
            ind = self.img_norm > 254
            self.img_p[ind,0] = 255
            self.img_p[ind,1] = 0
            self.img_p[ind,2] = 255
        return

    def burn_scalebar_into_image(self):
        print('burn scalebar function')
        self.img_sb = self.img_p
        if self.win.cbox_show_scalebar.isChecked():
            s = self.img_sb.shape
            if self.win.combobox_colourmap.currentIndex() == 1:
                self.img_sb[int(s[0]*0.95):int(s[0]*0.955), int(s[1]*0.05):int(s[1]*0.05+100/self.um_per_pixel), 0] = 255
                self.img_sb[int(s[0]*0.95):int(s[0]*0.955), int(s[1]*0.05):int(s[1]*0.05+100/self.um_per_pixel), 1] = 0
                self.img_sb[int(s[0]*0.95):int(s[0]*0.955), int(s[1]*0.05):int(s[1]*0.05+100/self.um_per_pixel), 2] = 255
            else:
                self.img_sb[int(s[0]*0.95):int(s[0]*0.955), int(s[1]*0.05):int(s[1]*0.05+100/self.um_per_pixel), 0] = 0
                self.img_sb[int(s[0]*0.95):int(s[0]*0.955), int(s[1]*0.05):int(s[1]*0.05+100/self.um_per_pixel), 1] = 255
                self.img_sb[int(s[0]*0.95):int(s[0]*0.955), int(s[1]*0.05):int(s[1]*0.05+100/self.um_per_pixel), 2] = 0
        self.img_fin = self.img_sb
        self.img_fin = np.array(self.img_fin*255,dtype='uint8')
        return


import sys
sys.setrecursionlimit(10000)

def dfs(d, v, visited):
    visited[v]= True
    for i in d[v]:
        if not visited[i]:
            dfs(d, i, visited)

n,m = map(int, input().split())
d = [[] for _ in range(n+1)]
visited = [False]*(n+1)
result = 0

for _ in range(m):
    u,v = map(int, input().split())
    d[u].append(v)
    d[v].append(u)

for i in range(1,n+1):
    if not visited[i]:
        dfs(d,i,visited)
        result += 1
print(result)


import tensorflow as tf
import json
from model_provider import get_model
from utils.create_gan_tfrecords import TFRecordsGAN
from utils.augment_images import augment_autoencoder
import os
import tensorflow.keras as K
import datetime
import string
from losses import get_loss, gradient_penalty
import argparse

physical_devices = tf.config.experimental.list_physical_devices("GPU")
for gpu in physical_devices:
    tf.config.experimental.set_memory_growth(gpu, True)
mirrored_strategy = tf.distribute.MirroredStrategy()

args = argparse.ArgumentParser(description="Train a network with specific settings")
args.add_argument("-d", "--dataset", type=str, default="zebra2horse",
                  help="Name a dataset from the tf_dataset collection",
                  choices=["zebra2horse"])
args.add_argument("-opt", "--optimizer", type=str, default="Adam", help="Select optimizer",
                  choices=["SGD", "RMSProp", "Adam"])
args.add_argument("-lrs", "--lr_scheduler", type=str, default="constant", help="Select learning rate scheduler",
                  choices=["poly", "exp_decay", "constant"])
args.add_argument("-gm", "--gan_mode", type=str, default="constant", help="Select training mode for GAN",
                  choices=["normal", "wgan_gp"])
args.add_argument("-e", "--epochs", type=int, default=1000, help="Number of epochs to train")
args.add_argument("--lr", type=float, default=2e-4, help="Initial learning rate")
args.add_argument("--momentum", type=float, default=0.9, help="Momentum")
args.add_argument("-bs", "--batch_size", type=int, default=16, help="Size of mini-batch")
args.add_argument("-si", "--save_interval", type=int, default=5, help="Save interval for model")
args.add_argument("-m", "--model", type=str, default="cyclegan", help="Select model")
args.add_argument("-logs", "--logdir", type=str, default="./logs", help="Directory to save tensorboard logdir")
args.add_argument("-l_m", "--load_model", type=str,
                  default=None,
                  help="Load model from path")
args.add_argument("-s", "--save_dir", type=str, default="./cyclegan_runs",
                  help="Save directory for models and tensorboard")
args.add_argument("-tfrecs", "--tf_record_path", type=str, default="/data/input/datasets/tf2_gan_tfrecords",
                  help="Save directory that contains train and validation tfrecords")
args.add_argument("-sb", "--shuffle_buffer", type=int, default=1024, help="Size of the shuffle buffer")
args.add_argument("--width", type=int, default=286, help="Size of the shuffle buffer")
args.add_argument("--height", type=int, default=286, help="Size of the shuffle buffer")
args.add_argument("--c_width", type=int, default=256, help="Crop width")
args.add_argument("--c_height", type=int, default=256, help="Crop height")
args.add_argument("--random_seed", type=int, default=1, help="Set random seed to this if true")
args = args.parse_args()

tf_record_path = args.tf_record_path
dataset = args.dataset
BUFFER_SIZE = args.shuffle_buffer
BATCH_SIZE = args.batch_size
IMG_WIDTH = args.width
IMG_HEIGHT = args.height
CROP_HEIGHT = args.c_height if args.c_height < IMG_HEIGHT else IMG_HEIGHT
CROP_WIDTH = args.c_width if args.c_width < IMG_WIDTH else IMG_WIDTH
LAMBDA = 10
EPOCHS = args.epochs
LEARNING_RATE = args.lr
LEARNING_RATE_SCHEDULER = args.lr_scheduler
save_interval = args.save_interval
save_dir = args.save_dir
load_model_path = args.load_model
MODEL = args.model
gan_mode = args.gan_mode
time = str(datetime.datetime.now())
time = time.translate(str.maketrans('', '', string.punctuation)).replace(" ", "-")[:-8]
logdir = "{}_{}_e{}_lr{}_{}x{}_{}".format(time, MODEL, EPOCHS, LEARNING_RATE, IMG_HEIGHT, IMG_WIDTH, gan_mode)

train_A, train_B = \
    TFRecordsGAN(
        tfrecord_path=
        "{}/{}_train.tfrecords".format(tf_record_path, dataset + "_a")).read_tfrecords(), \
    TFRecordsGAN(
        tfrecord_path=
        "{}/{}_train.tfrecords".format(tf_record_path, dataset + "_b")).read_tfrecords()

with open(f"{args.tf_record_path}/data_samples.json") as f:
    data = json.load(f)
num_samples_ab = [data[dataset + "_a"], data[dataset + "_b"]]
if num_samples_ab[0] > num_samples_ab[1]:
    total_samples = num_samples_ab[0]
    train_B = train_B.repeat()
else:
    total_samples = num_samples_ab[1]
    train_A = train_A.repeat()

augmentor = lambda batch: augment_autoencoder(batch, size=(IMG_HEIGHT, IMG_WIDTH), crop=(CROP_HEIGHT, CROP_WIDTH))
train_A = train_A.map(
    augmentor, num_parallel_calls=tf.data.AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)

train_B = train_B.map(
    augmentor, num_parallel_calls=tf.data.AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)
train_A = mirrored_strategy.experimental_distribute_dataset(train_A)
train_B = mirrored_strategy.experimental_distribute_dataset(train_B)

if gan_mode == "wgan_gp":
    gan_loss_obj = get_loss(name="Wasserstein")
else:
    gan_loss_obj = get_loss(name="binary_crossentropy")
cycle_loss_obj = get_loss(name="MAE")
id_loss_obj = get_loss(name="MAE")

def discriminator_loss(real, generated):
    if gan_mode == "wgan_gp":
        real_loss = gan_loss_obj(-tf.ones_like(real), real)
        generated_loss = gan_loss_obj(tf.ones_like(generated), generated)
    else:
        real_loss = gan_loss_obj(tf.ones_like(real), real)
        generated_loss = gan_loss_obj(tf.zeros_like(generated), generated)
    total_disc_loss = generated_loss + real_loss
    return tf.reduce_mean(total_disc_loss) * 0.5

def generator_loss(generated):
    return tf.reduce_mean(
        gan_loss_obj(-tf.ones_like(generated), generated)) if gan_mode == "wgan_gp" else tf.reduce_mean(
        gan_loss_obj(tf.ones_like(generated), generated))

def calc_cycle_loss(real_image, cycled_image):
    loss1 = cycle_loss_obj(real_image, cycled_image)
    return loss1

def identity_loss(real_image, same_image):
    loss = id_loss_obj(real_image, same_image)
    return LAMBDA * 0.5 * loss

if LEARNING_RATE_SCHEDULER == "poly":
    lrs = K.optimizers.schedules.PolynomialDecay(LEARNING_RATE,
                                                 decay_steps=EPOCHS,
                                                 end_learning_rate=1e-8, power=0.8)
elif LEARNING_RATE_SCHEDULER == "exp_decay":
    lrs = K.optimizers.schedules.ExponentialDecay(LEARNING_RATE,
                                                  decay_steps=EPOCHS,
                                                  decay_rate=0.5)
else:
    lrs = LEARNING_RATE

with mirrored_strategy.scope():
    generator_g = get_model("{}_gen".format(MODEL), type="gan")
    generator_f = get_model("{}_gen".format(MODEL), type="gan")

    discriminator_x = get_model("{}_disc".format(MODEL), type="gan")
    discriminator_y = get_model("{}_disc".format(MODEL), type="gan")
    tmp = tf.cast(tf.random.uniform((1, CROP_HEIGHT, CROP_WIDTH, 3), dtype=tf.float32, minval=0, maxval=1),
                  dtype=tf.float32)
    generator_g(tmp), generator_f(tmp), discriminator_x(tmp), discriminator_y(tmp)
    generator_g_optimizer = tf.keras.optimizers.Adam(lrs, beta_1=0.5)
    generator_f_optimizer = tf.keras.optimizers.Adam(lrs, beta_1=0.5)

    discriminator_x_optimizer = tf.keras.optimizers.Adam(lrs, beta_1=0.5)
    discriminator_y_optimizer = tf.keras.optimizers.Adam(lrs, beta_1=0.5)

def load_models(models_parent_dir):
    assert os.path.exists(models_parent_dir), "The path {} is not valid".format(models_parent_dir)
    p_gen_g = K.models.load_model(os.path.join(models_parent_dir, "gen_g"))
    p_gen_f = K.models.load_model(os.path.join(models_parent_dir, "gen_f"))
    p_disc_x = K.models.load_model(os.path.join(models_parent_dir, "disc_x"))
    p_disc_y = K.models.load_model(os.path.join(models_parent_dir, "disc_y"))
    generator_g.set_weights(p_gen_g.get_weights())
    print("Generator G loaded successfully")
    generator_f.set_weights(p_gen_f.get_weights())
    print("Generator F loaded successfully")
    discriminator_x.set_weights(p_disc_x.get_weights())
    print("Discriminator X loaded successfully")
    discriminator_y.set_weights(p_disc_y.get_weights())
    print("Discriminator Y loaded successfully")

if load_model_path is not None:
    load_models(load_model_path)
    START_EPOCH = int(load_model_path.split("/")[-1])
else:
    START_EPOCH = 0

def write_to_tensorboard(g_loss_g, g_loss_f, d_loss_x, d_loss_y, c_step, writer):
    with writer.as_default():
        tf.summary.scalar("G_Loss_G", g_loss_g.numpy(), c_step)
        tf.summary.scalar("G_Loss_F", g_loss_f.numpy(), c_step)
        tf.summary.scalar("D_Loss_X", tf.reduce_mean(d_loss_x).numpy(), c_step)
        tf.summary.scalar("D_Loss_Y", tf.reduce_mean(d_loss_y).numpy(), c_step)
        if len(physical_devices) > 1:
            o_img_a = tf.cast(image_x.values[0], dtype=tf.float32)
            o_img_b = tf.cast(image_y.values[0], dtype=tf.float32)
            img_a, img_b = o_img_a, o_img_b
        else:
            img_a = image_x
            img_b = image_y
        # img_size_a, img_size_b = img_a.shape[1] * img_a.shape[2] * img_a.shape[3], img_b.shape[1] * img_b.shape[2] * \
        #                          img_b.shape[3]
        # mean_a, mean_b = tf.reduce_mean(img_a, axis=[1, 2, 3], keepdims=True), tf.reduce_mean(img_b, axis=[1, 2, 3],
        #                                                                                keepdims=True)
        # adjusted_std_a = tf.maximum(tf.math.reduce_std(img_a, axis=[1, 2, 3], keepdims=True),
        #                             1 / tf.sqrt(img_size_a / 1.0))
        # adjusted_std_b = tf.maximum(tf.math.reduce_std(img_b, axis=[1, 2, 3], keepdims=True),
        #                             1 / tf.sqrt(img_size_b / 1.0))
        f_image_y = generator_g(img_a, training=True)
        f_image_x = generator_f(img_b, training=True)
        confidence_a = discriminator_x(f_image_x, training=True)
        confidence_b = discriminator_y(f_image_y, training=True)
        tf.summary.image("img_a", tf.cast(127.5 * (img_a + 1), dtype=tf.uint8), step=c_step)
        tf.summary.image("img_b", tf.cast(127.5 * (img_b + 1), dtype=tf.uint8), step=c_step)
        tf.summary.image("fake_img_a", tf.cast((f_image_x + 1) * 127.5, dtype=tf.uint8), step=c_step)
        tf.summary.image("fake_img_b", tf.cast((f_image_y + 1) * 127.5, dtype=tf.uint8), step=c_step)
        tf.summary.image("confidence_a", confidence_a, step=c_step)
        tf.summary.image("confidence_b", confidence_b, step=c_step)

@tf.function
def train_step(real_x, real_y, n_critic=5):
    # real_x = tf.image.per_image_standardization(real_x)
    # real_y = tf.image.per_image_standardization(real_y)
    with tf.GradientTape(persistent=True) as tape:
        fake_y = generator_g(real_x, training=True)
        cycled_x = generator_f(fake_y, training=True)

        fake_x = generator_f(real_y, training=True)
        cycled_y = generator_g(fake_x, training=True)

        # same_x and same_y are used for identity loss.
        same_x = generator_f(real_x, training=True)
        same_y = generator_g(real_y, training=True)

        disc_real_x = discriminator_x(real_x, training=True)
        disc_real_y = discriminator_y(real_y, training=True)

        disc_fake_x = discriminator_x(fake_x, training=True)
        disc_fake_y = discriminator_y(fake_y, training=True)

        # calculate the loss
        gen_g_loss = generator_loss(disc_fake_y)
        gen_f_loss = generator_loss(disc_fake_x)

        total_cycle_loss = calc_cycle_loss(real_x, cycled_x) + calc_cycle_loss(real_y, cycled_y)

        # Total generator loss = adversarial loss + cycle loss
        total_gen_g_loss = LAMBDA * total_cycle_loss + identity_loss(real_y, same_y) + gen_g_loss
        total_gen_f_loss = LAMBDA * total_cycle_loss + identity_loss(real_x, same_x) + gen_f_loss

        if gan_mode != "wgan_gp":
            disc_x_loss = discriminator_loss(disc_real_x, disc_fake_x)
            disc_y_loss = discriminator_loss(disc_real_y, disc_fake_y)

    # ------------------- Disc Cycle -------------------- #
    if gan_mode == "wgan_gp":
        disc_x_loss, disc_y_loss = wgan_disc_apply(fake_x, fake_y, n_critic, real_x, real_y)
    # Calculate the gradients for generator and discriminator
    generator_g_gradients = tape.gradient(total_gen_g_loss,
                                          generator_g.trainable_variables)
    generator_f_gradients = tape.gradient(total_gen_f_loss,
                                          generator_f.trainable_variables)
    if gan_mode != "wgan_gp":
        discriminator_x_gradients = tape.gradient(disc_x_loss,
                                                  discriminator_x.trainable_variables)
        discriminator_y_gradients = tape.gradient(disc_y_loss,
                                                  discriminator_y.trainable_variables)

    # Apply the gradients to the optimizer
    generator_g_optimizer.apply_gradients(zip(generator_g_gradients,
                                              generator_g.trainable_variables))

    generator_f_optimizer.apply_gradients(zip(generator_f_gradients,
                                              generator_f.trainable_variables))
    if gan_mode != "wgan_gp":
        discriminator_x_optimizer.apply_gradients(zip(discriminator_x_gradients,
                                                  discriminator_x.trainable_variables))

        discriminator_y_optimizer.apply_gradients(zip(discriminator_y_gradients,
                                                      discriminator_y.trainable_variables))

    return total_gen_g_loss, total_gen_f_loss, disc_x_loss, disc_y_loss

def wgan_disc_apply(fake_x, fake_y, n_critic, real_x, real_y):
    for _ in range(n_critic):
        with tf.GradientTape(persistent=True) as disc_tape:
            disc_real_x = discriminator_x(real_x, training=True)
            disc_real_y = discriminator_y(real_y, training=True)

            disc_fake_x = discriminator_x(fake_x, training=True)
            disc_fake_y = discriminator_y(fake_y, training=True)
            disc_x_loss = discriminator_loss(disc_real_x, disc_fake_x) + 10 * gradient_penalty(real_x, fake_x,
                                                                                discriminator_x)
            disc_y_loss = discriminator_loss(disc_real_y, disc_fake_y) + 10 * gradient_penalty(real_y, fake_y,
                                                                                discriminator_y)

        discriminator_x_gradients = disc_tape.gradient(disc_x_loss,
                                                       discriminator_x.trainable_variables)
        discriminator_y_gradients = disc_tape.gradient(disc_y_loss,
                                                       discriminator_y.trainable_variables)
        discriminator_x_optimizer.apply_gradients(zip(discriminator_x_gradients,
                                                      discriminator_x.trainable_variables))

        discriminator_y_optimizer.apply_gradients(zip(discriminator_y_gradients,
                                                      discriminator_y.trainable_variables))
    return disc_x_loss, disc_y_loss

@tf.function
def distributed_train_step(dist_inputs_a, dist_inputs_b):
    per_replica_gen_g_losses, per_replica_gen_f_losses, per_replica_disc_x_losses, per_replica_disc_y_losses = \
        mirrored_strategy.run(train_step, args=(dist_inputs_a, dist_inputs_b))
    reduced_gen_g_loss, reduced_gen_f_loss = mirrored_strategy.reduce(tf.distribute.ReduceOp.MEAN,
                                                                      per_replica_gen_g_losses,
                                                                      axis=None), mirrored_strategy.reduce(
        tf.distribute.ReduceOp.MEAN, per_replica_gen_f_losses,
        axis=None)
    reduced_disc_x_loss, reduced_disc_y_loss = mirrored_strategy.reduce(tf.distribute.ReduceOp.MEAN,
                                                                        per_replica_disc_x_losses,
                                                                        axis=None), mirrored_strategy.reduce(
        tf.distribute.ReduceOp.MEAN, per_replica_disc_y_losses,
        axis=None)
    return reduced_gen_g_loss, reduced_gen_f_loss, reduced_disc_x_loss, reduced_disc_y_loss

train_writer = tf.summary.create_file_writer(os.path.join(args.logdir, logdir))

def save_models():
    K.models.save_model(generator_g, os.path.join(save_dir, MODEL, str(epoch + 1), "gen_g"))
    K.models.save_model(generator_f, os.path.join(save_dir, MODEL, str(epoch + 1), "gen_f"))
    K.models.save_model(discriminator_x, os.path.join(save_dir, MODEL, str(epoch + 1), "disc_x"))
    K.models.save_model(discriminator_y, os.path.join(save_dir, MODEL, str(epoch + 1), "disc_y"))
    print("Model at Epoch {}, saved at {}".format(epoch, os.path.join(save_dir, MODEL, str(epoch))))

for epoch in range(START_EPOCH, EPOCHS):
    print("\n ----------- Epoch {} --------------\n".format(epoch + 1))
    n = 0
    with train_writer.as_default():
        tf.summary.scalar("Learning Rate", lrs(epoch).numpy(),
                          epoch) if LEARNING_RATE_SCHEDULER != "constant" else tf.summary.scalar("Learning Rate", lrs,
                                                                                epoch)
    for image_x, image_y in zip(train_A, train_B):
        gen_g_loss, gen_f_loss, disc_x_loss, disc_y_loss = distributed_train_step(image_x, image_y)
        print(
            "Epoch {} \t Gen_G_Loss: {}, Gen_F_Loss: {}, Disc_X_Loss: {}, Disc_Y_Loss: {}".format(epoch + 1, gen_g_loss,
                                                                                gen_f_loss,
                                                                                disc_x_loss,
                                                                                disc_y_loss))
        n += 1
        if n % 20 == 0:
            write_to_tensorboard(gen_g_loss, gen_f_loss, disc_x_loss, disc_y_loss,
                                 (epoch * total_samples // BATCH_SIZE) + n, train_writer)
    if (epoch + 1) % save_interval == 0:
        save_models()


"""
Utility functions
"""
import torch
import matplotlib as mpl
import numpy as np
import math

mpl.use('Agg')
from matplotlib import pyplot as plt

def sin_data(n_train, n_test, noise_std, sort=False):
    """Create 1D sine function regression dataset
    :n_train: Number of training samples.
    :n_test: Number of testing samples.
    :noise_srd: Standard deviation of observation noise.
    :returns: x_train, y_train, x_test, y_test
    """

    def ground_truth(x):
        return torch.sin(math.pi * x)

    xn = torch.rand(n_train, 1) * 2 - 1  # Uniformly random in [-1, 1]
    yn = ground_truth(xn) + noise_std * torch.randn(n_train, 1)
    if sort:
        indices = torch.argsort(xn, axis=0)
        xn = xn[indices.squeeze()]
        yn = yn[indices.squeeze()]
    xt = torch.linspace(-1.1, 1.1, n_test).view(-1, 1)
    yt = ground_truth(xt) + noise_std * torch.randn(n_test, 1)
    return xn, yn, xt, yt

def plot_lengthscale(xt, lengthscale, uncertainty, name=None):
    """
    Visualize lengthscale function and its corresponding uncertainty.

    :lengthscale: Lengthscale mean.
    :uncertainty: Standard deviation of lengthscale prediction.
    """
    lengthscale = lengthscale.numpy().ravel()
    uncertainty = uncertainty.numpy().ravel()
    lower = lengthscale - 2.0 * uncertainty
    upper = lengthscale + 2.0 * uncertainty
    xt = xt.numpy().ravel()

    fig, ax = plt.subplots()
    ax.plot(xt, lengthscale, 'b', lw=2, alpha=0.8, label='Lengthscale')
    ax.fill_between(xt, lower, upper,
                    facecolor='b', alpha=0.3, label='95% CI')
    ax.set_xlim([xt.min(), xt.max()])
    ax.legend(loc='lower left', bbox_to_anchor=(0, 1.02, 1, 0.2), ncol=3,
              borderaxespad=0, frameon=False)
    if name is not None:
        fig.savefig('../results/prediction/' + name + '.svg')

def plot_pytorch(dataset, preds, name=None):
    dataset = [tensor.numpy().ravel() for tensor in dataset]
    xn, yn, xt, ft = dataset

    mean = preds.mean.cpu().numpy().ravel()
    lower, upper = preds.confidence_region()
    lower = lower.cpu().numpy().ravel()
    upper = upper.cpu().numpy().ravel()

    fig, ax = plt.subplots()
    ax.plot(xn, yn, 'k.', label='Training data')
    ax.plot(xt, ft, 'r--', lw=2, alpha=0.8, label='Function')
    ax.plot(xt, mean, 'b', lw=2, alpha=0.8, label='Prediction')
    ax.fill_between(xt, lower, upper,
                    facecolor='b', alpha=0.3, label='95% CI')
    ax.set_xlim([xt.min(), xt.max()])
    ax.legend(loc='lower left', bbox_to_anchor=(0, 1.02, 1, 0.2), ncol=3,
              borderaxespad=0, frameon=False)
    if name is not None:
        fig.savefig('../results/prediction/' + name + '.svg')

def plot_1d_results(dataset, mean, std, title=None, name=None):
    """
    Visualize training data, ground-truth function, and prediction.

    :dataset: A tuple containing (Xn, Yn, Xt, Ft)
    :mean: Mean of predictive Gaussian distribution.
    :std: Standard deviation of predictive Gaussian distribution.
    """
    dataset = [tensor.cpu().numpy().ravel() for tensor in dataset]
    xn, yn, xt, ft = dataset

    mean, std = mean.cpu().numpy().ravel(), std.cpu().numpy().ravel()
    lower = mean - 2.0 * std
    upper = mean + 2.0 * std

    fig, ax = plt.subplots()
    ax.plot(xn, yn, 'k.', label='Training data')
    ax.plot(xt, ft, 'r.', lw=2, alpha=0.8, label='Test data')
    ax.plot(xt, mean, 'b', lw=2, alpha=0.8, label='Prediction')
    ax.fill_between(xt, lower, upper,
                    facecolor='b', alpha=0.3, label='95% CI')
    ax.set_xlim([xt.min(), xt.max()])
    ax.legend(loc='lower left', bbox_to_anchor=(0, 1.02, 1, 0.2), ncol=3,
              borderaxespad=0, frameon=False)
    if title is not None:
        ax.set_title(title, loc='center')
    if name is not None:
        fig.savefig(name + '.pdf')

def train(model, optimizer, n_iter, verbose=True, name=None, Xn=None, yn=None, tol=None):
    """
    Training helper function.
    """
    n_train = Xn.size(0) if Xn is not None else model.Xn.size(0)
    losses = []
    for i in range(n_iter):
        optimizer.zero_grad()
        if Xn is None and yn is None:
            loss = model.loss()
        else:
            loss = model.loss(Xn, yn)
        loss.backward()
        optimizer.step()
        losses.append(loss.item())
        if tol is not None:
            # if the result is stable for over 50 iteration, then we consider it converges.
            n = 50
            if len(losses) > n:
                last = losses[-n:]
                if max(last) - min(last) <= tol:
                    if verbose:
                        print("Converges at iteration: ", i)
                    break
        if verbose:
            print('Iteration: {0:04d} Loss: {1: .6f}'.format(i, loss.item() / n_train))

    if name is not None:
        plt.figure()
        plt.plot(losses, lw=2)
        plt.ylabel('Loss')
        plt.xlabel('Number of iteration')
        plt.savefig(name + '.svg')


with open("inputs/day14.txt", 'r') as fh:
    lines = fh.readlines()

schedules = {}
for line in lines:
    parts = line.split()
    name = parts[0]
    speed = int(parts[3])
    duration = int(parts[6])
    rest = int(parts[13])
    schedule = []
    while len(schedule) < 2503:
        schedule += [speed, ] * duration
        schedule += [0, ] * rest
    schedules[name] = schedule

for reindeer in schedules:
    print("{} flew {} in 2503 seconds".format(reindeer, sum(schedules[reindeer][:2503])))

scores = {}
for reindeer in schedules:
    scores.update({reindeer: {"dist": 0, "score": 0}})

for sec in range(2503):
    for reindeer in scores:
        scores[reindeer]["dist"] += schedules[reindeer][sec]
    furthest = 0
    winning = []
    for reindeer in scores:
        if scores[reindeer]["dist"] == furthest:
            winning.append(reindeer)
        elif scores[reindeer]["dist"] > furthest:
            furthest = scores[reindeer]["dist"]
            winning = [reindeer, ]
    for reindeer in winning:
        scores[reindeer]["score"] += 1

from pprint import pprint as pp
pp(scores)


# Import pakages
import torch
import torch.nn as nn
import gym
import os
import torch.nn.functional as F
import torch.multiprocessing as mp
import numpy as np
# Import python files
from utils import v_wrap, set_init, push_and_pull, record
from shared_adam import SharedAdam

os.environ["OMP_NUM_THREADS"] = "1"
os.environ['KMP_DUPLICATE_LIB_OK']='True'
# Setting hyperparameters
UPDATE_GLOBAL_ITER = 10 #
GAMMA = 0.99
MAX_EP = 500
hidden_dim_pi = 16
hidden_dim_v = 16
env = gym.make('CartPole-v0')
N_S = env.observation_space.shape[0]
N_A = env.action_space.n

# Define basic neural network(It will be same for each worker)
class Net(nn.Module):
    def __init__(self, s_dim, a_dim):
        super(Net, self).__init__()
        self.s_dim = s_dim # 4
        self.a_dim = a_dim # 2
        self.pi1 = nn.Linear(s_dim, hidden_dim_pi) # (N, 4) -> (N, hidden_dim_pi)
        self.pi2 = nn.Linear(hidden_dim_pi, a_dim) # (N, hidden_dim_pi) -> (N, 2)
        self.v1 = nn.Linear(s_dim, hidden_dim_v) # (N, 4) -> (N, hidden_dim_v)
        self.v2 = nn.Linear(hidden_dim_v, 1) # (N, hidden_dim_v) -> (N, 1)
        set_init([self.pi1, self.pi2, self.v1, self.v2])
        self.distribution = torch.distributions.Categorical # It means that [a, b, c, ...] -> 0:a, 1:b, 2:c, ...
    # forward returns output of model
    # Return : softmax^(-1)(probability) and V(s)  (Note. During using crossentropy loss in pytorch, network must not contain softmax layer)
    def forward(self, x):
        pi1 = torch.tanh(self.pi1(x))
        logits = self.pi2(pi1)
        v1 = torch.tanh(self.v1(x))
        values = self.v2(v1)
        return logits, values
    # choose_action returns action from state s
    # Return : action
    def choose_action(self, s):
        self.eval()
        logits, _ = self.forward(s)
        prob = F.softmax(logits, dim=1).data # We need to change to probability
        m = self.distribution(prob) # take actions by given probability
        return m.sample().numpy()[0]
    # evaluate loss function
    # v_t : r+gamma*v_(t+1)
    def loss_func(self, s, a, v_t):
        self.train()
        logits, values = self.forward(s)
        td = v_t - values
        c_loss = td.pow(2)

        probs = F.softmax(logits, dim=1)
        m = self.distribution(probs)
        exp_v = m.log_prob(a) * td.detach().squeeze()
        a_loss = -exp_v
        total_loss = (c_loss + a_loss).mean()
        return total_loss

class Worker(mp.Process):
    def __init__(self, gnet, opt, global_ep, global_ep_r, res_queue, name):
        super(Worker, self).__init__()
        self.name = 'w%02i' % name
        self.g_ep, self.g_ep_r, self.res_queue = global_ep, global_ep_r, res_queue
        self.gnet, self.opt = gnet, opt
        self.lnet = Net(N_S, N_A)           # local network
        self.env = gym.make('CartPole-v0').unwrapped

    def run(self):
        total_step = 1
        while self.g_ep.value < MAX_EP:
            s = self.env.reset()
            buffer_s, buffer_a, buffer_r = [], [], []
            ep_r = 0.
            while True: #현재 시점 t
                if self.name == 'w00':
                    self.env.render()
                a = self.lnet.choose_action(v_wrap(s[None, :]))
                s_, r, done, _ = self.env.step(a)
                if done: r = -1
                ep_r += r
                buffer_a.append(a)
                buffer_s.append(s)
                buffer_r.append(r)

                if total_step % UPDATE_GLOBAL_ITER == 0 or done:  # update global and assign to local net
                    # sync
                    push_and_pull(self.opt, self.lnet, self.gnet, done, s_, buffer_s, buffer_a, buffer_r, GAMMA)
                    buffer_s, buffer_a, buffer_r = [], [], []

                    if done:  # done and print information
                        ep_r = min(ep_r, 200)
                        record(self.g_ep, self.g_ep_r, ep_r, self.res_queue, self.name)
                        break
                s = s_
                total_step += 1
        self.res_queue.put(None)

if __name__ == "__main__":
    gnet = Net(N_S, N_A)        # global network
    gnet.share_memory()         # share the global parameters in multiprocessing
    opt = SharedAdam(gnet.parameters(), lr=5e-4, betas=(0.92, 0.999))      # global optimizer
    global_ep, global_ep_r, res_queue = mp.Value('i', 0), mp.Value('d', 0.), mp.Queue()

    # parallel training
    workers = [Worker(gnet, opt, global_ep, global_ep_r, res_queue, i) for i in range(mp.cpu_count())]
    [w.start() for w in workers]
    res = []                    # record episode reward to plot
    while True:
        r = res_queue.get()
        if r is not None:
            res.append(r)
        else:
            break
    [w.join() for w in workers]

    import matplotlib.pyplot as plt
    res = np.array(res)
    np.save("discrete_result.npy", res)
    plt.plot(res)
    plt.ylabel('ep reward')
    plt.xlabel('Episode')
    plt.show()


import numpy as np

data = [[]]
with open("data.txt","r") as fichier:
    for line in fichier.read().splitlines():
        if line:
            data[-1].append(line)
        else:
            data.append([])

nb_stacks = int(data[0][-1][-2])
stacks = [[]]
pile_max = len(data[0])-1
for i in range(nb_stacks):
    stacks.append([])
    for j in range(pile_max):
        lettre = data[0][pile_max-j-1][4*i+1]
        if lettre!=" ":
            stacks[i].append([])
            stacks[i][j] = lettre
stacks.pop(-1)

def deplacer(tas,nombre,pile1,pile2):
    taille1 = len(tas[pile1])
    taille2 = len(tas[pile2])
    for i in range(nombre):
        tas[pile2].append([])
        tas[pile2][taille2+i] = tas[pile1][taille1-i-1]
        tas[pile1].pop(-1)
    return tas

def deplacer2(tas,nombre,pile1,pile2):
    taille1 = len(tas[pile1])
    taille2 = len(tas[pile2])
    for i in range(nombre):
        tas[pile2].append([])
        tas[pile2][taille2+i] = tas[pile1][taille1-nombre+i]
    for i in range(nombre):
        tas[pile1].pop(-1)
    return tas

for ligne in data[1]:
    instruction = ligne.split(" ")
    deplacer2(stacks,int(instruction[1]),int(instruction[3])-1,int(instruction[5])-1)

dessus = ""
for ligne in stacks:
    dessus += ligne[-1]

print(dessus)


from models.models import VatsimPilot
from data_reader import reader

def main():
    print("VATSIM LIB")
    json_data = reader.init()

    vgs = reader.get_vatsim_general(json_data)
    # print(vgs)

    pilots = reader.get_vatsim_pilots(json_data)
    print(f"number of pilots this update: {len(pilots)}")

    flight_plans = reader.get_flight_plans(pilots)
    print(f"number of flight plans this update: {len(flight_plans)}")

if __name__ == "__main__":
    main()


from Tkinter import *
root = Tk()
frame = Frame(root, bd=2, relief=SUNKEN)

frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=E+W)

yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=N+S)

canvas = Canvas(frame, bd=0,
                xscrollcommand=xscrollbar.set,
                yscrollcommand=yscrollbar.set)

canvas.grid(row=0, column=0, sticky=N+S+E+W)

xscrollbar.config(command=canvas.xview)
yscrollbar.config(command=canvas.yview)

frame.pack()

mainloop()


from lxml import etree
from xml.etree import ElementTree

def get_text_from_file(xml_file):
    tree = etree.parse(xml_file)
    root = tree.getroot()
    for element in root.iterfind('.//para'):
        for ele in element.findall('.//display'):
            parent = ele.getparent()
            parent.remove(ele)
        ElementTree.dump(element)


"""
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly L characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.
Corner Cases:

    A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
"""

class Solution(object):
    def fullJustify(self, words, maxWidth):
        """
        :type words: List[str]
        :type maxWidth: int
        :rtype: List[str]
        """
        if len(words) == 0:
            return []
        if maxWidth == 0:
            return [v for v in words if len(v) == 0]
        pos = 0
        ans = []
        curr_words = []
        while True:
            while sum(map(len, curr_words)) + len(curr_words) + len(words[pos]) <= maxWidth:
                curr_words.append(words[pos])
                pos += 1
                if pos >= len(words):
                    last_line = ' '.join(curr_words)
                    last_line += (maxWidth - len(last_line)) * " "
                    ans.append(last_line)
                    return ans
            words_length = sum(map(len, curr_words))
            if len(curr_words) == 1:
                ans.append(curr_words[0] + (maxWidth - words_length) * " ")
            else:
                join_spaces = (maxWidth - words_length) // (len(curr_words) - 1)
                extra_spaces = (maxWidth - words_length) % (len(curr_words) - 1)
                for i in range(extra_spaces):
                    curr_words[i] += " "
                ans.append((' ' * join_spaces).join(curr_words))
            curr_words = []

            # better way: ' '.join words, then chop off each group of words based on the indices of the spaces
            # for each chunk, split, do same thing as above to calculate the spaces to join with
            # append to ans, end when len(joined_string) <= 16

ans = Solution()
print(ans.fullJustify([""], 16))
print(ans.fullJustify([""], 0))
print(ans.fullJustify(["this", "is"], 0))
print(ans.fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16))
print(ans.fullJustify(["This", "is", "an", "example", "of", "text", "justification.", "exhaustively"], 16))


#!/usr/bin/env python3

import json
import os
import requests
import datetime

base_url="https://raw.githubusercontent.com/threatstop/crl-ocsp-whitelist/master/"
uri_list=['crl-hostnames.txt','crl-ipv4.txt','crl-ipv6.txt','ocsp-hostnames.txt','ocsp-ipv4.txt','ocsp-ipv6.txt']
dict=dict()
dict['list']=list()
def source_read_and_add(input_file):
	output_list=list()
	for item in input_file:
		item=item.rstrip()
		output_list.append(item)
	return output_list

for uri in uri_list:
	url = base_url + uri
	r=requests.get(url)
	dict['list'] += source_read_and_add(r.text)

dict['type'] = "string"
dict['matching_attributes']=["hostname","domain","ip-dst","ip-src","url", "domain|ip"]
dict['name']="CRL Warninglist"
dict['version']= int(datetime.date.today().strftime('%Y%m%d'))
dict['description']="CRL Warninglist from threatstop (https://github.com/threatstop/crl-ocsp-whitelist/)"
dict['list']=list(set(dict['list']))

print(json.dumps(dict))


import pickle
import numpy as np
import scipy.io as sio

from library.error_handler import Error_Handler

class Data_Loader:

    def load_data_from_pkl(self, filepath_x, filepath_y, ordering="True"):
        with open(filepath_x, "rb") as file_x:
            x_data = pickle.load(file_x)

        with open(filepath_y, "rb") as file_y:
            y_data = pickle.load(file_y)

        x_data = np.asarray(x_data)
        y_data = np.asarray(y_data)

        if np.min(y_data) > 0:
            y_data = y_data - np.min(y_data)

        reordered_data = Data_Loader.__reorder(x_data, ordering)

        return reordered_data, y_data

    def load_data_from_npy(self, filepath_x, filepath_y, ordering="True"):
        x_data = np.load(filepath_x)
        y_data = np.load(filepath_y)

        x_data = np.asarray(x_data)
        y_data = np.asarray(y_data)

        if np.min(y_data) > 0:
            y_data = y_data - np.min(y_data)

        reordered_data = Data_Loader.__reorder(x_data, ordering)

        return reordered_data, y_data

    def load_data_from_mat(self, filepath, x_key, y_key, ordering):
        mat_dict = sio.loadmat(filepath)
        x_data = mat_dict[x_key]
        y_data = mat_dict[y_key]

        x_data = np.asarray(x_data)
        y_data = np.asarray(y_data)

        if np.min(y_data) > 0:
            y_data = y_data - np.min(y_data)

        reordered_data = Data_Loader.__reorder(x_data, ordering)

        return reordered_data, y_data

    def __reorder(x, ordering):
        if ordering == "SWHC":
            return x
        elif ordering == "CWHS":
             x = np.swapaxes(x, 3, 0)
             return x
        elif ordering == "WHCS":
             x = np.rollaxis(x, 2, 0)
             x = np.swapaxes(x, 0, 3)
             return x
        elif ordering == "WHSC":
             x = np.rollaxis(x, 3, 0)
             x = np.swapaxes(x, 0, 3)
             return x
        elif ordering == "SCWH":
             x = np.rollaxis(x, 1, 4)
             return x
        elif ordering == "CSWH":
             x = np.swapaxes(x, 0, 1)
             x = np.rollaxis(x, 1, 4)
             return x
        else:
            Error_Handler.error_in_data_ordering()