"""
SiEPIC Analysis Package

Author:     Mustafa Hammood
            Mustafa@siepic.com

Example:    Application of SiEPIC_AP analysis functions
            Process data of various contra-directional couplers (CDCs)
            Extract the period and bandwidth from a set of devices
"""
#%%
import sys
import io
sys.path.append(r'D:\Academics\PyCharmProjects')  # Add the directory to sys.path
import siepic_analysis_package as siap
import matplotlib.pyplot as plt
import matplotlib, os
import numpy as np
import pandas as pd

class DirectionalCoupler:
    def __init__(self, fname_data, device_prefix, port_thru, port_drop, device_suffix,
                 name, wavl, pol, main_script_directory,
                 tol=3, N_seg=325):
        self.fname_data = fname_data
        self.device_prefix = device_prefix
        self.port_thru = port_thru
        self.port_drop = port_drop
        self.device_suffix = device_suffix
        self.name = name
        self.wavl = wavl
        self.pol = pol
        self.main_script_directory = main_script_directory

        self.tol = tol
        self.N_seg = N_seg

        self.devices = []
        self.period = []
        self.WL = []
        self.BW = []
        self.df_figures = pd.DataFrame()

    def getDeviceParameter(self, deviceID, devicePrefix, deviceSuffix=''):
        """Find the variable parameter of a device based on the ID

        IMPORTANT: "removeprefix" and "removesuffix" are only available
            for Python >= 3.9

        Args:
            deviceID (string): ID of the device.
            devicePrefix (string): Prefix string in the device that's before the variable parameter
            deviceSuffix (string): Any additional fields in the suffix of a device that need to be stripped, optional.

        Returns:
            parameter (float): variable parameter of the device (unit based on whats in the ID)
        """
        parameter = float(deviceID.removeprefix(devicePrefix).removesuffix(deviceSuffix))
        return parameter

    def process_files(self):
        for root, dirs, files in os.walk(self.fname_data):
            if os.path.basename(root).startswith(self.device_prefix):
                for file in files:
                    if file.endswith(".csv"):
                        file_path = os.path.join(root, file)
                        device = siap.analysis.processCSV(file_path)

                        device.dropCalib, device.ThruEnvelope, x, y = siap.analysis.calibrate_envelope(
                            device.wavl, device.pwr[self.port_thru], device.pwr[self.port_drop],
                            N_seg=self.N_seg, tol=self.tol, verbose=False)

                        [device.BW, device.WL] = siap.analysis.bandwidth(device.wavl, -device.dropCalib, threshold=6)

                        self.devices.append(device)
                        self.period.append(self.getDeviceParameter(device.deviceID, self.device_prefix, self.device_suffix))
                        self.WL.append(device.WL)
                        self.BW.append(device.BW)

        return self.devices, self.period, self.WL, self.BW

    def plot_devices(self):
        plt.figure(figsize=(10, 6))
        for device in self.devices:
            label = 'Period = ' + str(self.getDeviceParameter(device.deviceID, self.device_prefix, self.device_suffix)) + ' nm'
            plt.plot(device.wavl, device.pwr[self.port_drop], label=label)

        plt.legend(loc=0)
        plt.ylabel('Power [dBm]', color='black')
        plt.xlabel('Wavelength [nm]', color='black')
        plt.title("Raw measurement of all structures")
        matplotlib.rcParams.update({'font.size': 11, 'font.family': 'Times New Roman', 'font.weight': 'bold'})

        # save plots
        pdf_path_devices_raw, pdf_path_devices_calib, pdf_path_analysis, pdf_path_analysis_WL = self.saveGraph()
        plt.savefig(pdf_path_devices_raw, format='pdf')
        # plt.show()  # Display graph

        # Save the Matplotlib figure to a BytesIO object
        img_buffer = io.BytesIO()
        plt.savefig(img_buffer, format='png')
        img_buffer.seek(0)

        # Directly append the figure information to the existing DataFrame
        self.df_figures = self.df_figures._append(
            {'Name': f'{self.name}_{self.pol}{self.wavl}_raw', 'Figure': img_buffer},
            ignore_index=True
        )

        plt.figure(figsize=(10, 6))
        for device in self.devices:
            label = 'Period = ' + str(self.getDeviceParameter(device.deviceID, self.device_prefix, self.device_suffix)) + ' nm'
            plt.plot(device.wavl, device.dropCalib, label=label)

        plt.legend(loc=0)
        plt.ylabel('Transmission (dB)', color='black')
        plt.xlabel('Wavelength (nm)', color='black')
        plt.title("Calibrated measurement of all structures (using envelope calibration)")
        matplotlib.rcParams.update({'font.size': 11, 'font.family': 'Times New Roman', 'font.weight': 'bold'})

        # save plots
        pdf_path_devices_raw, pdf_path_devices_calib, pdf_path_analysis, pdf_path_analysis_WL = self.saveGraph()
        plt.savefig(pdf_path_devices_calib, format='pdf')
        # plt.show()  # Display graph

        # Save the Matplotlib figure to a BytesIO object
        img_buffer = io.BytesIO()
        plt.savefig(img_buffer, format='png')
        img_buffer.seek(0)

        # Directly append the figure information to the existing DataFrame
        self.df_figures = self.df_figures._append(
            {'Name': f'{self.name}_{self.pol}{self.wavl}_calib', 'Figure': img_buffer},
            ignore_index=True
        )

    def plot_analysis_results(self):
        fig, ax1 = plt.subplots(figsize=(10, 6))

        ax1.scatter(self.period, self.WL, color='blue')
        ax1.set_xlabel('Grating period [nm]')
        ax1.set_ylabel('Bragg wavelength [nm]', color='blue')
        ax1.tick_params(axis='y', colors='blue')

        ax2 = ax1.twinx()
        ax2.scatter(self.period, self.BW, color='red')
        ax2.set_ylabel('3 dB Bandwidth [nm]', color='red')
        ax2.tick_params(axis='y', colors='red')

        plt.title("Extracted bandwidth and central wavelength of the Bragg gratings")
        matplotlib.rcParams.update({'font.size': 11, 'font.family': 'Times New Roman', 'font.weight': 'bold'})

        # save plots
        pdf_path_devices_raw, pdf_path_devices_calib, pdf_path_analysis, pdf_path_analysis_WL = self.saveGraph()
        plt.savefig(pdf_path_analysis, format='pdf')
        # plt.show()  # Display graph

        # Save the Matplotlib figure to a BytesIO object
        img_buffer = io.BytesIO()
        plt.savefig(img_buffer, format='png')
        img_buffer.seek(0)

        # Directly append the figure information to the existing DataFrame
        self.df_figures = self.df_figures._append(
            {'Name': f'{self.name}_{self.pol}{self.wavl}_central', 'Figure': img_buffer},
            ignore_index=True
        )

    def overlay_simulation_data(self, target_wavelength, sim_label = 'Simulation (SiO2 Clad)'):
        simulation_period_sio2 = [313, 315, 317, 319, 321, 323]
        simulation_wavl_sio2 = [1536.64, 1542.24, 1547.85, 1553.45, 1559.06, 1564.56]

        # Interpolate simulation period at target_wavelength_sim
        simulation_period_at_target_sim = np.interp(target_wavelength, simulation_wavl_sio2, simulation_period_sio2)

        # Interpolate experimental period at target_wavelength_exp
        experimental_period_at_target_exp = np.interp(target_wavelength, self.WL, self.period)

        print(f"Simulation period at {target_wavelength} nm: {simulation_period_at_target_sim} nm")
        print(f"Experimental period at {target_wavelength} nm: {experimental_period_at_target_exp} nm")

        plt.figure(figsize=(10, 6))
        plt.scatter(self.period, self.WL, color='r', marker='x', label='Experiment')
        plt.scatter(simulation_period_sio2, simulation_wavl_sio2, color='b', marker='o', label=sim_label)

        plt.legend()
        plt.ylabel('Bragg Wavelength [nm]', color='black')
        plt.xlabel('Grating Period [nm]', color='black')
        plt.title("Comparison of Bragg wavelength between simulation and experiment.")
        matplotlib.rcParams.update({'font.size': 11, 'font.family': 'Times New Roman', 'font.weight': 'bold'})

        # Define a common set of wavelengths for interpolation
        common_wavelengths = np.linspace(min(min(self.period), min(simulation_period_sio2)),
                                         max(max(self.period), max(simulation_period_sio2)), 100)

        # Interpolate both experimental and simulated data to the common set of wavelengths
        exp_wavelength_interp = np.interp(common_wavelengths, self.period, self.WL)
        sim_wavelength_interp = np.interp(common_wavelengths, simulation_period_sio2, simulation_wavl_sio2)

        # Fit a polynomial to the experimental data
        exp_coefficients = np.polyfit(common_wavelengths, exp_wavelength_interp, 2)
        exp_poly_func = np.poly1d(exp_coefficients)

        # Fit a polynomial to the simulated data
        sim_coefficients = np.polyfit(common_wavelengths, sim_wavelength_interp, 2)
        sim_poly_func = np.poly1d(sim_coefficients)

        # Evaluate both fit lines at the common wavelengths
        exp_wavelength_fit = exp_poly_func(common_wavelengths)
        sim_wavelength_fit = sim_poly_func(common_wavelengths)

        # Calculate the differences between the fit lines
        differences = exp_wavelength_fit - sim_wavelength_fit

        # Find the average difference
        average_difference = np.mean(differences)

        print(f"Bragg Grating Wavelength Drift is: {average_difference} nm for {self.name}_{self.pol}{self.wavl}")

        # save plots
        pdf_path_devices_raw, pdf_path_devices_calib, pdf_path_analysis, pdf_path_analysis_WL = self.saveGraph()
        plt.savefig(pdf_path_analysis_WL, format='pdf')
        # plt.show()  # Display graph

        # Save the Matplotlib figure to a BytesIO object
        img_buffer = io.BytesIO()
        plt.savefig(img_buffer, format='png')
        img_buffer.seek(0)

        # Directly append the figure information to the existing DataFrame
        self.df_figures = self.df_figures._append(
            {'Name': f'{self.name}_{self.pol}{self.wavl}_overlay', 'Figure': img_buffer},
            ignore_index=True
        )

        # Return the average difference and the combined DataFrame
        return average_difference
    def saveGraph(self):
        """
        Save a graph as PDF files in a directory based on the `self.name` attribute.

        This method creates a directory named after `self.name` inside the specified `main_script_directory`
        (or a custom directory if provided) and saves two PDF files within this directory: one for raw data
        and another for cutback data.

        Returns:
        - pdf_path_raw (str): The full path to the saved raw data PDF file.
        - pdf_path_cutback (str): The full path to the saved cutback data PDF file.
        """
        # Create a directory based on self.name, self.pol, and self.wavl if it doesn't exist
        output_directory = os.path.join(self.main_script_directory, f"{self.name}_{self.pol}{self.wavl}")
        os.makedirs(output_directory, exist_ok=True)

        # Combine the directory and the filename to get the full paths
        pdf_path_devices_raw = os.path.join(output_directory, f"{self.name}_{self.pol}{self.wavl}_devices_raw.pdf")
        pdf_path_devices_calib = os.path.join(output_directory, f"{self.name}_{self.pol}{self.wavl}_devices_calib.pdf")
        pdf_path_analysis = os.path.join(output_directory, f"{self.name}_{self.pol}{self.wavl}_analysis.pdf")
        pdf_path_analysis_WL = os.path.join(output_directory, f"{self.name}_{self.pol}{self.wavl}_analysis_WL.pdf")

        # Now, you can save your PDFs to pdf_path_raw and pdf_path_cutback
        return pdf_path_devices_raw, pdf_path_devices_calib, pdf_path_analysis, pdf_path_analysis_WL


# -*- coding:utf-8 -*-
# @TIME : 2021/3/2 16:20
# @Author : MYH
# @File : request练习.py
# @Software : PyCharm
# 需求：爬取搜狗首页的页面数据

import requests

if __name__ == "__main__":
    # step1: 指定url
    url = 'https://www.sogou.com/'

    # step2: 发起请求
    # get方法会返回一个响应对象
    response = requests.get(url = url)

    # step3: 获取响应数据
    # .text返回的是字符串形式的响应数据
    page_text = response.text
    print(page_text)

    # step4: 存储
    with open('./sogou.html', 'w', encoding='utf-8') as fp:
        fp.write(page_text)


# __author__ == ChiWun Yang
# email == christiannyang37@gmail.com

import os
import datasets
import pandas as pd
from config import task, src_tgt, train_data_path, val_data_path, num_data

def main():
    print(f"Downloading {task.upper()} Dataset {src_tgt.upper()}")
    wmt_data = datasets.load_dataset(task, src_tgt)
    train_df = pd.DataFrame(wmt_data['train']).iloc[:num_data]
    val_df = pd.DataFrame(wmt_data['validation'])

    folder_path = '/'.join(train_data_path.split('/')[:-1])
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    train_df.to_csv(train_data_path, index=False)
    val_df.to_csv(val_data_path, index=False)

if __name__ == '__main__':
    main()


import click
import json as jsonPkg
from utils import fetch, files

@click.command()
@click.option('--export', is_flag=True, default=False)
@click.option('--json', is_flag=True, default=False)
@click.option('--csv', is_flag=True, default=False)
@click.option('--limit', type=int, default=5)
@click.option('--page', type=int, default=1)
@click.pass_context
def list_articles(ctx, limit, export, json, csv, page):
    """This is a command that returns a list of articles to
    Arguments:
        article {[string or id]} -- [unique identifier for the article]
    """
    response = fetch.get('articles?page_size={}&page={}'.format(limit, page))
    articles = response.json().get('articles', {})
    click.echo(jsonPkg.dumps(articles, indent=4))
    if export:
        filename = "all_articles"
        if json:
            files.export_to_json(filename=filename, data=articles)
        if csv:
            files.export_to_csv(filename=filename, data=articles)
    next_q = click.prompt(
        "Use [n] to go next page or [q] to exit", default='n')
    if next_q.lower() == 'n':
        ctx.invoke(list_articles,
                   limit=limit, export=False, page=page+1)


import sys, getopt, subprocess, os, argparse
from gene import Gene
from bioEntity import bioEntity

class Gff3(object):
	"""docstring for Gff3"""
	def __init__(self, filename):
		self.filename          = filename
		self.genes             = {}
		self.transcripts       = {}
		self.inronsGemnome     = {}
		self.sequencesFeatures = {}
		self.introns           = {}
		self.entitiesAlone     = []
		self.cdsAlone          = []
		self.max_position      = 0
		self.execute_geneMap   = False

	def set_gffreadPath(self, gffreadPath):
		self.gffreadPath = gffreadPath

	def set_gffFile(self, fileName):
		self.gffFile_name = fileName
		self.gffFile      = open(fileName,'w')

	def close_gffFile(self):
		self.gffFile.close()

	def close_geneMapFile(self):
		self.genemapFile.close()

	def readFile(self):
		with open(self.filename) as allFile:
			for line in allFile:
				line     = line.strip()
				cols     = line.split("\t")
				entity   = cols[2]
				extras   = []
				lastCol  = cols[8].split(";")
				tmp_end  = int(cols[4])
				ID       = ''
				Name     = ''
				Parent   = ''
				GeneAtrr = ''
				for info in lastCol:
					data = info.split("=")
					if data[0] == 'ID':
						ID = data[1]
						continue

					if data[0] == 'Name':
						Name = data[1]
						continue

					if data[0] == 'Parent':
						Parent = data[1]
						continue

					if data[0] == 'gene':
						GeneAtrr = data[1]
						continue

					extras.append(info)

				extras_str = ";".join(extras)

				if tmp_end > self.max_position:
					self.max_position = tmp_end + 1

				bio_entity = bioEntity()
				bio_entity.set_Chromosome(cols[0])
				bio_entity.set_Source(cols[1])
				bio_entity.set_Type(cols[2])
				bio_entity.set_Init(int(cols[3]))
				bio_entity.set_End(tmp_end)
				bio_entity.set_Score(cols[5])
				bio_entity.set_Strand(cols[6])
				bio_entity.set_ScoreCDS(cols[7])
				bio_entity.set_ID(ID)
				bio_entity.set_Name(Name)
				bio_entity.set_Parent(Parent)
				bio_entity.set_Extras(extras_str)
				bio_entity.set_Gene(GeneAtrr)

				if entity == 'gene':
					self.setNewGene(ID, bio_entity)

				if entity == 'mRNA' or entity == 'tRNA' or entity == 'rRNA':
					if Parent not in self.genes:
						self.setNewGene(Parent, bioEntity())

					self.genes[Parent].add_transcript(bio_entity)
					self.transcripts[ID] = Parent

				if entity == 'five_prime_UTR':
					self.genes[self.transcripts[Parent]].add_fivePrimeUTR(bio_entity)

				if entity == 'three_prime_UTR':
					self.genes[self.transcripts[Parent]].add_threePrimeUTR(bio_entity)

				if entity == 'sequence_feature':
					self.sequencesFeatures[ID] = bio_entity

				if entity == 'intron':
					if GeneAtrr != '':
						if GeneAtrr not in self.genes:
							GeneAtrr = 'gene-' + GeneAtrr + '-2'
							bio_entity.set_Gene(GeneAtrr)
							if GeneAtrr not in self.genes:
								self.setNewGene(GeneAtrr, bioEntity())

						self.genes[GeneAtrr].add_introns(bio_entity)
						self.introns[ID] = GeneAtrr

					else:
						self.inronsGemnome[ID] = bio_entity

				if entity == 'CDS':
					if Parent in self.genes:
						self.genes[Parent].add_cds(bio_entity)

					else:
						if Parent in self.transcripts:
							self.genes[self.transcripts[Parent]].add_cds(bio_entity)

						else:
							self.cdsAlone.append(bio_entity)

				if entity == 'exon':
					if Parent in self.genes:
						self.genes[Parent].add_exons(bio_entity)

					else:
						if Parent in self.transcripts:
							self.genes[self.transcripts[Parent]].add_exons(bio_entity)

						else:
							self.entitiesAlone.append(bio_entity)

	def setNewGene(self, ID, bio_entity):
		if ID not in self.genes:
			self.genes[ID] = Gene()

		if bio_entity.get_ID() != '':
			self.genes[ID].add_bioEntity(bio_entity)

	def addGenesToEmptyCDS(self):
		for cds in self.cdsAlone:
			newId      = cds.get_ID().replace("cds", "gene")
			bio_entity = bioEntity()
			bio_entity.set_Chromosome(cds.get_Chromosome())
			bio_entity.set_Source(cds.get_Source())
			bio_entity.set_Type('gene')
			bio_entity.set_Init(cds.get_Init())
			bio_entity.set_End(cds.get_End())
			bio_entity.set_Score('.')
			bio_entity.set_Strand(cds.get_Strand())
			bio_entity.set_ScoreCDS('.')
			bio_entity.set_ID(newId)
			bio_entity.set_Name(newId)
			cds.set_Parent(newId)
			self.setNewGene(newId, bio_entity)
			self.genes[newId].add_cds(cds)

	def addTranscriptsToEmptyGenes(self):
		for key in self.genes:
			gene = self.genes[key]
			if gene.size_transcript() == 0:
				if gene.size_cds() == 0:
					print("ATTENTION!!! Este cuate no se puede deducir: " + key)

				else:

					init         = self.max_position
					end          = 0
					new_id       = key + '-mRNA-1'
					new_entities = []
					if gene.size_exons() > 0:
						for exon in gene.get_exons():
							init = self.checkPosition(exon.get_Init(), init, True)
							end = self.checkPosition(exon.get_End(), end, False)
							exon.set_Parent(new_id)
							new_entities.append(exon)

						self.genes[key].change_exons(new_entities)

					else:
						for cds in gene.get_cds():
							init = self.checkPosition(cds.get_Init(), init, True)
							end = self.checkPosition(cds.get_End(), end, False)
							cds.set_Parent(new_id)
							new_entities.append(cds)

						self.genes[key].change_cds(new_entities)

					transcript = self.createTranscript(gene.get_bioEntity(), 0, init, end, new_id, key)
					self.genes[key].add_transcript(transcript)

	def checkPosition(self, x, y, direction):
		if direction:
			decision = x < y

		else:
			decision = x > y

		if decision:
			return x

		return y

	def createTranscript(self, gene, pos, init, end, new_id, parent):
		bio_entity = bioEntity()
		bio_entity.set_Chromosome(gene[pos].get_Chromosome())
		bio_entity.set_Source(gene[pos].get_Source())
		bio_entity.set_Type("mRNA")
		bio_entity.set_Init(init)
		bio_entity.set_End(end)
		bio_entity.set_Strand(gene[pos].get_Strand())
		bio_entity.set_ID(new_id)
		bio_entity.set_Name(new_id)
		bio_entity.set_Parent(parent)
		return bio_entity

	def printGene(self, ID):
		for gene in self.genes[ID].get_bioEntity():
			self.print_gff(gene.get_line())

		for transcript in self.genes[ID].get_transcript():
			self.print_gff(transcript.get_line())

		for cds in self.genes[ID].get_cds():
			self.print_gff(cds.get_line())

		for exon in self.genes[ID].get_exons():
			self.print_gff(exon.get_line())

		for intron in self.genes[ID].get_introns():
			self.print_gff(intron.get_line())

		for fivePrimeUTR in self.genes[ID].get_fivePrimeUTR():
			self.print_gff(fivePrimeUTR.get_line())

		for threePrimeUTR in self.genes[ID].get_threePrimeUTR():
			self.print_gff(threePrimeUTR.get_line())

	def print_sequencesFeatures(self):
		for key in self.sequencesFeatures:
			self.print_gff(self.sequencesFeatures[key].get_line())

	def print_inronsGemnome(self):
		for key in self.inronsGemnome:
			self.print_gff(self.inronsGemnome[key].get_line())

	def print_entitiesAlone(self):
		for entitieAlone in self.entitiesAlone:
			self.print_gff(entitieAlone.get_line())

	def print_genes(self):
		for key in self.genes:
			self.printGene(key)

	def print_gff(self, text):
		self.gffFile.write(text + "\n")

	def print_genemap(self, transcript, gene):
		self.genemapFile.write(transcript + "\t" + gene + "\n")

	def execute_gffRead(self, fasta):
		cmdline(self.gffreadPath + " " + self.gffFile_name + " -g " + fasta + " -w " + fasta + ".new")

	def setGeneMap(self, genemapFile):
		if genemapFile != '':
			self.execute_geneMap = True
			self.genemapFile     = open(genemapFile,'w')

	def executeGeneMap(self):
		for key in self.genes:
			for transcript in self.genes[key].get_transcript():
				self.print_genemap(transcript.get_ID(), key)

def getArguments():
	"""Get the arguments of the command executed

	Get the arguments and their values of the program execution

	Returns:
		Object -- The arguments with values
	"""
	parser = argparse.ArgumentParser()
	parser.add_argument("-g", "--gff3", help = "GFF3 file")
	parser.add_argument("-f", "--fasta", help = "Genome fasta file")
	parser.add_argument("-r", "--gffread", help = "Path to gffRead bin")
	parser.add_argument("-m", "--geneMap", help = "geneMap file")

	data   = parser.parse_args()

	if isEmpty(data.gff3):
		print("Is required the GFF3 file.")
		parser.print_help()
		exit()

	#if isEmpty(data.fasta):
		#print("Is required the fasta file.")
		#parser.print_help()
		#exit()

	if isEmpty(data.geneMap):
		data.geneMap = ''

	if isEmpty(data.gffread):
		data.gffread = '/home/rdorado/gffread/gffread/gffread'

	return data

'''
Execute a command in the linux terminal
@Param	{String}	command	Command to execute
@return	{String}			Result of the execution
'''
def cmdline(command):
    process = subprocess.Popen(
        args  = command,
        shell = True
    )

    return process.communicate()[0]

def isEmpty(var):
	"""Verify if a variable is empty

	Verify if a variable is empty

	Arguments:
		var {all} -- Variable

	Returns:
		[boolean] -- If the variable is 'None' or '' or the length of it is 0
	"""
	return var == None or var == '' or len(var) == 0

arguments = getArguments()
gff3 = Gff3(arguments.gff3)
gff3.readFile()
gff3.addGenesToEmptyCDS()
gff3.addTranscriptsToEmptyGenes()
gff3.set_gffFile(arguments.gff3 + ".new")
gff3.print_genes()

#gff3.print_sequencesFeatures()
#gff3.print_inronsGemnome()
#gff3.print_entitiesAlone()

gff3.close_gffFile()

gff3.set_gffreadPath(arguments.gffread)
gff3.execute_gffRead(arguments.fasta)

gff3.setGeneMap(arguments.geneMap)
gff3.executeGeneMap()


from flask import Flask
from flask_cors import CORS
from flask_marshmallow import Marshmallow
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

ENVS_MAP = {
    "development": "config.development.DevelopmentConfig",
    "production": "config.production.ProductionConfig",
    "testing": "config.testing.TestingConfig",
}

db = SQLAlchemy()
migrate = Migrate(db=db)
ma = Marshmallow()

def create_app(config_name="development"):
    # create and configure the app
    app = Flask(__name__)
    config_env = ENVS_MAP.get(config_name)
    app.config.from_object(config_env)
    init_logging(app)
    init_extensions(app)

    with app.app_context():
        init_blueprints(app)

    return app

def init_extensions(app):
    CORS(app)
    db.init_app(app)
    migrate.init_app(app)
    ma.init_app(app)

def init_blueprints(app):
    from app.api.infra.v1.apiv1 import blueprint as data_api_blueprint

    app.register_blueprint(data_api_blueprint)

def init_logging(app):
    import logging

    app.logger.setLevel(logging.ERROR)
    return


import os
import mimetypes
import json

class OperaHTTP:
  """ HTTP comm helper methods to be used across all the Python testserver
  modules. """

  @staticmethod
  def AsDataDirPath(path):
    syncserver_dir = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(syncserver_dir, os.pardir, 'data', path)

  # Generates a response dictionary to be used to send the HTTP response with
  # the _send_response() method.
  #
  # @handled: Whether the response was succesfully handled by the currently
  #   running handler, setting this to False should cause remaining handlers
  #   to run until one is found that returns True.
  # @code: The HTTP code that comes with the response.
  # @headers: HTTP headers to be included in the response.
  # @content_type: Allows overriding the Content-Type HTTP header sent with the
  #   response.
  # @content: Raw content response, e.g. a HTML document or a binary file to be
  #   sent to the client. Note that if content is not empty, to_json has to
  #   be equal to None.
  # @to_json: A dictionary of values that will be JSON-encoded and sent as the
  #   raw content response. Note that in case this param is not None, the
  #   explicit content parameter has to be empty.
  # @network_error: Setting this to True will casue closing the socket even
  #   before the HTTP response is sent, effectively causing a network error
  #   on the client side.
  # @file_name: A helper for uploading a specific file to the client. The file
  #   contents will be returned along with a mime type guessed basing on its
  #   extension. Note that the file will be looked for in the
  #   sync/testserver/data directory.
  @staticmethod
  def response(handled=True,
               code=200,
               headers=[],
               content_type='text/html',
               content='',
               to_json=None,
               network_error=False,
               file_name=None,
               replacements={}):

    if file_name is not None:
      assert (content is ''), "content must be empty if file_name given"
      assert (to_json is None), "to_json must be None if file_name given"

      file_name = OperaHTTP.AsDataDirPath(file_name)

      if not os.path.isfile(file_name):
        raise Exception("File '%s' not found." % file_name)

      content = open(file_name, 'r').read()
      content_type = mimetypes.MimeTypes().guess_type(file_name)

      for key in replacements:
        findstr = '[%s]' % key
        replacestr = str(replacements[key])
        content = content.replace(findstr, replacestr)

    elif to_json is not None:
      assert (content is ''), "content must be empty if to_json given"
      assert (file_name is None), "file_name must be None if to_json given"

      content = json.dumps(to_json,
                           sort_keys=True,
                           indent=2,
                           separators=(',', ': '))
      content_type = 'application/json'

    ret_dict = {
        'handled': handled,
        'code': code,
        'headers': headers,
        'type': content_type,
        'content': content,
        'network_error': network_error,
    }
    return ret_dict


import argparse
import os
from PIL import Image
import numpy as np

parser = argparse.ArgumentParser(description='export_gt_depth')

parser.add_argument('--improved_path',
                    type=str,
                    default="./kitti_dapth",
                    help='path to the root of the KITTI data')
opt = parser.parse_args()

def readlines(filename):
    """Read all the lines in a text file and return as a list
    """
    with open(filename, 'r') as f:
        lines = f.read().splitlines()
    return lines

filenames = readlines(os.path.join(os.path.dirname(__file__), "test_files.txt"))
new_filenames = []
GT_depths = []
count = 0
for index in range(len(filenames)):
    line = filenames[index].split()
    folder = line[0]
    if len(line) == 3:
        frame_index = int(line[1])
    else:
        frame_index = 0
    GT_path = os.path.join(opt.improved_path, "train", line[0][11:], "proj_depth/groundtruth/image_02", line[1]+".png")
    if os.path.exists(GT_path):
        count += 1
        new_filenames.append(filenames[index]+"\n")
        GT_depth = np.array(Image.open(GT_path)) / 255.
        GT_depths.append(GT_depth)
    GT_path = GT_path.replace("train", "val")
    if os.path.exists(GT_path):
        count += 1
        new_filenames.append(filenames[index]+"\n")
        GT_depth = np.array(Image.open(GT_path)) / 255.
        GT_depths.append(GT_depth)
# GT_depths = np.array(GT_depths)
np.savez(os.path.join(os.path.dirname(__file__), 'gt_depths.npz'),data=GT_depths)
# with open("new_test_files.txt", 'w') as f:
#     lines = f.writelines(new_filenames)
print(count)


import datetime

def addExpense(listOfExpenses, expense):
    '''
        Functions that adds a new expense to the list of expenses
    in:
        - listOfExpenses: list of lists
        - expense: list - (day,sum,type)
    out: boolean - True if that isn't any other expense with the same category in the current date, False otherwise
    '''
    for e in listOfExpenses:
        if e[0] == expense[0] and e[2] == expense[2]:
            return False
    else:
        listOfExpenses.append(expense)
        return True

def insertExpense(listOfExepenses, expense):
    '''
            Functions that inserts a new expense to the list of expenses, if there is another expense with the given
        type in that day, it is overwritten
        in:
            - listOfExpenses: list of lists
            - expense: list - (day,sum,type)
        out: boolean - always True because there aren't any conflicts
        '''
    auxExpense = -1
    for i in range(len(listOfExepenses)):
        if listOfExepenses[i][0] == expense[0] and listOfExepenses[i][2] == expense[2]:
            auxExpense = i
    if auxExpense != -1:
        listOfExepenses[auxExpense] = expense
    else:
        listOfExepenses.append(expense)
    return True

def removeExpenseByDay(day, listOfExpenses):
    '''
            Functions that remove a expense by day from the list of expenses
        in:
            - listOfExpenses: list of tuples of form (day,sum,type)
            - day: integer
        out: boolean - True if there are expenses to be removed in the given day, False otherwise
    '''
    auxList = listOfExpenses
    listOfExpenses[:] = filter(lambda a: a[0] != day, listOfExpenses)
    if auxList == listOfExpenses:
        return False
    return True

def removeExpenseByStartEndDay(startDay, endDay, listOfExpense):
    '''
            Functions that remove a expense from period of time from the list of expenses
        in:
            - listOfExpenses: list of tuples of form (day, sum, type)
            - startDay: integer - first day from which we start to remove expenses
            - endDay: integer - last day from which we start to remove expenses
        out: boolean - True if there are expenses to be removed in the given period of time, False otherwise
    '''
    auxList = listOfExpense
    listOfExpense[:] = filter(lambda a: startDay > a[0] or a[0] > endDay, listOfExpense)
    if auxList == listOfExpense:
        return False
    return True

def removeExpenseByType(typeOfExepense, listOfExpenses):
    '''
            Functions that remove a expense from period of time from the list of expenses
        in:
            - listOfExpenses: list of tuples of form (day, sum, type)
            - typeOfExpense: string - the type of expense we want to remove
        out: boolean - True if there are expenses to be removed that are the given type, False otherwise
    '''
    auxList = listOfExpenses
    listOfExpenses[:] = filter(lambda a: a[2] != typeOfExepense, listOfExpenses)
    if auxList == listOfExpenses:
        return False
    return True

def listExpenseByType(listOfExpenses, typeOfExpense):
    '''
            Functions that lists the expenses by type
        in:
            - listOfExpenses: list of tuples of form (day, sum, type)
            - typeOfExpense: string - the type of expense by which we want to list the list
        out: list of tuple of form (day, sum, type)
    '''
    return list(filter(lambda a: a[2] in typeOfExpense, listOfExpenses))

def listExpenseByTypeAndCondition(listOfExpense, typeOfExpense, condition, value):
    '''
            Functions that lists the expenses by type and condition
        in:
            - listOfExpenses: list of tuples of form (day, sum, type)
            - typeOfExpense: string - the type of expense by which we want to list the list
            - condition: string - one of (<, =, >)
            - value: integer - value with which we verify the condition
        out: list of list of form (day, sum, type)
    '''
    if condition == '>':
        auxList = listExpenseByType(listOfExpense, typeOfExpense)
        auxList[:] = filter(lambda a: a[1] > value, auxList)
        return auxList
    if condition == '<':
        auxList = listExpenseByType(listOfExpense, typeOfExpense)
        auxList[:] = filter(lambda a: a[1] < value, auxList)
        return auxList
    if condition == '=':
        auxList = listExpenseByType(listOfExpense, typeOfExpense)
        auxList[:] = filter(lambda a: a[1] == value, auxList)
        return auxList

def addExpenseCommand(params, listOfExpenses):
    '''
            A wrapper function that check the params of the 'add' command, converts the strings to integers,
        links the data input to the functions that modifies directly the list
        in:
            - listOfExpenses: list of tuples of form (day, sum, type)
            - params: list - (<sum>, <category>)
    '''
    if len(params) < 2:
        print("Invalid data type")
    else:
        try:
            params[0] = int(params[0])
        except ValueError:

            print("Invalid data type: sum cannot be string")
            return
        now = datetime.datetime.now()
        global HOUSE_EXPENSES_TYPE
        if params[1] not in HOUSE_EXPENSES_TYPE:
            print("Invalid data type: expense type isn't valid")
            return
        else:
            expense = [now.day, params[0], params[1]]
            if not addExpense(listOfExpenses, expense):
                print("Expense was not valid")
            else:
                print("Expense added to the list")

def insertExpenseCommand(params, listOfExpenses):
    '''
           A wrapper function that check the params of the 'insert' command, converts the strings to integers,
        links the data input to the functions that modifies directly the list
        in:
           - listOfExpenses: list of lists of form (day, sum, type)
           - params: list - <day> <sum> <category>
    '''
    if len(params) < 3:
        print("Invalid data type")
    else:
        try:
            params[0] = int(params[0])
        except ValueError:
            print("Invalid data type: sum cannot be string")
            return
        try:
            params[1] = int(params[1])
        except ValueError:
            print("Invalid data type: sum cannot be string")
            return

        if not 0 < params[0] < 31:
            print("Invalid data type: day must be between 1 and 30")
            return

        global HOUSE_EXPENSES_TYPE
        if params[2] not in HOUSE_EXPENSES_TYPE:
            print("Invalid data type: expense type isn't valid")
            return
        else:
            expense = [params[0], params[1], params[2]]
            if not insertExpense(listOfExpenses, expense):
                print("Expense was not valid")
            else:
                print("Expense added to the list")

def removeExpenseCommand(params, listOfExpenses):
    '''
               A wrapper function that check the params of the 'remove' command, converts the strings to integers,
            links the data input to the functions that modifies directly the list
            in:
               - listOfExpenses: list of lists of form (day, sum, type)
               - params: list - (<day>)
                              - (<start day>, to, <end day>)
                              - (<category>)
    '''
    global HOUSE_EXPENSES_TYPE
    if not checkParams(params):
        print("Invalid Command")
        return
    if len(params) < 2:
        if params[0] in HOUSE_EXPENSES_TYPE:
            if not removeExpenseByType(params[0], listOfExpenses):
                print("Day expenses were removed")
            else:
                print("The given category haven't had any expenses")
        else:
            try:
                params = int(params[0])
                print(params)
                if not removeExpenseByDay(params, listOfExpenses):
                    print("Day expenses were removed")
                else:
                    print("The given day haven't had any expenses")
            except ValueError:
                print("Invalid command")
    elif len(params) < 4 and params[1] == 'to':
        try:
            startDay = int(params[0])
            endDay = int(params[2])
            if not 0 < startDay < endDay <= 31:
                print("Invalid day type: startDate and endDate must be between 1 and 30")
                return
            if not removeExpenseByStartEndDay(startDay, endDay, listOfExpenses):
                print("Day expenses were removed")
                return
            else:
                print("The days between didn't have any expenses")
        except ValueError:

            print('Invalid day type: starDate and endDate must be integers')
            return

    else:
        print("Invalid command")

def expenseToString(expense):
    '''
        Converts a list of form (day, sum, category) to a formated string
        in:
            expense - list of form (day, sum, category)
        out:
            string
    '''
    return "Day: " + str(expense[0]) + " Sum: " + str(expense[1]) + " Category: " + str(expense[2])

def listToString(list):
    '''
        Converts a list of lists of form (day, sum, category) to a formated string
        in:
            list - list of lists of form (day, sum, category)
        out:
            string
    '''
    string = ""
    if list is None:
        return "No expense here"
    elif len(list) == 0:
        return "No expense here"
    for i in list:
        string += "\n"+expenseToString(i)
    return string

def listCommand(params, listOfExpenses):
    '''
           A wrapper function that check the params of the 'list' command, converts the strings to integers,
        links the data input to the functions that modifies directly the list
        in:
           - listOfExpenses: list of lists of form (day, sum, type)
           - params: list - []
                          - (<category>)
                          - (< <category>, [ < | = | > ], <value>>)
    '''
    global HOUSE_EXPENSES_TYPE
    operands = ['<', '>', '=']

    if len(params) == 0:
        print("Entire list of expenses")
        print(listToString(listExpenseByType(listOfExpenses, HOUSE_EXPENSES_TYPE)))
    elif params[0] in HOUSE_EXPENSES_TYPE:
        if len(params) == 1:
            print("All the expenses for the " + str(params[0] + " are:"))
            print(listToString(listExpenseByType(listOfExpenses, params[0])))
        elif len(params) != 3:
            print("Invalid input")
        else:
            try:
                value = int(params[2])
                if params[1] in operands:
                    print(listToString(listExpenseByTypeAndCondition(listOfExpenses, params[0], params[1], value)))
                else:
                    print("Invalid operand type")
            except ValueError:
                print("Invalid input type")
    else:
        print("Invalid input")

def readCommand():
    cmd = input("Give the command:\n")
    cmd.strip()
    if cmd.find(' ') == -1:
        command = cmd
        params = []
    else:
        command = cmd[0:cmd.find(' ')]
        params = cmd[cmd.find(' '):]
        params = params.split(';')
        auxParams = []
        for param in params:
            auxParams.append(param.split())
        params = auxParams
    return command, params

# tuple that contain all our constant expense types
HOUSE_EXPENSES_TYPE = ('housekeeping', 'food', 'transport', 'clothing', 'internet', 'others')

def testInit(listOfExpenses):
    '''
        A function that initialize our list with a set of data
    '''
    listOfExpenses.append([1, 20, 'food'])
    listOfExpenses.append([2, 20, 'food'])
    listOfExpenses.append([3, 30, 'food'])
    listOfExpenses.append([9, 30, 'food'])
    listOfExpenses.append([22, 30, 'internet'])
    listOfExpenses.append([2, 30, 'clothing'])
    listOfExpenses.append([1, 20, 'clothing'])
    listOfExpenses.append([12, 20, 'internet'])
    listOfExpenses.append([13, 30, 'food'])
    listOfExpenses.append([29, 30, 'others'])
    listOfExpenses.append([22, 30, 'internet'])
    listOfExpenses.append([2, 30, 'clothing'])

def checkParams(params):
    if len(params) == 0:
        return False
    return True

def helpCommand(menuCommands):
    '''
        This functions print out all the commands with their example
        menuCommands -> dictionary
    '''
    for command in menuCommands:
        print(menuCommands[command][1])

def start():
    '''
        This is our main function that controlls our program
    '''
    # a dictionary with all our menu options with key, message to print and function to call}
    menuCommands = {
        'add': (
            addExpenseCommand,
            '1.Add a new expense into the list. \n\t'
            'add <sum> <category>\n\t'
            'e.g.\n\t'
            'add 10 internet – add to the current day an expense of 10 RON for internet.'
        ),
        'insert': (
            insertExpenseCommand,
            '2.Insert a new expense into the list in a given day\n\t'
            'insert <day> <sum> <category>\n\t'
            'e.g\n\t'
            'insert 25 100 food – insert to day 25 an expense of 100 RON for food.'
        ),
        'remove': (
            removeExpenseCommand,
            '3. Modify expenses from the list.\n\t'
            'remove <day>\n\t'
            'remove <start day> to <end day>\n\t'
            'remove <category>\n\t'
            'e.g.\n\t'
            'remove 15 – remove all the expenses for day 15\n\t'
            'remove 2 to 9 – remove all the expenses between day 2 and day 9\n\t'
            'remove food – remove all the expenses for food from the current month.'
        ),
        'list': (
            listCommand,
            '4. Write the expenses having different properties\n\t'
            'list\n\t'
            'list <category>\n\t'
            'list <category> [ < | = | > ] <value>\n\t'
            'e.g\n\t'
            'list – write the entire list of expenses\n\t'
            'list food – write all the expenses for food.\n\t'
            'list food > 5 - writes all expenses for food with an amount of money > 5\n\t'
            'list internet = 44 - writes all expenses for internet with an amount of money = 44\n\t'
        ),
        'help': (
        helpCommand,
            '5. Show help'
        )
    }
    listOfExpenses = []
    testInit(listOfExpenses)

    while True:
        cmd = readCommand()
        command = cmd[0]
        params = cmd[1]
        if command == 'exit':
            print("Program ends here with success")
            break
        elif command == 'help':
            menuCommands['help'][0](menuCommands)
        elif command in menuCommands:
            if len(params) == 0:
                menuCommands[command][0](params, listOfExpenses)
            else:
                for param in params:
                    menuCommands[command][0](param, listOfExpenses)
        else:
            print("Invalid command")

def commandToString(cmd,params):
    return "The executed command is: " + str(cmd) + " " + str([str(a) + " " for a in params])

start()


import unittest
import os
import importlib.util
from datetime import datetime
from gradescope_utils.autograder_utils.decorators import weight
from unittest.mock import patch
from io import StringIO

class TestBonus(unittest.TestCase):

    submissions_path = "/autograder/submission/"

    def setUp(self):
        self.exercise_numbers = [1, 2, 3]
        self.start_times = [
            datetime(2023, 9, 5, 17, 0, 0),  # 5pm September 5th, 2023
            datetime(2023, 9, 11, 17, 0, 0), # 5pm September 11th, 2023
            datetime(2023, 9, 7, 8, 0, 0),   # 8am September 7th, 2023
            datetime(2023, 9, 8, 8, 0, 0)    # 8am September 8th, 2023
        ]

    def load_module(self, exercise_number):
        file_path = os.path.join(self.submissions_path, f'A7E{exercise_number}.py')
        module_name = f'A7E{exercise_number}'
        spec = importlib.util.spec_from_file_location(module_name, file_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        return module

    @weight(10)
    def test_exercise_1_bonus(self):
        """BONUS: Test Exercise 1 for error-checking."""
        module = self.load_module(1)
        with patch('builtins.input', side_effect=['not_a_number']), \
             patch('sys.stdout', new_callable=StringIO) as mock_stdout:
            module.main()
            output = mock_stdout.getvalue().strip()
            expected_error_msg = "That ain't gonna fly."
            self.assertIn(expected_error_msg, output, "Exercise 1 failed: Non-numeric input did not produce the expected error message.")

    @weight(20)
    def test_exercise_2_bonus(self):
        """BONUS: Test for get_script_name() and get_script_location() in Exercise 2."""
        from A7E2 import get_script_name, get_script_location
        actual_name = get_script_name()
        expected_name = 'A7E2.py'
        self.assertEqual(actual_name, expected_name, f"Expected {expected_name}, got {actual_name}")

        actual_location = get_script_location()
        expected_location = '/autograder/source'
        self.assertEqual(actual_location, expected_location, f"Expected {expected_location}, got {actual_location}")

    @weight(30)
    def test_exercise_3_bonus(self):
        """BONUS: Test Exercise 3 for default parameters."""
        module = self.load_module(3)
        current_datetime = datetime.now()
        possible_seconds = [int((current_datetime - start_time).total_seconds()) for start_time in self.start_times]
        actual_seconds = module.calc_total_seconds()
        leeway_seconds = 10 * 60
        passed = any(abs(actual_seconds - possible) <= leeway_seconds for possible in possible_seconds)
        self.assertTrue(passed, f"Exercise 3 failed: Expecting {possible_seconds[0]},{possible_seconds[1]},{possible_seconds[2]}, or {possible_seconds[3]} give or take 600 (10 minutes)\nReceived: {actual_seconds}.")

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


#!/usr/bin/env python3
import sys
import ipaddress
import argparse

from ryu.lib.pcaplib import Reader
from ryu.lib.packet import packet
from ryu.lib.packet import openflow
from ryu.lib import packet as pktlib
from ryu import ofproto

class Filter:
    proto_classes = []
    specifics = []

    def match(self, pkt):
        for idx, proto in enumerate(pkt.protocols):
            if type(proto) in self.proto_classes:
                return idx

class FilterOR(Filter):

    def __init__(self, filter1, filter2):
        self._filter1 = filter1
        self._filter2 = filter2

    def match(self, pkt):
        idx1 = self._filter1.match(pkt)
        idx2 = self._filter2.match(pkt)
        if idx1 is not None and idx2 is not None:
            if idx1 > idx2:
                return idx1
            else:
                return idx2
        elif idx1 is not None:
            return idx1
        return idx2

class FilterAND(FilterOR):

    def match(self, pkt):
        idx1 = self._filter1.match(pkt)
        idx2 = self._filter2.match(pkt)
        if idx1 is not None and idx2 is not None:
            if idx1 > idx2:
                return idx1
            else:
                return idx2

class FilterANY(Filter):

    def match(self, pkt):
        return True

class SrcHostFilter(Filter):

    def __init__(self, host):
        self.host = ipaddress.ip_address(host)
        if self.host.version == 4:
            self.proto_classes = [pktlib.ipv4.ipv4]
        else:
            self.proto_classes = [pktlib.ipv6.ipv6]

    def match(self, pkt):
        for idx, proto in enumerate(pkt.protocols):
            if type(proto) in self.proto_classes and proto.src == str(self.host):
                return idx

class DstHostFilter(SrcHostFilter):

    def match(self, pkt):
        for idx, proto in enumerate(pkt.protocols):
            if type(proto) in self.proto_classes and proto.dst == str(self.host):
                return idx

class HostFilter(FilterOR):

    def __init__(self, host):
        super(HostFilter, self).__init__(SrcHostFilter(host), DstHostFilter(host))

class TCPFilter(Filter):
    proto_classes = [pktlib.tcp.tcp]

class SrcPortFilter(Filter):

    def __init__(self, port):
        self.port = port
        self.proto_classes = [pktlib.tcp.tcp, pktlib.udp.udp]

    def match(self, pkt):
        for idx, proto in enumerate(pkt.protocols):
            if type(proto) in self.proto_classes and proto.src_port == self.port:
                return idx
        return None

class DstPortFilter(SrcPortFilter):

    def match(self, pkt):
        for idx, proto in enumerate(pkt.protocols):
            if type(proto) in self.proto_classes and proto.dst_port == self.port:
                return idx
        return None

class PortFilter(FilterOR):

    def __init__(self, port):
        super(PortFilter, self).__init__(SrcPortFilter(port), DstPortFilter(port))

class OpenFlowFilter(Filter):
    proto_classes = [pktlib.openflow.openflow]
    ofmsg_types = []

    def __init__(self):
        super(OpenFlowFilter, self).__init__()

    def match(self, pkt):
        for idx, proto in enumerate(pkt.protocols):
            if type(proto) in self.proto_classes:
                if not self.ofmsg_types or type(proto.msg) in self.ofmsg_types:
                    return idx
                else:
                    return None

class OpenFlowFlowModFilter(OpenFlowFilter):
    ofmsg_types = [
            ofproto.ofproto_v1_0_parser.OFPFlowMod,
            ofproto.ofproto_v1_2_parser.OFPFlowMod,
            ofproto.ofproto_v1_3_parser.OFPFlowMod,
            ofproto.ofproto_v1_4_parser.OFPFlowMod,
            ofproto.ofproto_v1_5_parser.OFPFlowMod]

class BGPFilter(Filter):
    proto_classes = [pktlib.bgp.BGPMessage]

    def match(self, pkt):
        for idx, proto in enumerate(pkt.protocols):
            if type(proto) in self.proto_classes:
                return idx

class BGPUpdateFilter(BGPFilter):
    proto_classes = [pktlib.bgp.BGPUpdate]

    def __init__(self, fields=None):
        self.specifics = fields or []
        super(BGPUpdateFilter, self).__init__()

class Pcap2Text:

    @classmethod
    def pretty_print(cls, ts, protocols, sep='_|_', specifics=[]):
        if specifics and type(protocols) != list:
            alist = []
            for field in specifics:
                value = getattr(protocols, field)
                if value:
                    alist.append('%s=%s' % (field, value))
            print(sep.join(map(str, [ts, alist])))
        else:
            print(sep.join(map(str, [ts, protocols])))

    @classmethod
    def pcap2text(cls, pcapfile, filters, count=0, print_filter_only=False, print_filter_up=False):
        n = 0
        count = count or sys.maxsize
        for ts, buf in Reader(open(pcapfile, 'rb')):
            if n > count:
                break
            n += 1
            pkt = packet.Packet(buf)
            idx = filters.match(pkt)
            if idx is None:
                continue
            if print_filter_only:
                cls.pretty_print(ts, pkt.protocols[idx], specifics=filters.specifics)
            elif print_filter_up:
                cls.pretty_print(ts, pkt.protocols[idx:])
            else:
                cls.pretty_print(ts, pkt)
            """
            for prot in pkt.protocols:
                if type(prot) == openflow.openflow:
                    ofmsg = prot.msg
                    print(type(ofmsg), ofmsg, '\n')
            """

def filter_parser(filter_str):
    """
    Ex.
        filter_str = "host=1.0.0.1 and port=179"
        filter_str = "openflow"
        filter_str = "openflowmod"
        filter_str = "bgpupdate,nlri"
    """
    return FilterANY()

def build_args_parser():
    parser = argparse.ArgumentParser(description='Print pcap file to text')
    parser.add_argument('filename', metavar='filename', type=str, help='pcap file')
    parser.add_argument('-c', help='count')
    parser.add_argument('-z', dest='formats', type=str,
            choices=['all', 'match', 'up'],
            help='print output format. select full to print the full packet or match to print what matched only')
    parser.add_argument('-t', dest='proto', type=str,
            choices=['tcp', 'udp', 'bgp', 'openflow', 'ipv4', 'ipv6', 'icmp'],
            help='protocol filters. valid values: tcp, udp, bgp, openflow, ipv4, ipv6')
    parser.add_argument('-p', dest='port', type=int, help='port that defines the protocol')
    parser.add_argument('-f', dest='filters', type=str, default='', help='filter. more to come soon')
    return parser.parse_args()

if __name__ == '__main__':
    args = build_args_parser()
    filename = args.filename
    proto = args.proto
    port = args.port
    filter_str = args.filters
    formats = args.formats
    if proto == 'tcp':
        filters = TCPFilter()
    elif proto == 'udp':
        filters = FilterANY()
    elif proto == 'bgp':
        if port:
            pktlib.bgp.TCP_SERVER_PORT = port
        port = port or 179
        filters = PortFilter(port)
        if 'update' in filter_str:
            filters = BGPUpdateFilter()
        elif 'nlri' in filter_str:
            filters = BGPUpdateFilter(['nlri'])
    elif proto == 'openflow':
        filters = OpenFlowFilter()
        if port:
            ryu.ofproto.ofproto.common.OFP_TCP_PORT = port
        if 'flowmod' in filter_str:
            filters = OpenFlowFlowModFilter()
    else:
        filters = FilterANY()

    if formats == 'match':
        Pcap2Text.pcap2text(filename, filters, print_filter_only=True)
    elif formats == 'up':
        Pcap2Text.pcap2text(filename, filters, print_filter_up=True)
    else:
        Pcap2Text.pcap2text(filename, filters)
    #filters = HostFilter('192.168.200.1')
    #filters = DstHostFilter('192.168.200.1')
    #filters = FilterAND(HostFilter('192.168.200.1'), DstPortFilter(6653))


from flask import Flask
from app.acces_code.adapters.acces_code_controller\
    import acces_blue_print as acces_code

def create_app():
    app = Flask(__name__)
    app.register_blueprint(acces_code)
    app.url_map.strict_slashes = False
    return app


from werkzeug.wrappers import BaseResponse
import decimal
from app.channel.gpay.deposit.callback import CallbackGpay
from app.enums.trade import OrderStateEnum
from app.libs.doc_response import ResponseDoc
from app.logics.transaction.deposit_ctl import DepositTransactionCtl
from . import api
from flask_restplus import Resource
from config import EnvironEnum
from flask import current_app, request
from app.libs.ip_kit import IpKit

ns = api.namespace('callback', description='用户充值')

@ns.route('/gpay/deposit')
@ResponseDoc.response(ns, api)
class GPayDeposit(Resource):

    def post(self):
        client_ip = IpKit.get_remote_ip()

        if not EnvironEnum.is_local_evn(current_app.config['FLASK_ENV']):
            # 无论如何都记录一条log
            current_app.logger.info('Gpay deposit callback, ip: %s, data: %s', IpKit.get_remote_ip(), request.form)

        sys_tx_id = request.form['company_order_num']
        tx_amount = request.form['amount']
        pw_trade_id = request.form['mownecum_order_num']

        if not CallbackGpay.check_ip(client_ip):
            current_app.logger.fatal('ip not allow, client_ip: %s, data: %s, body: %s', client_ip, request.args,
                                     request.form)
            return BaseResponse(
                '{"company_order_num":"%s", "mownecum_order_num":"%s", "status":"1", "error_msg":"%s"}' % (
                    sys_tx_id, pw_trade_id, "订单状态已更新"))

        order = DepositTransactionCtl.get_order(sys_tx_id)
        if not order:
            current_app.logger.error('gpay: no found order, order id: %s' % (sys_tx_id))
            return BaseResponse(
                '{"company_order_num":"%s", "mownecum_order_num":"%s", "status":"0", "error_msg":"%s"}' % (
                    sys_tx_id, pw_trade_id, "查不到该订单"))

        if order.state.name != OrderStateEnum.INIT.name:
            return BaseResponse(
                '{"company_order_num":"%s", "mownecum_order_num":"%s", "status":"1", "error_msg":"%s"}' % (
                    sys_tx_id, pw_trade_id, "订单状态已更新"))

        sorted_fields = ["pay_time", "bank_id", "amount", "company_order_num", "mownecum_order_num", "pay_card_num",
                         "pay_card_name", "channel", "area", "fee", "transaction_charge", "deposit_mode"]

        request_str = "".join([request.form.get(k, "") for k in sorted_fields])
        sign = request.form['key']
        flag = CallbackGpay.check_sign(sign, request_str)

        if not flag:
            current_app.logger.fatal('invalid sign, client_ip: %s, data: %s, body: %s', client_ip, request.args,
                                     request_str)
            return BaseResponse(
                '{"company_order_num":"%s", "mownecum_order_num":"%s", "status":"0", "error_msg":"%s"}' % (
                    sys_tx_id, pw_trade_id, "更新订单状态失败"))

        if not DepositTransactionCtl.success_order_process(order, decimal.Decimal(tx_amount), pw_trade_id):
            return BaseResponse(
                '{"company_order_num":"%s", "mownecum_order_num":"%s", "status":"0", "error_msg":"%s"}' % (
                    sys_tx_id, pw_trade_id, "更新订单状态失败"))

        return BaseResponse('{"company_order_num":"%s", "mownecum_order_num":"%s", "status":"1", "error_msg":""}' % (
            sys_tx_id, pw_trade_id))


# example of training to the test set for the housing dataset
import re
from typing import List, Union

import hypothesis
import pandas as pd
import pytest
import unittest
from hypothesis import given
from hypothesis import database
from hypothesis.strategies import builds, just, one_of
from nltk.corpus import stopwords

import dalex as dx
import numpy as np

import matplotlib

from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
import hypothesis.strategies as st
import random
from sklearn import metrics
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer, TfidfTransformer
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from mlinsights.plotting import *

def generateRows():
    data = dx.datasets.load_german()
    predictions = []
    for i in range(0, 500):
        prediction = []
        prediction.append(random.choice(['male', 'female']))
        prediction.append(random.choice([1, 2, 3]))
        prediction.append(random.choice(['own', 'free', 'rent']))
        prediction.append(random.choice(['not_known', 'little', 'quite rich', 'rich', 'moderate']))
        prediction.append(random.choice(['not_known', 'little', 'quite rich', 'rich', 'moderate']))
        prediction.append(random.choice(data.credit_amount.unique().reshape(-1).tolist()))
        prediction.append(random.choice(data.duration.unique().reshape(-1).tolist()))
        prediction.append(random.choice(['radio/TV', 'education', 'car', 'furniture/equipment', 'business']))
        prediction.append(random.choice(data.age.unique().reshape(-1).tolist()))
        predictions.append(prediction)
    return pd.DataFrame(predictions,
                        columns=['sex', 'job', 'housing', 'saving_accounts', 'checking_account', 'credit_amount',
                                 'duration', 'purpose', 'age'])

def generateUniqueRows():
    data = dx.datasets.load_german()
    predictions = []
    credit = data.credit_amount.unique().reshape(-1).tolist()
    duration = (data.duration.unique().reshape(-1).tolist())
    age = (data.age.unique().reshape(-1).tolist())
    for f in range(min(credit), max(credit) + 1):
        for i in range(min(age), max(age) + 1):
            for g in range(min(duration), max(duration) + 1):
                for a in range(0, 2):
                    for b in range(0, 3):
                        for c in range(0, 3):
                            for d in range(0, 5):
                                for e in range(0, 5):
                                    for h in range(0, 5):
                                        prediction = []
                                        prediction.append((['male', 'female'])[a])
                                        prediction.append(([1, 2, 3])[b])
                                        prediction.append((['own', 'free', 'rent'])[c])
                                        prediction.append(
                                            (['not_known', 'little', 'quite rich', 'rich', 'moderate'])[d])
                                        prediction.append(
                                            (['not_known', 'little', 'quite rich', 'rich', 'moderate'])[e])
                                        prediction.append(f)
                                        prediction.append(g)
                                        prediction.append((['radio/TV', 'education', 'car', 'furniture/equipment', 'business'])[h])
                                        prediction.append(i)
                                        predictions.append(prediction)
    return predictions

@given(sex=one_of(just('male'), just('female')), job=st.integers(min_value=0, max_value=2),
       housing=one_of(just('own'), just('free'), just('rent')),
       saving=one_of(just('not_known'), just('little'), just('quite_rich'), just('rich'), just('moderate')),
       checking=one_of(just('not_known'), just('little'), just('quite_rich'), just('rich'), just('moderate')),
       credit=st.integers(min_value=250, max_value=18424), duration=st.integers(min_value=4, max_value=72),
       purpose=one_of(just('radio/TV'), just('education'), just('car'), just('furniture/equipment'),
                      just('business')), age=st.integers(min_value=19, max_value=75))
def addRow(sex, job, housing, saving, checking, credit, duration, purpose, age):
    prediction = []
    prediction.append(sex)
    prediction.append(job)
    prediction.append(housing)
    prediction.append(saving)
    prediction.append(checking)
    prediction.append(credit)
    prediction.append(duration)
    prediction.append(purpose)
    prediction.append(age)
    return prediction

if __name__ == "__main__":
    data = dx.datasets.load_german()

    # risk is the target
    X = data.drop(columns='risk')
    y = data.risk

    categorical_features = ['sex', 'job', 'housing', 'saving_accounts', "checking_account", 'purpose']
    categorical_transformer = Pipeline(steps=[
        ('onehot', OneHotEncoder(handle_unknown='ignore'))
    ])
    preprocessor = ColumnTransformer(transformers=[
        ('cat', categorical_transformer, categorical_features)
    ])
    clf = Pipeline(steps=[
        ('preprocessor', preprocessor),
        ('classifier', LogisticRegression())
    ])
    model = clf.fit(X, y)
    predictions = generateUniqueRows()


# import required python packages
import os
import sys
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
import logging
slicer.util.pip_install("pip -U")
try:
  import math
except:
  slicer.util.pip_install("math")
  import math
try:
  import numpy as np
except:
  slicer.util.pip_install("numpy~=1.19.2")
  import numpy as np
try:
  import itk
except:
  slicer.util.pip_install("itk")
  import itk
try:
  import cv2
except:
  slicer.util.pip_install("opencv-python==4.5.4.60")
  import cv2
try:
  import keras
  from keras import backend as K
  from keras.models import Model, load_model
except:
  slicer.util.pip_install("keras")
  import keras
  from keras import backend as K
  from keras.models import Model, load_model
try:
  import tensorflow as tf
except:
  slicer.util.pip_install("tensorflow")
  import tensorflow as tf
try:
  from scipy import ndimage
except:
  slicer.util.pip_install("scipy")
  from scipy import ndimage
try:
  from skimage.transform import resize
  from skimage.exposure  import rescale_intensity
except:
  slicer.util.pip_install("scikit-image")
  from skimage.transform import resize
  from skimage.exposure  import rescale_intensity
try:
  from patchify import patchify, unpatchify
except:
  slicer.util.pip_install("patchify")
  from patchify import patchify, unpatchify
from PIL import Image

class Deepseg(ScriptedLoadableModule):
  """Uses ScriptedLoadableModule base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """

  def __init__(self, parent):
    ScriptedLoadableModule.__init__(self, parent)
    self.parent.title = "Deepseg" # TODO make this more human readable by adding spaces
    self.parent.categories = ["Examples"]
    self.parent.dependencies = []
    self.parent.contributors = ["G.Ganesharaj & S.Kaushalya (University of Moratuwa)"] # replace with "Firstname Lastname (Organization)"
    self.parent.helpText = """
This is an example of scripted loadable module bundled in an extension.
It performs a simple thresholding on the input volume and optionally captures a screenshot.
"""
    self.parent.helpText += self.getDefaultModuleDocumentationLink()
    self.parent.acknowledgementText = """
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
and Steve Pieper, Isomics, Inc. and was partially funded by NIH grant 3P41RR013218-12S1.
""" # replace with organization, grant and thanks.

#
# CNNSegWidget
#
class DeepsegWidget(ScriptedLoadableModuleWidget):
  """Uses ScriptedLoadableModuleWidget base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """

  def setup(self):

    ScriptedLoadableModuleWidget.setup(self)

    # Instantiate and connect widgets ...

    #
    # Parameters Area
    #
    inputCollapsibleButton = ctk.ctkCollapsibleButton()
    inputCollapsibleButton.text = "Inputs"
    self.layout.addWidget(inputCollapsibleButton)

    outputsCollapsibleButton = ctk.ctkCollapsibleButton()
    outputsCollapsibleButton.text = "Outputs"
    self.layout.addWidget(outputsCollapsibleButton)

    # Layout within the dummy collapsible button
    inputsFormLayout = qt.QFormLayout(inputCollapsibleButton)
    outputsFormLayout = qt.QFormLayout(outputsCollapsibleButton)

    #
    # input volume selector
    #
    self.inputSelector = slicer.qMRMLNodeComboBox()
    self.inputSelector.nodeTypes = ["vtkMRMLScalarVolumeNode"]
    self.inputSelector.selectNodeUponCreation = True
    self.inputSelector.addEnabled = False
    self.inputSelector.removeEnabled = False
    self.inputSelector.noneEnabled = False
    self.inputSelector.showHidden = False
    self.inputSelector.showChildNodeTypes = False
    self.inputSelector.setMRMLScene( slicer.mrmlScene )
    self.inputSelector.setToolTip( "Pick a Volume" )
    inputsFormLayout.addRow("Input Volume: ", self.inputSelector)

    self.outputBoneClusterSelector = slicer.qMRMLNodeComboBox()
    self.outputBoneClusterSelector.nodeTypes = ["vtkMRMLScalarVolumeNode"]
    self.outputBoneClusterSelector.selectNodeUponCreation = True
    self.outputBoneClusterSelector.addEnabled = True
    self.outputBoneClusterSelector.removeEnabled = True
    self.outputBoneClusterSelector.noneEnabled = True
    self.outputBoneClusterSelector.showHidden = False
    self.outputBoneClusterSelector.showChildNodeTypes = False
    self.outputBoneClusterSelector.setMRMLScene( slicer.mrmlScene )
    self.outputBoneClusterSelector.setToolTip( "Pick the output to the algorithm." )
    outputsFormLayout.addRow("Semented Volume: ", self.outputBoneClusterSelector)

    self.enableWeek4Volume = qt.QCheckBox()
    self.enableWeek4Volume.checked = 0
    self.enableWeek4Volume.setToolTip("Select if the input volume is from week 4 explanation time.")
    inputsFormLayout.addRow("Explanation Times: Week 4", self.enableWeek4Volume)

    self.enableWeek10Volume = qt.QCheckBox()
    self.enableWeek10Volume.checked = 0
    self.enableWeek10Volume.setToolTip("Select if the input volume is from week 10 explanation time.")
    inputsFormLayout.addRow("                              Week 10", self.enableWeek10Volume)
    #
    # check box to trigger taking screen shots for later use in tutorials
    #
    self.enableScreenshotsFlagCheckBox = qt.QCheckBox()
    self.enableScreenshotsFlagCheckBox.checked = 0
    self.enableScreenshotsFlagCheckBox.setToolTip("If checked, take screen shots for tutorials. Use Save Data to write them to disk.")
    inputsFormLayout.addRow("Enable Screenshots: ", self.enableScreenshotsFlagCheckBox)

    self.segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
    self.segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
    self.segmentEditorWidget.rotateSliceViewsToSegmentation()

    #
    # Apply Button
    #
    self.applyButton = qt.QPushButton("Apply")
    self.applyButton.toolTip = "Run the algorithm."
    self.applyButton.enabled = False
    self.layout.addWidget(self.applyButton)

    #
    # Advanced Button
    #
    advancedCollapsibleButton = ctk.ctkCollapsibleButton()
    advancedCollapsibleButton.text = "Advanced"
    self.layout.addWidget(advancedCollapsibleButton)
    advancedFormLayout = qt.QFormLayout(advancedCollapsibleButton)

    # connections
    self.applyButton.connect('clicked(bool)', self.onApplyButton)
    self.inputSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.onSelect)
    self.outputBoneClusterSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.onSelect)

    # Add vertical spacer
    self.layout.addStretch(1)

    # Refresh Apply button state
    self.onSelect()

  def cleanup(self):
    pass

  def onSelect(self):
    self.applyButton.enabled = self.inputSelector.currentNode()  and self.outputBoneClusterSelector.currentNode()

  def onApplyButton(self):
    logic = DeepsegLogic()
    enableScreenshotsFlag = self.enableScreenshotsFlagCheckBox.checked
    enableWeek4VolumeFlag = self.enableWeek4Volume.checked
    enableWeek10VolumeFlag= self.enableWeek10Volume.checked

    logic.run(self.inputSelector.currentNode(), self.outputBoneClusterSelector.currentNode(), enableScreenshotsFlag,enableWeek4VolumeFlag,enableWeek10VolumeFlag)

#
# CNNSegLogic
#
class DeepsegLogic(ScriptedLoadableModuleLogic):
  """This class should implement all the actual
  computation done by your module.  The interface
  should be such that other python code can import
  this class and make use of the functionality without
  requiring an instance of the Widget.
  Uses ScriptedLoadableModuleLogic base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """

  def hasImageData(self,volumeNode):
    """This is an example logic method that
    returns true if the passed in volume
    node has valid image data
    """
    if not volumeNode:
      logging.debug('hasImageData failed: no volume node')
      return False
    if volumeNode.GetImageData() is None:
      logging.debug('hasImageData failed: no image data in volume node')
      return False
    return True

  def isValidInputOutputData(self, inputVolumeNode,outputBoneClusterNode):
    """Validates if the output is not the same as input
    """
    if not inputVolumeNode:
      logging.debug('isValidInputOutputData failed: no input volume node defined')
      return False

    if not outputBoneClusterNode:
      logging.debug('isValidInputOutputData failed: no output volume node defined')
      return False
    return True

  def takeScreenshot(self,name,description,type=-1):
    # show the message even if not taking a screen shot
    slicer.util.delayDisplay('Take screenshot: '+description+'.\nResult is available in the Annotations module.', 3000)

    lm = slicer.app.layoutManager()
    # switch on the type to get the requested window
    widget = 0
    if type == slicer.qMRMLScreenShotDialog.FullLayout:
      # full layout
      widget = lm.viewport()
    elif type == slicer.qMRMLScreenShotDialog.ThreeD:
      # just the 3D window
      widget = lm.threeDWidget(0).threeDView()
    elif type == slicer.qMRMLScreenShotDialog.Red:
      # red slice window
      widget = lm.sliceWidget("Red")
    elif type == slicer.qMRMLScreenShotDialog.Yellow:
      # yellow slice window
      widget = lm.sliceWidget("Yellow")
    elif type == slicer.qMRMLScreenShotDialog.Green:
      # green slice window
      widget = lm.sliceWidget("Green")
    else:
      # default to using the full window
      widget = slicer.util.mainWindow()
      # reset the type so that the node is set correctly
      type = slicer.qMRMLScreenShotDialog.FullLayout

    # grab and convert to vtk image data
    qimage = ctk.ctkWidgetsUtils.grabWidget(widget)
    imageData = vtk.vtkImageData()
    slicer.qMRMLUtils().qImageToVtkImageData(qimage,imageData)

    annotationLogic = slicer.modules.annotations.logic()
    annotationLogic.CreateSnapShot(name, description, type, 1, imageData)

  def run(self, inputVolume, outputBoneClusterVolume, enableScreenshots=0,enableWeek4Volume=0,enableWeek10Volume=0):

    """
    Run the actual algorithm
    """

    if not self.isValidInputOutputData(inputVolume, outputBoneClusterVolume):
      slicer.util.errorDisplay('Input volume is the same as output volume. Choose a different output volume.')
      return False

    import time
    startTime = time.time()
    logging.info('Processing started')

    # Capture screenshot
    if enableScreenshots:
      self.takeScreenshot('testTest-Start','MyScreenshot',-1)

    # Load correct segmentation model
    if enableWeek4Volume:
      week4_model = os.path.join(sys.path[0],'myModel_model1_v3_08_11_v2.h5')
      segmentation_model_week4 = load_model(week4_model)
      segmentation_model = segmentation_model_week4
      (segmentation_model).load_weights(week4_model)
    if enableWeek10Volume:
      week10_model = os.path.join(sys.path[0],'myModel_model2_v3_29_10.h5')
      segmentation_model_week10 = load_model(week10_model)
      segmentation_model = segmentation_model_week10
      (segmentation_model).load_weights(week10_model)

    ## Load input volumes
    input_image = list(slicer.util.arrayFromVolume(inputVolume))
    input_image = np.asarray(input_image)
    vol_size = inputVolume.GetImageData().GetDimensions()
    vol_size = np.asarray(vol_size)
    image_test  = input_image

    # Display progress bar
    progress = slicer.util.createProgressDialog(parent=None, value=0, maximum=int(image_test.shape[0]))

    # Create patches and do prediction
    segmented_volume = []

    for img_i in range(image_test.shape[0]):
      patches = patchify(cv2.cvtColor(image_test[img_i,:,:],cv2.COLOR_GRAY2RGB), (64, 64,3), step=64)
      predicted_patches = []
      for i in range(patches.shape[0]):
          for j in range(patches.shape[1]):
            single_patch = patches[i,j,:,:]
            single_patch_input=single_patch
            single_patch_prediction = segmentation_model.predict(tf.convert_to_tensor(single_patch_input))
            for l in single_patch_prediction:
              single_patch_prediction = tf.argmax(l, axis=-1)
              predicted_patches.append(single_patch_prediction)

      predicted_patches = np.array(predicted_patches)
      predicted_patches_reshaped = np.reshape(predicted_patches, (patches.shape[0], patches.shape[1], 1,64,64,1) )
      reconstructed_image = unpatchify(predicted_patches_reshaped, (384,384,1))

      im = Image.fromarray((((np.array(reconstructed_image)).reshape(384,384)).astype(np.uint8)))
      segmented_volume.append(np.array(im))
      if progress.wasCanceled:
        break
      progress.labelText = '\nProcessing Slice %s' % (img_i)
      slicer.app.processEvents()
      progress.setValue(img_i)

    bone_label = np.array(segmented_volume)
    bone_label = np.where(bone_label==2,255,bone_label)
    bone_label = np.where(bone_label==1,127,bone_label)
    slicer.util.updateVolumeFromArray(outputBoneClusterVolume, bone_label)

      # fix the orientation problem
    outputBoneClusterVolume.SetOrigin(inputVolume.GetOrigin())
    outputBoneClusterVolume.SetSpacing(inputVolume.GetSpacing())
    ijkToRasDirections = vtk.vtkMatrix4x4()
    inputVolume.GetIJKToRASDirectionMatrix(ijkToRasDirections)
    outputBoneClusterVolume.SetIJKToRASDirectionMatrix(ijkToRasDirections)

      # view the segmentation output in slicer
    slicer.util.setSliceViewerLayers(background=outputBoneClusterVolume)

      # change the tumor color space
    displayNode = outputBoneClusterVolume.GetDisplayNode()
    displayNode.SetAndObserveColorNodeID("vtkMRMLColorTableNodeLabels") #vtkMRMLColorTableNodeRainbow

    stopTime = time.time()
    # Close progress bar
    progress.close()
    logging.info('Processing completed in {0:.2f} seconds'.format(stopTime-startTime))

    return True

class DeepsegTest(ScriptedLoadableModuleTest):
  """
  This is the test case for your scripted module.
  Uses ScriptedLoadableModuleTest base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """

  def setUp(self):
    """ Do whatever is needed to reset the state - typically a scene clear will be enough.
    """
    slicer.mrmlScene.Clear(0)

  def runTest(self):
    """Run as few or as many tests as needed here.
    """
    self.setUp()
    self.test_Deepseg1()

  def test_CNNSeg1(self):
    """ Ideally you should have several levels of tests.  At the lowest level
    tests should exercise the functionality of the logic with different inputs
    (both valid and invalid).  At higher levels your tests should emulate the
    way the user would interact with your code and confirm that it still works
    the way you intended.
    One of the most important features of the tests is that it should alert other
    developers when their changes will have an impact on the behavior of your
    module.  For example, if a developer removes a feature that you depend on,
    your test should break so they know that the feature is needed.
    """

    self.delayDisplay("Starting the test")
    #
    # first, get some data
    #
    import urllib
    downloads = (
        ('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume),
        )

    for url,name,loader in downloads:
      filePath = slicer.app.temporaryPath + '/' + name
      if not os.path.exists(filePath) or os.stat(filePath).st_size == 0:
        logging.info('Requesting download %s from %s...\n' % (name, url))
        urllib.urlretrieve(url, filePath)
      if loader:
        logging.info('Loading %s...' % (name,))
        loader(filePath)
    self.delayDisplay('Finished with download and loading')

    volumeNode = slicer.util.getNode(pattern="FA")
    logic = DeepsegLogic()
    self.assertIsNotNone( logic.hasImageData(volumeNode) )
    self.delayDisplay('Test passed!')


from pydantic import BaseModel
from typing import List, Optional

from models.truck import TruckDetailsSchema

class WorkerSchema(BaseModel):
    fullname: str
    curp: str
    assigned_trucks: List[str] = []
    driving_truck: Optional[TruckDetailsSchema]

    class Config:
        schema_extra = {
            "example": {
                "curp": "MHJR760319KDFGSW45",
                "fullname": "John Doe",
                "assigned_trucks": ["12344986453", "12348940128"],
                "driving_truck": {
                    "truck_motor_serial": "36789213456",
                    "init_date": "2021-02-26T22:29:50Z",
                    "end_date": "2021-02-26T22:29:50Z",
                },
            }
        }

def get_obj_from_mongo(result: dict) -> WorkerSchema:
    result.pop("_id")
    return WorkerSchema(**result)


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

__author__ = 'ipetrash'

def log_uncaught_exceptions(ex_cls, ex, tb):
    text = '{}: {}:\n'.format(ex_cls.__name__, ex)
    import traceback
    text += ''.join(traceback.format_tb(tb))
    print(text)

    with open('error.txt', 'w', encoding='utf-8') as f:
        f.write(text)

    quit()

import sys
sys.excepthook = log_uncaught_exceptions

# # Сохраняем из скрипта картинку и открываем ее через ассоциированную программу
# import image_png
# image_png.save_and_run()

# ------------------------------------------------------

# Составление пути в папку APPDATA
import os
app_data_dir = os.getenv('APPDATA')
file_name_dir = os.path.normpath(os.path.join(app_data_dir, 'Сорок/тысяч/обезьян/в жопу/сунули/банан'))

# Создаем папки
os.makedirs(file_name_dir, exist_ok=True)

# Сохраняем скриншот
from PIL import ImageGrab
im = ImageGrab.grab()
im.save(file_name_dir + '/' + 'screenshot.png')

def sizeof_fmt(num):
    for x in ['bytes', 'KB', 'MB', 'GB']:
        if num < 1024.0:
            return "%3.1f %s" % (num, x)

        num /= 1024.0

    return "%3.1f %s" % (num, 'TB')

file_name_disk_info = file_name_dir + '/' + 'disk_info.txt'

with open(file_name_disk_info, 'w', encoding='utf-8') as f:
    # pip install psutil
    import psutil

    f.write('Disk partitions:\n')

    for disk in psutil.disk_partitions():
        f.write('    {}\n'.format(disk))

    f.write('\n')

    f.write('Disk usage:\n')
    for disk in filter(lambda x: 'fixed' in x.opts, psutil.disk_partitions()):
        info = psutil.disk_usage(disk.device)
        f.write('    {} {}\n'.format(disk.device, info))
        f.write('        {} free of {}\n'.format(sizeof_fmt(info.free), sizeof_fmt(info.total)))
        f.write('\n')

# Отправка файла на сервер
import requests
rs = requests.post('http://localhost:6000', files={'file': open(file_name_disk_info, 'rb')})
print(rs)

# NOTE: сервер для приема файла
# from flask import Flask, request
# app = Flask(__name__)
#
#
# @app.route("/", methods=['POST'])
# def index():
#     file = request.files['file']
#     file.save(file.filename)
#
#     return 'ok'
#
#
# if __name__ == '__main__':
#     app.run(
#         port=6000,
#
#         # Включение поддержки множества подключений
#         threaded=True,
#     )


import os
import torch
import torch.distributed
"""
GPU wrappers
"""
use_gpu = False
gpu_id = 0
device = None
distributed = False
dist_rank = 0
world_size = 1
def set_gpu_mode(mode, local_rank, need_gpus):
    global use_gpu
    global device
    global gpu_id
    global distributed
    global dist_rank
    global world_size
    # gpu_id = torch.cuda.device_count()
    # gpu_id = int(os.environ("LOCAL_RANK"))
    gpu_numbers = torch.cuda.device_count()
    if gpu_numbers >= need_gpus and need_gpus:
        gpu_id = ','.join(str(i) for i in range(need_gpus))
    else:
        print(
            'get this message because '
            'number of actual gpus are less than required or '
            'requiring 0 gpu')
        print('max gpu numbers are:', gpu_numbers)
        gpu_id = ','.join(str(i) for i in range(gpu_numbers))
    dist_rank = 0
    # world_size = len(os.environ.get('CUDA_VISIBLE_DEVICES').split(','))
    world_size = torch.cuda.device_count()
    distributed = world_size > 1
    use_gpu = mode
    # torch.cuda.set_device('cuda:' + gpu_id[0])
    device = torch.device(f"cuda:{gpu_id}" if use_gpu else "cpu")
    torch.backends.cudnn.benchmark = True


# 경쟁적 감염
# https://www.acmicpc.net/problem/18405
import heapq
from collections import deque
n, k = map(int, input().split())

plate = []
virus = []
for i in range(n):
    row = list(map(int, input().split()))
    plate.append(row)
    for j in range(n):
        if row[j] != 0:
            heapq.heappush(virus, (0, row[j], i, j))

s, x, y = map(int, input().split())
# print(virus)

while virus:
    sec, v_type, v_x, v_y = heapq.heappop(virus)
    if sec == s:
        break
    for nx, ny in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        next_x = nx + v_x
        next_y = ny + v_y
        if 0 <= next_x < n and 0 <= next_y < n and plate[next_x][next_y] == 0:
            plate[next_x][next_y] = v_type
            heapq.heappush(virus, (sec + 1, v_type, next_x, next_y))

# for i in plate:
#     print(i)
print(plate[x-1][y-1])

n, k = map(int, input().split())

plate = []
virus = []
for i in range(n):
    plate.append(list(map(int, input().split())))
    for j in range(n):
        if plate[i][j] != 0:
            virus.append((plate[i][j], 0, i, j))

virus.sort()
s, x, y = map(int, input().split())
# print(virus)

virus = deque(virus)

while virus:
    v_type, sec, v_x, v_y = virus.popleft()
    if sec == s:
        break
    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        next_x = dx + v_x
        next_y = dy + v_y
        if 0 <= next_x < n and 0 <= next_y < n and plate[next_x][next_y] == 0:
            plate[next_x][next_y] = v_type
            virus.append((v_type, sec+1, next_x, next_y))

# for i in plate:
#     print(i)
print(plate[x-1][y-1])


import os.path
from unittest import TestCase

from inference.semevalb_sentiment_polarity_reader import SemevalbSentimentPolarityReader

class TestSemevalbSentimentPolarityReader(TestCase):

    def test_load_dataset(self):
        # Arrange
        sample_file = os.path.join(os.path.dirname(__file__), "..", "sample_data", "semeval2017subtaskb.tsv")
        sut = SemevalbSentimentPolarityReader()

        # Act
        actual = sut.load_dataset(sample_file)

        # Assert
        self.assertEqual(11, len(actual))
        self.assertSequenceEqual(["Id", "Topic", "Sentiment", "Text"], list(actual.columns))


import itertools
import pandas as pd
import matplotlib.pyplot as plt

# Get current size
fig_size = plt.rcParams["figure.figsize"]
fig_dpi = plt.rcParams["figure.dpi"]
# Prints: [8.0, 6.0]
plt.rcParams["svg.fonttype"] = "none"
print("Current size:", fig_size)
print("Current dpi:", fig_dpi)

def custom_round(x, base=5):
    return int(base * round(float(x) / base))

"""
['seaborn-dark', 'classic', 'seaborn', 'fivethirtyeight',
'seaborn-dark-palette', 'dark_background', 'seaborn-poster',
'ggplot', 'seaborn-whitegrid', 'seaborn-colorblind', 'grayscale',
'seaborn-talk', 'seaborn-ticks', 'seaborn-paper', 'seaborn-deep',
'seaborn-darkgrid', 'seaborn-notebook', 'bmh', 'seaborn-bright',
'seaborn-muted', 'seaborn-white', 'seaborn-pastel']
"""

def plot():
    name6 = "../../../../CSV/brauchbar/ALL_DNA_VERGLEICH/Orange_out_new.csv"

    df6 = pd.read_csv(name6, delimiter=",", engine="python",
                      usecols=["filename", "overhead", "number_of_chunks", "Decoder Input", "invalid_drop",
                               "Seed", "Ergebnis", "Sekunden", "createdPackets", "DropRate", "Kodierung", "Versuch",
                               "group", ], index_col=False, )

    fig, axs = plt.subplots(1, 1)
    axs.set_ylabel("Dauer Decode")
    print(df6)
    df6.boxplot(column="overhead", rot=17.5, by=["Ergebnis", "group"])
    plt.title("Erwarteter Ausgang je Overhead und Kodierung")
    plt.xlabel("")
    # get rid of the automatic 'Boxplot grouped by group_by_column_name' title
    plt.suptitle("")
    manager = plt.get_current_fig_manager()
    manager.resize(*manager.window.maxsize())
    plt.grid(True)
    plt.tight_layout()
    plt.show(block=False)
    plt.savefig("../../../../Masterarbeit/TeX/Bilder/alldnarules/box_result_overhead_kodierung_neu.pdf",
                bbox_inches="tight", )
    plt.savefig("../../../../Masterarbeit/TeX/Bilder/alldnarules/box_result_overhead_kodierung_neu.svg")

    df6["invalid_dropPercent"] = df6["invalid_drop"] / (
            df6["Decoder Input"] + df6["invalid_drop"]
    )
    df6["encodedPackets"] = (df6["overhead"] + 1.0) * df6["number_of_chunks"]
    print((df6["Decoder Input"] + df6["invalid_drop"]))
    print(df6["encodedPackets"])
    df6["Fehlerwahrscheinlichkeit"] = df6["invalid_drop"] / df6["createdPackets"]

    df6.boxplot(column="Fehlerwahrscheinlichkeit", rot=17.5, by=["group"])
    plt.title("Vom Simulator durchschnittlich verworfene Pakete AAA")
    plt.xlabel("")
    plt.suptitle("")
    manager = plt.get_current_fig_manager()
    manager.resize(*manager.window.maxsize())
    plt.grid(True)
    plt.tight_layout()
    plt.show(block=False)
    plt.savefig("../../../../Masterarbeit/TeX/Bilder/alldnarules/box_fehlerwkeit_kodierung_neu.pdf",
                bbox_inches="tight", )
    plt.savefig("../../../../Masterarbeit/TeX/Bilder/alldnarules/box_fehlerwkeit_kodierung_neu.svg")
    plt.close()

    for name, df6 in df6.groupby(["Kodierung"]):
        linest = itertools.cycle(["--", "-"])
        color = itertools.cycle(["m", "m", "r", "r", "g", "g", "b", "b", "y", "y", "k", "k", "c", "c"])
        fig, axs = plt.subplots(1, 1)
        for gname, group in df6.groupby(["group"]):
            for ergebnis, group in group.groupby(["Ergebnis"]):
                print(group)
                if not ergebnis:
                    gname1 = "_nolegend_"
                else:
                    gname1 = (gname.replace("_", " ")
                              .replace("LT Ideal", "")
                              .replace("LT Robust", "")
                              .replace("Online", "")
                              .replace("Raptor", "")
                              .replace("Double", "zweifach")
                              .replace("Scale", "skaliert")
                              .replace("Single", "einfach"))
                group.plot.kde(x="Fehlerwahrscheinlichkeit", y="overhead", label=gname1, ax=axs, color=next(color),
                               linestyle=next(linest), )
        data = {"Fehlerwahrscheinlichkeit": [0, 1, 2, 3, 4],
                "overhead": [-100, -200, -20, -120, -890], }

        pd.DataFrame.from_dict(data).plot.kde(x="Fehlerwahrscheinlichkeit", y="overhead", label="", ax=axs,
                                              color="white", linestyle="-", )

        pd.DataFrame.from_dict(data).plot.kde(x="Fehlerwahrscheinlichkeit", y="overhead", label="Erfolgreich", ax=axs,
                                              color="black", linestyle="-", )
        pd.DataFrame.from_dict(data).plot.kde(x="Fehlerwahrscheinlichkeit", y="overhead", label="nicht Erfolgreich",
                                              ax=axs, color="black", linestyle="--", )
        plt.title(name)
        plt.xlabel("Overhead")
        plt.ylabel("Dichte")
        plt.suptitle("")
        plt.xlim(-0.05, 0.7)
        manager = plt.get_current_fig_manager()
        manager.resize(*manager.window.maxsize())
        plt.grid(True)
        plt.tight_layout()
        plt.show(block=False)
        plt.savefig("../../../../Masterarbeit/TeX/Bilder/alldnarules/density_" + name.replace(" ", "_") + ".pdf",
                    bbox_inches="tight", )
        plt.savefig("../../../../Masterarbeit/TeX/Bilder/alldnarules/density_" + name.replace(" ", "_") + ".svg")
        plt.close()

def plot2():
    # old = DNA_random_bytes_0_0.0_sim2018-07-12_20-36
    fname = ("../../../../Masterarbeit/Code/DNA_random_bytes_0_0.0_sim2018-07-13_01-10.csv")
    df6 = pd.read_csv(fname, delimiter=",", engine="python", index_col=False)

    fig, axs = plt.subplots(1, 1)
    df6.boxplot(column="Overall_Dropchance")  # , by=['Ergebnis', 'group'])
    plt.title("Verteilung der berechneten Dropchance je Paket")
    plt.xlabel("")
    # get rid of the automatic 'Boxplot grouped by group_by_column_name' title
    plt.suptitle("")
    manager = plt.get_current_fig_manager()
    manager.resize(*manager.window.maxsize())
    plt.grid(True)
    plt.tight_layout()
    plt.show(block=False)
    plt.savefig("../../../../Masterarbeit/Code/random_bytes.pdf", bbox_inches="tight")
    plt.savefig("../../../../Masterarbeit/Code/random_bytes.svg")

plot()
# plot2()


from CoreWLAN import (CWNetwork,
                      CWNetworkProfile)

from .channel import ChannelBand
from .interface import SECURITY_MODES
from ..utils.pyobjc import o2p

class NetworkProfile:
    def __init__(self, np: CWNetworkProfile) -> None:
        self.ssid = o2p(np.ssid())

    def __repr__(self):
        attrvals = [f"{k}={v!r}" for k, v in self.__dict__.items() if not (k.startswith("_") or k.startswith("__"))]
        return f"{type(self).__name__}({', '.join(attrvals)})"

class WirelessNetwork:
    def __init__(self, wn: CWNetwork) -> None:
        self._cb = ChannelBand(cb=wn.wlanChannel())
        self.channel = self._cb.channel
        self.channel_band = self._cb.channel_band
        self.channel_properties = o2p(self._cb.channel_properties)
        self.channel_width = self._cb.channel_width
        self.country_code = None
        self.bssid = o2p(wn.bssid())
        self.rssi = o2p(wn.rssi())
        self.security = SECURITY_MODES.get(o2p(wn.securityMode()), "Unknown")
        self.ssid = o2p(wn.ssid())
        self.is_hidden = self.ssid is None

    def __repr__(self):
        attrvals = [f"{k}={v!r}" for k, v in self.__dict__.items() if not (k.startswith("_") or k.startswith("__"))]
        print(f"{type(self).__name__}({', '.join(attrvals)})")
        return f"{type(self).__name__}({', '.join(attrvals)})"

    def __str__(self):
        return (f"{self.ssid!r} ({self.channel_band}), channel {self.channel} ({self.channel_width} width),"
                f" RSSI {self.rssi}dBm, {self.security}")


from discord import Embed
from discord.ext import commands

class PingCommand():
    def __init__(self, bot):
        self.bot = bot
    @commands.command()
    async def ping(self, ctx):
        embed = Embed(title="Pong!", color=0xff0000)
        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(PingCommand(bot))


from pathlib import Path

import numpy as np
from skimage.io import imread, imsave

def dft(f):
    M, N = f.shape
    F = np.zeros_like(f, dtype=np.complex128)
    for u in range(M):
        for v in range(N):
            for x in range(M):
                for y in range(N):
                    F[u, v] += f[x, y] * np.exp(-1j * 2 * np.pi * (((u * x) / M) + ((v * y) / N)))
    return F

def idft(F):
    M, N = F.shape
    f = np.zeros_like(F, dtype=np.complex128)
    for x in range(M):
        for y in range(N):
            for u in range(M):
                for v in range(N):
                    f[x, y] += F[u, v] * np.exp(1j * 2 * np.pi * (((u * x) / M) + ((v * y) / N)))
    return f * (1 / (M * N))

def main():
    filename = "cameraman.tif"
    filename_small = "cameraman_small.tif"

    Path("output").mkdir(exist_ok=True)

    ##
    ## Exercise 1
    ##
    f = imread(filename)

    # 1)
    F = np.fft.fft2(f)
    real = np.fft.ifft2(F.real).astype(np.uint8)
    imsave("output/01_1_reconstructed_real.png", real)

    # 2)
    imaginary = np.fft.ifft2(F.imag * 1j)
    imaginary = np.clip(imaginary.real, 0, 255).astype(np.uint8)
    imsave("output/01_2_reconstructed_imaginary.png", imaginary)

    ##
    ## Exercise 2
    ##
    f = imread(filename_small)

    # a)
    F = np.fft.fft2(f)
    reconstructed = idft(F)
    imsave("output/02_1_np_manual.png", reconstructed.real)

    # b)
    F = dft(f)
    reconstructed = np.fft.ifft2(F)
    imsave("output/02_2_manual_np.png", reconstructed.real)

    # c)
    F = dft(f)
    reconstructed = idft(F)
    imsave("output/02_3_manual_manual.png", reconstructed.real)

    ####
    #### Exercise 3
    ####
    f = imread(filename)

    # a)
    F = np.fft.fft2(f)
    F[0, 0] = 0
    reconstructed = np.clip(np.fft.ifft2(F).real, 0, 255).astype(np.uint8)
    imsave("output/03_1_reconstructed_dc_zeroed.png", reconstructed.real)

    # b)
    imsave("output/03_2_mean_subtracted.png", np.clip(f - f.mean(), 0, 255).astype(np.uint8))

    ####
    #### Exercise 4
    ####

    # a)
    F = np.fft.fft2(f)
    spectrum = np.abs(F)
    spectrum = np.log(spectrum + 1)
    imsave("output/04_1_spectrum.png", spectrum)

    shifted_spectrum = np.fft.fftshift(F)
    shifted_spectrum = np.abs(shifted_spectrum)
    shifted_spectrum = np.log(shifted_spectrum + 1)
    imsave("output/04_2_shifted_spectrum.png", shifted_spectrum)

    shifted_spectrum = np.fft.fftshift(F)
    reconstructed = np.fft.ifft2(shifted_spectrum).real
    reconstructed = np.clip(reconstructed, 0, 255).astype(np.uint8)
    imsave("output/04_3_reconstructed.png", reconstructed)

    # b)
    g = np.empty(f.shape)

    for x in range(f.shape[0]):
        for y in range(f.shape[1]):
            g[x, y] = (-1) ** (x + y) * f[x, y]

    g = np.clip(g, 0, 255).astype(np.uint8)
    imsave("output/04_4_g.png", g)

    # c)
    shifted_space = np.fft.fftshift(f)
    imsave("output/04_4_shifted_space.png", shifted_space)

    spectrum = np.fft.fft2(shifted_space)
    spectrum = np.abs(spectrum)
    spectrum = np.log(spectrum + 1)
    imsave("output/04_5_shifted_space_spectrum.png", spectrum)

    ####
    #### Exercise 4
    ####

    # a)
    F = np.fft.fft2(f)
    F = np.abs(F)
    reconstructed = np.fft.ifft2(F)
    reconstructed = np.clip(reconstructed, 0, 255).astype(np.uint8)
    imsave("output/05_1_reconstructed.png", reconstructed)

    # b)
    F = np.fft.fft2(f)
    F = np.exp(1j * np.angle(F))
    reconstructed = np.fft.ifft2(F).real
    reconstructed = (reconstructed - reconstructed.min()) / (reconstructed.max() - reconstructed.min())
    imsave("output/05_2_reconstructed.png", reconstructed)

if __name__ == "__main__":
    main()