"""
Team 46
Haoyue Xie 1003068 @Melbourne
Jiayu Li 713551 @Melbourne
Ruqi Li 1008342 @Melbourne
Yi Zhang 1032768 @Melbourne
Zimeng Jia 978322 @Hebei, China
"""

import json

path  = "E:/Unimelb/2020semester1/COPM90024 Cluster and Cloud Computing/assignment2/code/"
filename = path + 'SA4_2016_AUST.json'

with open(filename, 'r') as f:
    jsonfile = json.load(f)    #jsonfile is a dict
    print("---------------reading json file done--------------------------------------")

print(jsonfile.keys())  #dict_keys(['type', 'features'])
features = jsonfile['features']

'''
print("type of features:",type(features))  #features is a list
print(len(features))
print("type of features[0]:",type(features[0]))  #each feature is a dict
feature = features[0]
print("keys of feature:",feature.keys())  #dict_keys(['type', 'geometry', 'properties'])
properities = feature['properties']
geometry = feature['geometry']
print("type of properities:",type(properities))
print("keys of properities:",properities.keys())  #dict_keys(['SA4_CODE', 'SA4_CODE16', 'SA4_NAME', 'STATE_CODE', 'STATE_NAME', 'AREA_SQKM'])
print("type of geometry:",type(geometry)) #<class 'dict'>
print("keys of geometry:",geometry.keys()) #dict_keys(['type', 'coordinates'])
coordinates = geometry['coordinates']
print("type of coordinates",type(coordinates)) #<class 'list'>
print(len(coordinates)) # len=1
'''

coordinates_Melbourne = []
coordinates_Sydney = []
coordinates_Brisbane = []
coordinates_GoldCoast = []
coordinates_Adelaide = []
coordinates_Perth = []
coordinates_Canberra = []
coordinates_ACT = []
coordinates_NSW = []
coordinates_NT = []
coordinates_QLD = []
coordinates_SA = []
coordinates_TAS = []
coordinates_VIC = []
coordinates_WA = []

area_sqkm_Sydney = 0
area_sqkm_Melbourne = 0
area_sqkm_Brisbane = 0
area_sqkm_GoldCoast = 0
area_sqkm_Adelaide = 0
area_sqkm_Perth = 0
area_sqkm_Canberra = 0
area_sqkm_NT = 0
area_sqkm_WA = 0
area_sqkm_TAS = 0
area_sqkm_SA = 0
area_sqkm_QLD = 0
area_sqkm_VIC = 0
area_sqkm_NSW = 0
area_sqkm_ACT = 0

for feature in features:
    properities = feature['properties']
    sa4_code16 = properities['SA4_CODE16']
    print("+++++++++++++++"+sa4_code16+"++++++++++++")
    geometry = feature['geometry']  #geometry has two keys: "tpye" and "coordinates"
    print(properities)
    if(int(sa4_code16)>=115 and int(sa4_code16)<=128): #-------------------------------merge for Sydney---------------------
        if(geometry !=None):  #some coordinates are None
            coordinates = geometry['coordinates']
            #print(properities['SA4_NAME'],geometry['type'],len(coordinates))
            if (geometry["type"]=="Polygon"):
                coordinates_Sydney.append(coordinates)
            else:
                for coordinate in coordinates:
                    coordinates_Sydney.append(coordinate)
            area_sqkm_Sydney += properities['AREA_SQKM']
    elif(int(sa4_code16)>=206 and int(sa4_code16)<=213): #-----------------------------merge for Melbourne-----------------------------------
        if(geometry !=None):  #some coordinates are None
            coordinates = geometry['coordinates']
            if (geometry["type"]=="Polygon"):
                coordinates_Melbourne.append(coordinates)
            else:
                for coordinate in coordinates:
                    coordinates_Melbourne.append(coordinate)
            area_sqkm_Melbourne += properities['AREA_SQKM']
    elif(int(sa4_code16)>=401 and int(sa4_code16)<=404): #-----------------------------merge for Adelaide-------------------------------------
        if(geometry !=None):  #some coordinates are None
            coordinates = geometry['coordinates']
            if (geometry["type"]=="Polygon"):
                coordinates_Adelaide.append(coordinates)
            else:
                for coordinate in coordinates:
                    coordinates_Adelaide.append(coordinate)
            area_sqkm_Adelaide += properities['AREA_SQKM']
    elif(int(sa4_code16)>=301 and int(sa4_code16)<=305): #-----------------------------merge for Brisbane-------------------------------------
        if(geometry !=None):  #some coordinates are None
            coordinates = geometry['coordinates']
            if (geometry["type"]=="Polygon"):
                coordinates_Brisbane.append(coordinates)
            else:
                for coordinate in coordinates:
                    coordinates_Brisbane.append(coordinate)
            area_sqkm_Brisbane += properities['AREA_SQKM']
    elif(int(sa4_code16)==801): #------------------------------------------------------merge for Canberra-------------------------------------
        if(geometry !=None):  #some coordinates are None
            geometry_Canberra = geometry  #Canberraonly have one region
            #coordinates = geometry['coordinates']
            #if (geometry["type"]=="Polygon"):
            #    coordinates_Canberra.append(coordinates)
            #else:
            #    for coordinate in coordinates:
            #        coordinates_Canberra.append(coordinate)
            area_sqkm_Canberra += properities['AREA_SQKM']
    elif(int(sa4_code16)==309): #------------------------------------------------------merge for Gold Coast-------------------------------------
        if(geometry !=None):  #some coordinates are None
            coordinates = geometry['coordinates']
            if (geometry["type"]=="Polygon"):
                coordinates_GoldCoast.append(coordinates)
            else:
                for coordinate in coordinates:
                    coordinates_GoldCoast.append(coordinate)
            area_sqkm_GoldCoast += properities['AREA_SQKM']
    elif(int(sa4_code16)>=503 and int(sa4_code16)<=507): #-----------------------------merge for Perth-------------------------------------
        if(geometry !=None):  #some coordinates are None
            coordinates = geometry['coordinates']
            if (geometry["type"]=="Polygon"):
                coordinates_Perth.append(coordinates)
            else:
                for coordinate in coordinates:
                    coordinates_Perth.append(coordinate)
            area_sqkm_Perth += properities['AREA_SQKM']

    else:  #other regions for each state
        if(geometry !=None):
            if(properities['STATE_CODE']=="8"): #---------------------------------------merge for ACT
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_ACT.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_ACT.append(coordinate)
                area_sqkm_ACT += properities['AREA_SQKM']
            elif(properities['STATE_CODE']=="1"): #---------------------------------------merge for NSW
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_NSW.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_NSW.append(coordinate)
                area_sqkm_NSW += properities['AREA_SQKM']
            elif(properities['STATE_CODE']=="7"): #---------------------------------------merge for NT
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_NT.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_NT.append(coordinate)
                area_sqkm_NT += properities['AREA_SQKM']
            elif(properities['STATE_CODE']=="3"): #---------------------------------------merge for QLD
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_QLD.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_QLD.append(coordinate)
                area_sqkm_QLD += properities['AREA_SQKM']
            elif(properities['STATE_CODE']=="4"): #---------------------------------------merge for SA
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_SA.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_SA.append(coordinate)
                area_sqkm_SA += properities['AREA_SQKM']
            elif(properities['STATE_CODE']=="6"): #---------------------------------------merge for TAS
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_TAS.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_TAS.append(coordinate)
                area_sqkm_TAS += properities['AREA_SQKM']
            elif(properities['STATE_CODE']=="2"): #---------------------------------------merge for VIC
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_VIC.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_VIC.append(coordinate)
                area_sqkm_VIC += properities['AREA_SQKM']
            elif(properities['STATE_CODE']=="5"): #---------------------------------------merge for WA
                coordinates = geometry['coordinates']
                if (geometry["type"]=="Polygon"):
                    coordinates_WA.append(coordinates)
                else:
                    for coordinate in coordinates:
                        coordinates_WA.append(coordinate)
                area_sqkm_WA += properities['AREA_SQKM']

    #create a new ragion code

#===============================properties,geometry, feature of each region=========================
#----------------------Melbourne--------------------------
properties_Melbbourne = {}
properties_Melbbourne["REGION_CODE"]="01"
properties_Melbbourne["AREA_SQKM"] = area_sqkm_Melbourne
properties_Melbbourne["STATE_CODE"]="2"
properties_Melbbourne["STATE_NAME"]="VIC"
properties_Melbbourne["CITY_NAME"]="Melbourne"

geometry_Melbourne = {}
geometry_Melbourne["type"] = "MultiPolygon"
geometry_Melbourne["coordinates"] = coordinates_Melbourne

feature_Melbbourne = {}
feature_Melbbourne["type"]="Feature"
feature_Melbbourne["geometry"] = geometry_Melbourne
feature_Melbbourne["properties"] = properties_Melbbourne

#----------------------Sydney-------------------------------
properties_Sydney = {}
properties_Sydney["REGION_CODE"]="02"
properties_Sydney["AREA_SQKM"] = area_sqkm_Sydney
properties_Sydney["STATE_CODE"]="1"
properties_Sydney["STATE_NAME"]="NSW"
properties_Sydney["CITY_NAME"]="Sydney"

geometry_Sydney = {}
geometry_Sydney["type"] = "MultiPolygon"
geometry_Sydney["coordinates"] = coordinates_Sydney

feature_Sydney = {}
feature_Sydney["type"]="Feature"
feature_Sydney["geometry"] = geometry_Sydney
feature_Sydney["properties"] = properties_Sydney

#----------------------Brisbane-------------------------------
properties_Brisbane = {}
#properties_Brisbane["type"]="Feature"
properties_Brisbane["REGION_CODE"]="03"
properties_Brisbane["AREA_SQKM"] = area_sqkm_Brisbane
properties_Brisbane["STATE_CODE"]="3"
properties_Brisbane["STATE_NAME"]="QLD"
properties_Brisbane["CITY_NAME"]="Brisbane"

geometry_Brisbane = {}
geometry_Brisbane["type"] = "MultiPolygon"
geometry_Brisbane["coordinates"] = coordinates_Brisbane

feature_Brisbane = {}
feature_Brisbane["type"]="Feature"
feature_Brisbane["geometry"] = geometry_Brisbane
feature_Brisbane["properties"] = properties_Brisbane

#----------------------GoldCoast-------------------------------
properties_GoldCoast = {}
#properties_GoldCoast["type"]="Feature"
properties_GoldCoast["REGION_CODE"]="04"
properties_GoldCoast["AREA_SQKM"] = area_sqkm_GoldCoast
properties_GoldCoast["STATE_CODE"]="3"
properties_GoldCoast["STATE_NAME"]="QLD"
properties_GoldCoast["CITY_NAME"]="Gold Coast"

geometry_GoldCoast = {}
geometry_GoldCoast["type"] = "MultiPolygon"
geometry_GoldCoast["coordinates"] = coordinates_GoldCoast

feature_GoldCoast = {}
feature_GoldCoast["type"]="Feature"
feature_GoldCoast["geometry"] = geometry_GoldCoast
feature_GoldCoast["properties"] = properties_GoldCoast

#----------------------Adelaide-------------------------------
properties_Adelaide = {}
#properties_Adelaide["type"]="Feature"
properties_Adelaide["REGION_CODE"]="05"
properties_Adelaide["AREA_SQKM"] = area_sqkm_Adelaide
properties_Adelaide["STATE_CODE"]="4"
properties_Adelaide["STATE_NAME"]="SA"
properties_Adelaide["CITY_NAME"]="Adelaide"

geometry_Adelaide = {}
geometry_Adelaide["type"] = "MultiPolygon"
geometry_Adelaide["coordinates"] = coordinates_Adelaide

feature_Adelaide = {}
feature_Adelaide["type"]="Feature"
feature_Adelaide["geometry"] = geometry_Adelaide
feature_Adelaide["properties"] = properties_Adelaide

#----------------------Perth-------------------------------
properties_Perth = {}
#properties_Perth["type"]="Feature"
properties_Perth["REGION_CODE"]="06"
properties_Perth["AREA_SQKM"] = area_sqkm_Perth
properties_Perth["STATE_CODE"]="5"
properties_Perth["STATE_NAME"]="WA"
properties_Perth["CITY_NAME"]="Perth"

geometry_Perth = {}
geometry_Perth["type"] = "MultiPolygon"
geometry_Perth["coordinates"] = coordinates_Perth

feature_Perth = {}
feature_Perth["type"]="Feature"
feature_Perth["geometry"] = geometry_Perth
feature_Perth["properties"] = properties_Perth

#----------------------Canberra-------------------------------
properties_Canberra = {}
#properties_Canberra["type"]="Feature"
properties_Canberra["REGION_CODE"]="07"
properties_Canberra["AREA_SQKM"] = area_sqkm_Canberra
properties_Canberra["STATE_CODE"]="8"
properties_Canberra["CITY_NAME"]="Canberra"

#geometry_Canberra = {}
#geometry_Canberra["type"] = "Polygon"
#geometry_Canberra["coordinates"] = coordinates_Canberra

feature_Canberra = {}
feature_Canberra["type"]="Feature"
feature_Canberra["geometry"] = geometry_Canberra
feature_Canberra["properties"] = properties_Canberra

#----------------------ACT-------------------------------
properties_ACT = {}
#properties_ACT["type"]="Feature"
properties_ACT["REGION_CODE"]="08"
properties_ACT["AREA_SQKM"] = area_sqkm_ACT
properties_ACT["STATE_CODE"]="8"
properties_ACT["STATE_NAME"]="ACT"
properties_ACT["CITY_NAME"]="Australian Capital Territory Other Regions"

geometry_ACT = {}
#geometry_ACT["type"] = "Polygon"
#geometry_ACT["coordinates"] = coordinates_ACT

feature_ACT = {}
feature_ACT["type"]="Feature"
feature_ACT["geometry"] = geometry_ACT
feature_ACT["properties"] = properties_ACT

#----------------------NSW-------------------------------
properties_NSW = {}
#properties_NSW["type"]="Feature"
properties_NSW["REGION_CODE"]="09"
properties_NSW["AREA_SQKM"] = area_sqkm_NSW
properties_NSW["STATE_CODE"]="1"
properties_NSW["STATE_NAME"]="NSW"
properties_NSW["CITY_NAME"]="New South Wales Other Regions"

geometry_NSW = {}
geometry_NSW["type"] = "MultiPolygon"
geometry_NSW["coordinates"] = coordinates_NSW

feature_NSW = {}
feature_NSW["type"]="Feature"
feature_NSW["geometry"] = geometry_NSW
feature_NSW["properties"] = properties_NSW

#----------------------VIC-------------------------------
properties_VIC = {}
#properties_VIC["type"]="Feature"
properties_VIC["REGION_CODE"]="10"
properties_VIC["AREA_SQKM"] = area_sqkm_VIC
properties_VIC["STATE_CODE"]="2"
properties_VIC["STATE_NAME"]="VIC"
properties_VIC["CITY_NAME"]="Victoria Other Regions"

geometry_VIC = {}
geometry_VIC["type"] = "MultiPolygon"
geometry_VIC["coordinates"] = coordinates_VIC

feature_VIC = {}
feature_VIC["type"]="Feature"
feature_VIC["geometry"] = geometry_VIC
feature_VIC["properties"] = properties_VIC

#----------------------QLD-------------------------------
properties_QLD = {}
#properties_QLD["type"]="Feature"
properties_QLD["REGION_CODE"]="11"
properties_QLD["AREA_SQKM"] = area_sqkm_QLD
properties_QLD["STATE_CODE"]="3"
properties_QLD["STATE_NAME"]="QLD"
properties_QLD["CITY_NAME"]="Queensland Other Regions"

geometry_QLD = {}
geometry_QLD["type"] = "MultiPolygon"
geometry_QLD["coordinates"] = coordinates_QLD

feature_QLD = {}
feature_QLD["type"]="Feature"
feature_QLD["geometry"] = geometry_QLD
feature_QLD["properties"] = properties_QLD

#----------------------SA-------------------------------
properties_SA = {}
#properties_SA["type"]="Feature"
properties_SA["REGION_CODE"]="12"
properties_SA["AREA_SQKM"] = area_sqkm_SA
properties_SA["STATE_CODE"]="4"
properties_SA["STATE_NAME"]="SA"
properties_SA["CITY_NAME"]="South Australia Other Regions"

geometry_SA = {}
geometry_SA["type"] = "MultiPolygon"
geometry_SA["coordinates"] = coordinates_SA

feature_SA = {}
feature_SA["type"]="Feature"
feature_SA["geometry"] = geometry_SA
feature_SA["properties"] = properties_SA

#----------------------TAS-------------------------------
properties_TAS = {}
#properties_TAS["type"]="Feature"
properties_TAS["REGION_CODE"]="13"
properties_TAS["AREA_SQKM"] = area_sqkm_TAS
properties_TAS["STATE_CODE"]="6"
properties_TAS["STATE_NAME"]="TAS"
properties_TAS["CITY_NAME"]="Tasmania Other Regions"

geometry_TAS = {}
geometry_TAS["type"] = "MultiPolygon"
geometry_TAS["coordinates"] = coordinates_TAS

feature_TAS = {}
feature_TAS["type"]="Feature"
feature_TAS["geometry"] = geometry_TAS
feature_TAS["properties"] = properties_TAS

#----------------------WA-------------------------------
properties_WA = {}
#properties_WA["type"]="Feature"
properties_WA["REGION_CODE"]="14"
properties_WA["AREA_SQKM"] = area_sqkm_WA
properties_WA["STATE_CODE"]="5"
properties_WA["STATE_NAME"]="WA"
properties_WA["CITY_NAME"]="Western Australia Other Regions"

geometry_WA = {}
geometry_WA["type"] = "MultiPolygon"
geometry_WA["coordinates"] = coordinates_WA

feature_WA = {}
feature_WA["type"]="Feature"
feature_WA["geometry"] = geometry_WA
feature_WA["properties"] = properties_WA

#----------------------NT-------------------------------
properties_NT = {}
#properties_NT["type"]="Feature"
properties_NT["REGION_CODE"]="15"
properties_NT["AREA_SQKM"] = area_sqkm_NT
properties_NT["STATE_CODE"]="7"
properties_NT["STATE_NAME"]="NT"
properties_NT["CITY_NAME"]="Northern Territory Other Regions"

geometry_NT = {}
geometry_NT["type"] = "MultiPolygon"
geometry_NT["coordinates"] = coordinates_NT

feature_NT = {}
feature_NT["type"]="Feature"
feature_NT["geometry"] = geometry_NT
feature_NT["properties"] = properties_NT

#=============================Add feature into features and output====================================
#new_features = [feature_Adelaide,feature_Brisbane,feature_Canberra,feature_GoldCoast,feature_Melbbourne,feature_Perth,feature_Sydney,\
#feature_NSW,feature_NT,feature_QLD,feature_SA,feature_TAS,feature_VIC,feature_WA]

#feature_ACT is empty
new_features = [feature_WA,feature_Perth,feature_Adelaide,feature_Brisbane,feature_Canberra,feature_GoldCoast,feature_Melbbourne,feature_Sydney,\
    feature_NSW,feature_NT,feature_QLD,feature_SA,feature_TAS,feature_VIC,feature_WA]

#print("************************")
#print(feature_ACT["properties"])
#print(feature_ACT["geometry"])

newjsonfile = {}
newjsonfile["type"] = "FeatureCollection"
newjsonfile["features"] = new_features

#print(newjsonfile.keys())
#print(newjsonfile["features"][0].keys())
#print(newjsonfile["features"][0]['properties'])
#print(newjsonfile["features"][0]['geometry'])
#print("newjsonfile done!")

#json_str = json.dumps(newjsonfile)
#print("create json done!")

outputfilename = path + "City_geojson.json"

with open(outputfilename, 'w') as json_file:
    #json_file.write(json_str)
    for chunk in json.JSONEncoder().iterencode(newjsonfile):
        json_file.write(chunk)

print("All Done!")

Create City_GeoJSON file/coordinates for cities.py


from pyspark.sql import SparkSession
from pyspark.sql import functions as fs

spark= SparkSession.builder.appName("word_count").getOrCreate()

data= spark.read.text("book.txt")

pro_data= data.select(fs.explode(fs.split(data.value,"\\W+")).alias("words"))
pro_data.filter(pro_data.words !="")

a=pro_data.select("words").groupBy("words").count().show()


# -*- coding: utf-8 -*-
"""
Created on Sat May  8 12:16:46 2021

@author: tamon
"""

import csv
import numpy as np
from scipy.interpolate import griddata
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

radius = []
angles = []
points = []
result = []

with open('angrad.csv', newline='') as csvfile:
    readfile = csv.reader(csvfile, quotechar='|')
    for row in readfile:
        radius.append(row[12])
        angles.append(row[13])
        result.append(row[20])

radius.pop(0)
angles.pop(0)
result.pop(0)
radius = [int(i) for i in radius]
angles = [int(i) for i in angles]
for i in range(len(radius)):
    points.append([angles[i], radius[i]])
result = [np.float64(i) for i in result]

xgrid, ygrid = np.mgrid[10:90:1000j, 30:240:1000j]
grid = griddata(points, result, (xgrid, ygrid), method='cubic')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(angles, radius, 'k.', ms=1)
sp = ax.imshow(grid.T, cmap='jet', extent=(10, 90, 30, 240), origin='lower')
ax.set_aspect(80/(210))
ax.set_xlabel('Angle [deg]')
ax.set_ylabel('Radius [mm]')
clb = fig.colorbar(sp)
clb.set_label('Equivelant Maximum Stress [Pa]')
fig.savefig('angrad.pdf', format='pdf', bbox_inches='tight')
plt.show()

angslice1 = []
angslice2 = []
angslice3 = []
angs = np.linspace(10, 90, 9)
j = 1
for j in range(9):
    angslice1.append(result[8*j + 0])
    angslice2.append(result[8*j + 1])
    angslice3.append(result[8*j + 2])

xnew = np.linspace(10, 90, 200)
f1 = interp1d(angs, angslice1, kind='cubic')
f2 = interp1d(angs, angslice2, kind='cubic')
f3 = interp1d(angs, angslice3, kind='cubic')
plt.plot(xnew, f1(xnew), 'r', label='Radius=30 [mm]')
plt.plot(xnew, f2(xnew), 'b', label='Radius=60 [mm]')
plt.plot(xnew, f3(xnew), 'g', label='Radius=90 [mm]')
plt.grid('major')
plt.legend(loc='lower right')
plt.xlabel('Angle [deg]')
plt.ylabel('Equivelant Maximum Stress [Pa]')
plt.savefig('angslice.pdf', format='pdf', bbox_inches='tight')

# angslice1 = []
# angslice2 = []
# angslice3 = []
# angs = np.linspace(10, 90, 9)
# j = 1
# for j in range(9):
#     angslice1.append(result[8*j + 0])
#     angslice2.append(result[8*j + 1])
#     angslice3.append(result[8*j + 2])

# xnew = np.linspace(10, 90, 200)
# f1 = interp1d(angs, angslice1, kind='cubic')
# f2 = interp1d(angs, angslice2, kind='cubic')
# f3 = interp1d(angs, angslice3, kind='cubic')
# plt.plot(xnew, np.gradient(f1(xnew)), 'r', label='Radius=30 [mm]')
# plt.plot(xnew, np.gradient(f2(xnew)), 'b', label='Radius=60 [mm]')
# plt.plot(xnew, np.gradient(f3(xnew)), 'g', label='Radius=90 [mm]')
# plt.grid('major')
# plt.legend(loc='lower right')
# plt.xlabel('Angle [deg]')
# plt.ylabel('Equivelant Maximum Stress [Pa]')
# plt.savefig('angslice.pdf', format='pdf', bbox_inches='tight')

radslice1 = result[:8]
radslice2 = result[8:16]
radslice3 = result[16:24]
radslice4 = result[24:32]
radslice5 = result[32:40]
radslice6 = result[40:48]
radslice7 = result[48:56]
rads = np.linspace(30, 240, 8)

xnew = np.linspace(30, 240, 200)
f1 = interp1d(rads, radslice1, kind='cubic')
f2 = interp1d(rads, radslice2, kind='cubic')
f3 = interp1d(rads, radslice3, kind='cubic')
f4 = interp1d(rads, radslice4, kind='cubic')
f5 = interp1d(rads, radslice5, kind='cubic')
f6 = interp1d(rads, radslice6, kind='cubic')
f7 = interp1d(rads, radslice7, kind='cubic')

fig2 = plt.figure()
ax2 = plt.subplot(111)
ax2.plot(xnew, f1(xnew), 'r', label='Radius=10 [mm]')
ax2.plot(xnew, f2(xnew), 'b', label='Radius=20 [mm]')
ax2.plot(xnew, f3(xnew), 'g', label='Radius=30 [mm]')
ax2.plot(xnew, f4(xnew), 'y', label='Radius=40 [mm]')
ax2.plot(xnew, f5(xnew), 'orange', label='Radius=50 [mm]')
ax2.plot(xnew, f6(xnew), 'cyan', label='Radius=60 [mm]')
ax2.plot(xnew, f7(xnew), 'purple', label='Radius=70 [mm]')
ax2.grid('major')
chartBox = ax2.get_position()
ax2.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.6, chartBox.height])
ax2.legend(loc='upper center', bbox_to_anchor=(1.4, 0.8), shadow=True, ncol=1)
ax2.set_xlabel('Radius [mm]')
ax2.set_ylabel('Equivelant Maximum Stress [Pa]')
fig2.savefig('radslice.pdf', format='pdf', bbox_inches='tight')


import pygame
from constantes import *
from auxiliar import Auxiliar

class Background:
    '''
    Clase para representar un fondo en un juego utilizando Pygame.

    Attributes:
        x (int): La coordenada x de la esquina superior izquierda del fondo.
        y (int): La coordenada y de la esquina superior izquierda del fondo.
        width (int): Ancho del fondo.
        height (int): Alto del fondo.
        path (str): Ruta del archivo de imagen para el fondo.
    '''
    def __init__(self, x, y,width, height,  path):
        '''
        Constructor de la clase. Inicializa las propiedades del fondo.
        '''
        self.image = pygame.image.load(path).convert()
        self.image = pygame.transform.scale(self.image,(width,height))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def draw(self,screen):
        '''
        Dibuja el fondo en la pantalla especificada.
        '''
        screen.blit(self.image,self.rect)
        if(DEBUG):
            pygame.draw.rect(screen,color=ROJO,rect=self.collition_rect)


#!/usr/bin/env python

from __future__ import print_function

import os, sys, json

import numpy as np

from ase.build import bulk, surface
from ase.units import Rydberg, Bohr
from ase.io import read
from ase.visualize import view
from ase.spacegroup import crystal
from ase.calculators.espresso import Espresso

infile = sys.argv[1]
print(infile)
with open(infile) as handle:
    system = json.loads(handle.read())

face = system['face']
layers = system['layers']
vacuum = system['vacuum']
kpts = system['kpts']
ecut = system['ecut']
mode = system['mode']

cwd = os.getcwd()

def cassiterite(show=False):
    a = 4.7382
    c = 3.1871
    sno2 = crystal(['Sn','O'], basis=[(0, 0, 0), (0.3, 0.3, 0.0)],
                   spacegroup='P 4 2/mnm', cellpar=[a,a,c,90,90,90],
                   pbc=True)
    return sno2

def create_surface(atoms,face=(1,1,0),layers=3,vacuum=10.813,kpts=([6,3,1])):
    mySurface = surface( atoms, face, layers)
    mySurface.center(vacuum=vacuum, axis=2)
    kpts = np.asarray(kpts)
    return mySurface

sno2         = cassiterite()
sno2_surface = create_surface(sno2,
                              face=face,
                              layers=layers,
                              vacuum=vacuum,
                              kpts=kpts)

# Put together QE input dict
input_dict = {
    'control': {
        'calculation': 'scf',
        'etot_conv_thr': 1e-6,
        'nstep': 100,
        'outdir': 'sno2_test_face_{0}{1}{2}'.format(face[0], face[1], face[2]),
    },
    'system': {
        'ecutwfc': ecut,
    },
    'electrons': {
        'diagonalization': 'cg',
    },
}

# Put together pseudopotential dict
psp_dict = {'Sn': 'Sn.UPF',
            'O': 'O.UPF',
            }

calc = Espresso(input_data=input_dict,
                kpts=kpts,
                pseudo_dir=cwd + "/../pseudo",
                pseudopotentials=psp_dict,
                )

sno2_surface.set_calculator(calc)

if mode == 'view':
    view(sno2_surface)
elif mode == 'calc':
    calc.calculate(sno2_surface)
    print('SnO2 PE:', sno2_surface.get_potential_energy())


import pytest
from PyQt6.QtTest import QTest
from PyQt6.QtWidgets import QLineEdit
from pytestqt import qtbot

from main import OLXWork, OLXSettings
from PyQt6 import QtCore

def test_olxwork_button_stop_clicked(qtbot):
    parent = OLXSettings()
    widget = OLXWork(parent= parent)
    widget.show()
    qtbot.addWidget(widget)
    assert widget.isVisible()

    widget.start_timer()
    qtbot.mouseClick(widget.button_stop, QtCore.Qt.MouseButton.LeftButton)

    assert not widget.isVisible()
    assert parent.isVisible()

def test_olxwork_animation_label(qtbot):
    widget = OLXWork()
    assert widget.in_progress_label.text() == "Program w trakcie pracy"
    widget.animation_label_counter = 0
    widget.animation_label()
    assert widget.in_progress_label.text() == "Program w trakcie pracy."
    for _ in range(3):
        widget.animation_label()
    assert widget.in_progress_label.text() == "Program w trakcie pracy"

def test_olxwork_start_timer(qtbot):
    widget = OLXWork()
    qtbot.addWidget(widget)
    widget.start_timer()
    assert widget.animation_label_timer.isActive()
    assert widget.room_olx_timer.isActive()

def test_olxwork_stop_timer(qtbot):
    widget = OLXWork()
    qtbot.addWidget(widget)
    widget.start_timer()
    widget.stop_timer()
    assert not widget.animation_label_timer.isActive()
    assert not widget.room_olx_timer.isActive()

def test_olxwork_update_data_olxwork(qtbot):
    widget = OLXWork()
    qtbot.addWidget(widget)
    widget.update_data_olxwork("http://something.pl","City")
    assert widget.city == "City"
    assert widget.url == "http://something.pl"

def test_olxsettings_button_start_clicked(qtbot):
    widget = OLXSettings()
    son = OLXWork(parent=widget)
    widget.olx_work = son
    qtbot.keyClicks(widget.city, "Zakopane")
    widget.show()
    assert widget.isVisible()
    qtbot.addWidget(widget)
    qtbot.mouseClick(widget.button_start,QtCore.Qt.MouseButton.LeftButton)
    assert son.isVisible()
    assert not widget.isVisible()

def test_olxsettings_check_city(qtbot):
    widget = OLXSettings()
    widget.city = QLineEdit()
    widget.city.setText("Warszawa")
    assert widget.check_city() == 0

def test_olxsettings_check_city_empty(qtbot):
    widget = OLXSettings()
    widget.city = QLineEdit()
    widget.city.setText("")
    assert widget.check_city() == 1

def test_olxsettings_check_city_invalid(qtbot):
    widget = OLXSettings()
    widget.city = QLineEdit()
    widget.city.setText("InvalidCity")
    assert widget.check_city() == 1

def test_olxsettings_main_window_type_index_changed_visible(qtbot):
    widget = OLXSettings()
    qtbot.addWidget(widget)
    widget.show()
    assert widget.type.currentIndex() == 0
    assert not widget.rooms.isVisible()
    assert not widget.rooms_label.isVisible()
    assert widget.m_2_from.isVisible()
    assert widget.m_2_to.isVisible()

    qtbot.waitUntil(lambda: widget.type.count() > 0)
    qtbot.mouseClick(widget.type, QtCore.Qt.MouseButton.LeftButton)
    qtbot.keyClick(widget.type, QtCore.Qt.Key.Key_Down)
    qtbot.keyClick(widget.type, QtCore.Qt.Key.Key_Return)
    assert widget.rooms.isVisible()
    assert widget.rooms_label.isVisible()
    assert widget.m_2_from.isVisible()
    assert widget.m_2_to.isVisible()

    qtbot.waitUntil(lambda: widget.type.count() > 0)
    qtbot.mouseClick(widget.type, QtCore.Qt.MouseButton.LeftButton)
    qtbot.keyClick(widget.type, QtCore.Qt.Key.Key_Down)
    qtbot.keyClick(widget.type, QtCore.Qt.Key.Key_Return)
    assert not widget.rooms.isVisible()
    assert not widget.rooms_label.isVisible()
    assert not widget.m_2_from.isVisible()
    assert not widget.m_2_to.isVisible()


import torch
from .logger_utils import get_logger
import matplotlib.pyplot as plt
import numpy as np
import itertools

logger = get_logger()
def prediction(*, test_data, model, device):
    """Predict on test data and generate confusion matrix.

    Args:
        test_data (torch.utils.data.Dataset): Test dataset.
        model (torch.nn.Module): Model.
        device (str): Device (cpu or gpu or mps)
    """
    num_classes = 10
    class_names = [
        "T-shirt/top",
        "Trouser",
        "Pullover",
        "Dress",
        "Coat",
        "Sandal",
        "Shirt",
        "Sneaker",
        "Bag",
        "Ankle boot",
    ]
    confusion_matrix = torch.zeros(num_classes, num_classes)

    with torch.no_grad():
        for X, y in test_data:
            X = X.unsqueeze(0).to(device)
            y = torch.tensor([y]).to(device)

            outputs = model(X)
            _, predicted = torch.max(outputs.data, 1)

            for t, p in zip(y.view(-1), predicted.view(-1)):
                confusion_matrix[t.long(), p.long()] += 1

    plt.figure(figsize=(10,10))
    plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)
    plt.title('Confusion matrix')
    plt.colorbar()
    tick_marks = np.arange(len(class_names))
    plt.xticks(tick_marks, class_names, rotation=45)
    plt.yticks(tick_marks, class_names)

    thresh = confusion_matrix.max() / 2.
    for i, j in itertools.product(range(confusion_matrix.shape[0]), range(confusion_matrix.shape[1])):
        plt.text(j, i, format(confusion_matrix[i, j], '.1f'),
                 horizontalalignment="center",
                 color="white" if confusion_matrix[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.savefig('confusion_matrix.png')
    logger.info('Confusion Matrix saved as confusion_matrix.png')


import csv
import os
from flask import Blueprint, redirect, render_template, request
import sqlalchemy
from forms.addTeacherForm import AddTeacherForm
from models import GroupOfTeacher, Teacher, TeacherInGroup, db
from forms.editForm import EditTeacherForm

ROWS_PER_PAGE = 5

teachers_blueprint = Blueprint("teachers_blueprint", __name__)

@teachers_blueprint.route("/teachers", methods=["get", "post"])
def teachers():
    page = request.args.get('page', 1, type=int)

    teachers = Teacher.query.order_by(Teacher.last_name).paginate(page=page, per_page=ROWS_PER_PAGE)
    addTeacherFormData = AddTeacherForm()

    return render_template("teachers/teachers.html",
                           teachers=teachers,
                           form=addTeacherFormData)

add_teachers_blueprint = Blueprint("add_teachers_blueprint", __name__)

@add_teachers_blueprint.route("/teachers/add", methods=["get", "post"])
def add_teachers():
    addTeacherFormData = AddTeacherForm()
    # teachers = db.session.query(Teacher).all()
    if True:  # request.method == "POST":
        if addTeacherFormData.validate_on_submit():
            teacherData = Teacher()
            teacherData.first_name = addTeacherFormData.first_name.data
            teacherData.last_name = addTeacherFormData.last_name.data
            teacherData.birthdate = addTeacherFormData.birthdate.data
            # teacherData.teacher_in_group_Id = addTeacherFormData.teacher_in_group_Id.data

            db.session.add(teacherData)
            db.session.commit()

            return redirect("/teachers")

        else:
            return render_template("teachers/addTeacherForm.html",
                                   form=addTeacherFormData)

show_edit_teachers_blueprint = Blueprint(
    "show_edit_teachers_blueprint", __name__)

@show_edit_teachers_blueprint.route("/teachers/edit")
def show_edit_teachers():
    editTeacherFormData = EditTeacherForm()
    global current_data
    current_data = editTeacherFormData
    # itemId auslesen
    teacher_Id = request.args["teacher_Id"]
    # Item laden
    teacher_to_edit = db.session.query(Teacher).filter(
        Teacher.teacher_Id == teacher_Id).first()
    # Form befüllen
    editTeacherFormData.teacher_Id.data = teacher_Id
    editTeacherFormData.first_name.data = teacher_to_edit.first_name
    editTeacherFormData.last_name.data = teacher_to_edit.last_name
    editTeacherFormData.birthdate.data = teacher_to_edit.birthdate
    # editTeacherFormData.teacher_in_group_Id.data = teacher_to_edit.teacher_in_group_Id

    group_of_teachers = db.session.query(GroupOfTeacher).filter(
        sqlalchemy.and_(TeacherInGroup.group_of_teachers_Id == GroupOfTeacher.group_of_teachers_Id,
                        TeacherInGroup.teacher_Id == teacher_Id))

    return render_template("/teachers/editTeacherForm.html", form=editTeacherFormData,
                           group_of_teachers=group_of_teachers)

submit_edit_teachers_blueprint = Blueprint(
    "submit_edit_teachers_blueprint", __name__)

@submit_edit_teachers_blueprint.route("/teachers/edit", methods=["post"])
def submit_edit_teachers():
    editTeacherFormData = EditTeacherForm()
    create_report_file(editTeacherFormData)
    if editTeacherFormData.validate_on_submit():
        # daten aus Form auslesen
        teacher_Id = editTeacherFormData.teacher_Id.data
        teacher_to_edit = db.session.query(Teacher).filter(
            Teacher.teacher_Id == teacher_Id).first()
        # daten mit update in DB speichern
        teacher_to_edit.first_name = editTeacherFormData.first_name.data
        teacher_to_edit.last_name = editTeacherFormData.last_name.data
        teacher_to_edit.birthdate = editTeacherFormData.birthdate.data
        # teacher_to_edit.teacher_in_group_Id = editTeacherFormData.teacher_in_group_Id.data

        db.session.commit()

        return redirect("/teachers")

    else:
        raise("Fatal Error")

def create_report_file(teacher_data):
    header = ["Data", "Previous Data", "New Data"]
    teacher_id = ["Teacher ID", current_data.teacher_Id.data,
                  teacher_data.teacher_Id.data]
    first_name = ["First Name", current_data.first_name.data,
                  teacher_data.first_name.data]
    last_name = ["Last Name", current_data.last_name.data,
                 teacher_data.last_name.data]
    birthdate = ["Birthdate", current_data.birthdate.data,
                 teacher_data.birthdate.data]
    i = 0
    while os.path.exists("TeacherDataEdit%s.csv" % i):
        i += 1
    f = open(f"TeacherDataEdit{i}.csv", "w")

    writer = csv.writer(f)

    writer.writerow(header)
    writer.writerow(teacher_id)
    writer.writerow(first_name)
    writer.writerow(last_name)
    writer.writerow(birthdate)


from django.contrib import admin

from .models import InstrumentItem
# Register your models here.

class InstrumentItemAdmin(admin.ModelAdmin):
    search_fields = ['definition']
    list_filter = ['instrument']
    list_display = ['definition','instrument','discrimination','difficulty','guessing','upper_asymptote']
admin.site.register(InstrumentItem, InstrumentItemAdmin)


import asyncio
import base64
import collections
import json
import struct
import sys

import aiohttp
import pytest
import six
from pytest_httpserver import RequestHandler

import consul
import consul.aio

Check = consul.Check

@pytest.fixture
def local_server(httpserver):
    from pytest_httpserver import RequestHandler

    handler = httpserver.expect_request('/v1/agent/services')
    assert isinstance(handler, RequestHandler)
    handler.respond_with_data(json.dumps({"foo": "bar"}), status=599)
    port = httpserver.port
    LocalServer = collections.namedtuple('LocalServer', ['port'])
    yield LocalServer(port)
    httpserver.stop()

@pytest.fixture
async def local_timeout_server(httpserver):
    async def func():
        return json.dumps({"foo": "bar"})

    handler = httpserver.expect_request('/v1/agent/services')
    assert isinstance(handler, RequestHandler)
    handler.respond_with_data(await func(), status=200)
    LocalServer = collections.namedtuple('LocalServer', ['port', 'server'])
    return LocalServer(httpserver.port, httpserver)

@pytest.fixture
def loop(request):
    asyncio.set_event_loop(None)
    loop = asyncio.new_event_loop()

    def fin():
        loop.close()

    request.addfinalizer(fin)
    return loop

class TestAsyncioConsul(object):

    def test_kv(self, loop, consul_port):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            index, data = await c.kv.get('foo')

            assert data is None
            response = await c.kv.put('foo', 'bar')
            assert response is True
            response = await c.kv.put('foo-2', 'bar')
            assert response is True
            index, data = await c.kv.get('foo')
            assert data['Value'] == six.b('bar')

        loop.run_until_complete(main())

    def test_consul_ctor(self, loop, consul_port):
        # same as previous but with global event loop
        async def main():
            c = consul.aio.Consul(port=consul_port)
            assert c._loop is loop
            await c.kv.put('foo', struct.pack('i', 1000))
            index, data = await c.kv.get('foo')
            assert struct.unpack('i', data['Value']) == (1000,)

        asyncio.set_event_loop(loop)
        loop.run_until_complete(main())

    def test_kv_binary(self, loop, consul_port):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            await c.kv.put('foo', struct.pack('i', 1000))
            index, data = await c.kv.get('foo')
            assert struct.unpack('i', data['Value']) == (1000,)

        loop.run_until_complete(main())

    def test_kv_missing(self, loop, consul_port):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)

            fut = asyncio.ensure_future(put(), loop=loop)
            await c.kv.put('index', 'bump')
            index, data = await c.kv.get('foo')
            assert data is None
            index, data = await c.kv.get('foo', index=index)
            assert data['Value'] == six.b('bar')
            await fut

        async def put():
            c = consul.aio.Consul(port=consul_port, loop=loop)

            await asyncio.sleep(2.0 / 100, loop=loop)
            await c.kv.put('foo', 'bar')

        loop.run_until_complete(main())

    def test_kv_put_flags(self, loop, consul_port):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            await c.kv.put('foo', 'bar')
            index, data = await c.kv.get('foo')
            assert data['Flags'] == 0

            response = await c.kv.put('foo', 'bar', flags=50)
            assert response is True
            index, data = await c.kv.get('foo')
            assert data['Flags'] == 50

        loop.run_until_complete(main())

    def test_kv_delete(self, loop, consul_port):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            await c.kv.put('foo1', '1')
            await c.kv.put('foo2', '2')
            await c.kv.put('foo3', '3')
            index, data = await c.kv.get('foo', recurse=True)
            assert [x['Key'] for x in data] == ['foo1', 'foo2', 'foo3']

            response = await c.kv.delete('foo2')
            assert response is True
            index, data = await c.kv.get('foo', recurse=True)
            assert [x['Key'] for x in data] == ['foo1', 'foo3']
            response = await c.kv.delete('foo', recurse=True)
            assert response is True
            index, data = await c.kv.get('foo', recurse=True)
            assert data is None

        loop.run_until_complete(main())

    def test_kv_subscribe(self, loop, consul_port):
        async def get():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            fut = asyncio.ensure_future(put(), loop=loop)
            index, data = await c.kv.get('foo')
            assert data is None
            index, data = await c.kv.get('foo', index=index)
            assert data['Value'] == six.b('bar')
            await fut

        async def put():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            await asyncio.sleep(1.0 / 100, loop=loop)
            response = await c.kv.put('foo', 'bar')
            assert response is True

        loop.run_until_complete(get())

    def test_transaction(self, loop, consul_port):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            value = base64.b64encode(b"1").decode("utf8")
            d = {"KV": {"Verb": "set", "Key": "asdf", "Value": value}}
            r = await c.txn.put([d])
            assert r["Errors"] is None

            d = {"KV": {"Verb": "get", "Key": "asdf"}}
            r = await c.txn.put([d])
            assert r["Results"][0]["KV"]["Value"] == value

        loop.run_until_complete(main())

    def test_agent_services(self, loop, consul_port):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            services = await c.agent.services()
            assert services == {}
            response = await c.agent.service.register('foo')
            assert response is True
            services = await c.agent.services()
            assert services == {
                'foo': {'ID': 'foo',
                        'Service': 'foo',
                        'Tags': [],
                        'Meta': {},
                        'Port': 0,
                        'Address': '',
                        'Weights': {'Passing': 1, 'Warning': 1},
                        'EnableTagOverride': False}, }
            response = await c.agent.service.deregister('foo')
            assert response is True
            services = await c.agent.services()
            assert services == {}

        loop.run_until_complete(main())

    def test_catalog(self, loop, consul_port):
        async def nodes():
            c = consul.aio.Consul(port=consul_port, loop=loop)

            fut = asyncio.ensure_future(register(), loop=loop)
            index, nodes = await c.catalog.nodes()
            assert len(nodes) == 1
            current = nodes[0]

            index, nodes = await c.catalog.nodes(index=index)
            nodes.remove(current)
            assert [x['Node'] for x in nodes] == ['n1']

            index, nodes = await c.catalog.nodes(index=index)
            nodes.remove(current)
            assert [x['Node'] for x in nodes] == []
            await fut

        async def register():
            c = consul.aio.Consul(port=consul_port, loop=loop)

            await asyncio.sleep(1.0 / 100, loop=loop)
            response = await c.catalog.register('n1', '10.1.10.11')
            assert response is True
            await asyncio.sleep(50 / 1000.0, loop=loop)
            response = await c.catalog.deregister('n1')
            assert response is True

        loop.run_until_complete(nodes())

    def test_session(self, loop, consul_port):
        async def monitor():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            fut = asyncio.ensure_future(register(), loop=loop)
            index, services = await c.session.list()
            assert services == []
            await asyncio.sleep(20 / 1000.0, loop=loop)

            index, services = await c.session.list(index=index)
            assert len(services)

            index, services = await c.session.list(index=index)
            assert services == []
            await fut

        async def register():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            await asyncio.sleep(1.0 / 100, loop=loop)
            session_id = await c.session.create()
            await asyncio.sleep(50 / 1000.0, loop=loop)
            response = await c.session.destroy(session_id)
            assert response is True

        loop.run_until_complete(monitor())

    @pytest.mark.skipif(sys.version_info < (3, 4, 1),
                        reason="Python <3.4.1 doesnt support __del__ calls "
                               "from GC")
    def test_httpclient__del__method(self, loop, consul_port, recwarn):
        async def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            _, _ = await c.kv.get('foo')
            del c
            import gc
            # run gc to ensure c is collected
            gc.collect()
            w = recwarn.pop(ResourceWarning)
            assert issubclass(w.category, ResourceWarning)

        loop.run_until_complete(main())

    def test_root(self, loop, local_server):
        async def test_timeout():
            time_out = False
            c = consul.aio.Consul(port=local_server.port, loop=loop)
            try:
                await c.agent.services()
            except consul.Timeout:
                time_out = True
            assert time_out

        loop.run_until_complete(test_timeout())

    def test_http_session(self, loop, local_timeout_server, consul_port):
        async def test_session_close():
            http_server = await local_timeout_server
            c = consul.aio.Consul(port=http_server.port, loop=loop)
            c.agent.services()

            c.http._session = aiohttp.ClientSession()

            assert not c.http._session.closed
            c.http.__del__()
            await c.http.close()
            assert c.http._session.closed
            http_server.server.stop()
            ...

        loop.run_until_complete(test_session_close())


from collections import defaultdict
import os

import pandas as pd

path = '/home/djf/djf/POI/CDRF/data/Foursquare_NYC.txt'

dic = defaultdict(int)

f = open(path, 'r')
lines = f.readlines()

for line in lines:
    user, t, lat, lon, POI = line.strip().split('\t')
    dic[int(POI)] += 1

counts = [item[1] for item in dic.items()]

counts = sorted(counts)

with open('counts.txt', 'w') as f:
    for item in counts[:-10]:
        f.write('{}\n'.format(item))


import random
import os
import argparse
from typing import DefaultDict

# from paper "Learning Unknown from Correlations:
# Graph Neural Network for Inter-novel-protein Interaction Prediction"
standard_acids = [
        ('A', 1), ('C', 6), ('D', 5), ('E', 7), ('F', 2),
        ('G', 1), ('H', 4), ('I', 2), ('K', 5), ('L', 2),
        ('M', 3), ('N', 4), ('P', 2), ('Q', 7), ('R', 4),
        ('S', 3), ('T', 3), ('V', 1), ('W', 4), ('Y', 3)]

class PPI:
    def __init__(self, fst_pro, sec_pro, label=1):
        self.fst_pro = fst_pro
        self.sec_pro = sec_pro
        self.label = label
        assert label == 1 or label == 0

    def __eq__(self, __o: object) -> bool:
        if (self.fst_pro == __o.fst_pro and self.sec_pro == __o.sec_pro) or \
            (self.fst_pro == __o.sec_pro and self.sec_pro == __o.fst_pro):
            return True
        else:
            return False

    def __str__(self) -> str:
        return f"{self.fst_pro}\t{self.sec_pro}\t{str(self.label)}\n"

def handle(pairfp, fastafp):
    with open(pairfp, "r") as f:
        ppi_lines = f.readlines()
    with open(fastafp, "r") as f:
        seq_lines = f.readlines()

    # 存储正样本邻居，tsv文件中负样本总在最后
    pneighbors = DefaultDict(set)

    ppis = []
    proteins = set()
    for ppiline in ppi_lines:
        fpro, spro, label = ppiline.split()
        proteins.update((fpro, spro))
        label = int(float(label))
        if label == 0:
            if spro in pneighbors[fpro] or fpro in pneighbors[spro]:
                # 标签冲突，作为正样本
                continue
        elif label == 1:
            pneighbors[fpro].add(spro)
            pneighbors[spro].add(fpro)
        else:
            raise ValueError
        ppis.append(PPI(fpro, spro, label))

    acid_seqs = {}
    for idx in range(0, len(seq_lines), 2):
        key = seq_lines[idx].strip()[1:].strip()
        value = seq_lines[idx+1].strip()
        if key in proteins:
            acid_seqs[key] = value
    assert len(acid_seqs) == len(proteins)
    return ppis, acid_seqs

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--pair_dir", type=str, default="./data/dscript/data/pairs")
    parser.add_argument("--seq_dir", type=str, default="./data/dscript/data/seqs")
    parser.add_argument('--processed_dir', type=str, default="./data/dscript/processed")

    # 过滤样本的参数
    parser.add_argument('--max_length', type=int, default=800)
    parser.add_argument('--min_length', type=int, default=50)
    args = parser.parse_args()

    if not os.path.exists(args.processed_dir):
        os.mkdir(args.processed_dir)
        os.mkdir(os.path.join(args.processed_dir, "pairs"))
        os.mkdir(os.path.join(args.processed_dir, "seqs"))

    pair_fns = os.listdir(args.pair_dir)
    for pairfn in pair_fns:
        organism = pairfn.split("_")[0].strip()
        pairfp = os.path.join(args.pair_dir, pairfn)
        fastafp = os.path.join(args.seq_dir, organism + ".fasta")
        ppis, acid_seqs = handle(pairfp, fastafp)

        # 被丢弃的蛋白质
        dropout_proteins = {"Too short": [], "Too long": [],} # "With non-standard acids": []}
        # 蛋白质长度
        protein_lengths = []
        for pro, acid_seq in acid_seqs.items():
            # 筛选蛋白质
            qualified = False
            if len(acid_seq) < args.min_length:
                dropout_proteins['Too short'].append(pro)
            elif len(acid_seq) > args.max_length:
                dropout_proteins['Too long'].append(pro)
            # elif len(set(list(acid_seq)) - set([acid[0] for acid in standard_acids])) > 0:
            #     dropout_proteins['With non-standard acids'].append(pro)
            else:
                qualified = True
            if qualified:
                protein_lengths.append((pro, len(acid_seq)))

        # 输出蛋白质的数量等信息
        if sum([len(value) for value in dropout_proteins.values()]) > 0:
            print(f"============{pairfn.split('.')[0]} Dataset Filter============")
            print(f"Total {len(acid_seqs)} proteins.")
            print(f"\tFilter {len(dropout_proteins['Too short'])} because they are too short.")
            print(f"\tFilter {len(dropout_proteins['Too long'])} because they are too long.")
            # print(f"\tFilter {len(dropout_proteins['With non-standard acids'])} because they have non-standard acids.")
            # 集合过滤掉的蛋白质
            dropout_proteins = set(dropout_proteins['Too long'] + dropout_proteins['Too short'])
            # + dropout_proteins['With non-standard acids'])

            # 删除不合格的蛋白质和ppi
            ppis = [ppi for ppi in ppis if ppi.fst_pro not in dropout_proteins and ppi.sec_pro not in dropout_proteins]
            acid_seqs = {key: value for key, value in acid_seqs.items() if key not in dropout_proteins}

        with open(os.path.join(args.processed_dir, "pairs", pairfn), "w") as f:
            f.writelines([str(ppi) for ppi in ppis])
        with open(os.path.join(args.processed_dir, "seqs", pairfn.split(".")[0] + ".fasta"), "w") as f:
            f.writelines([f"{pro}\t{sequence}\n" for pro, sequence in acid_seqs.items()])

        # print statistics
        print(f"============{pairfn.split('.')[0]} Dataset Statistics============")
        print(f'Total {len(ppis)} positive samples:')
        print(f'\t Positive: {len([ppi for ppi in ppis if ppi.label == 1])}')
        print(f'\t Negative: {len([ppi for ppi in ppis if ppi.label == 0])}')

        print(f"Total {len(acid_seqs)}  Proteins:" )
        print(f"\tMax length of protein: {max([pro[1] for pro in protein_lengths])}")
        print(f"\tMin length of protein: {min([pro[1] for pro in protein_lengths])}")
        print(f"\tAvg length of protein: {round(sum([pro[1] for pro in protein_lengths])/len(protein_lengths), 3)}")


# coding: utf-8

import xlrd

def getProgramList(filepath):
	program_list = dict()
	program_list[u'网络剧'] = []
	program_list[u'网络电影'] = []
	program_list[u'网络综艺'] = []

	data = xlrd.open_workbook(filepath)

	table = data.sheet_by_name(u'网络剧')
	nrows = table.nrows
	ncols = table.ncols
	for i in range(2, nrows):
		one_program = dict()
		one_program[u'节目名称'] = table.cell(i, 1).value
		one_program[u'播出网站'] = table.cell(i, 3).value
		program_list[u'网络剧'].append(one_program)

	table = data.sheet_by_name(u'网络电影')
	nrows = table.nrows
	ncols = table.ncols
	for i in range(2, nrows):
		one_program = dict()
		one_program[u'节目名称'] = table.cell(i, 1).value
		one_program[u'播出网站'] = table.cell(i, 3).value
		program_list[u'网络电影'].append(one_program)

	table = data.sheet_by_name(u'网络综艺')
	nrows = table.nrows
	ncols = table.ncols
	for i in range(2, nrows):
		one_program = dict()
		one_program[u'节目名称'] = table.cell(i, 1).value
		one_program[u'播出网站'] = table.cell(i, 3).value
		program_list[u'网络综艺'].append(one_program)

	return program_list

def get_next_keyword(filepath, idx):
	data = xlrd.open_workbook(filepath)
	table = data.sheet_by_name('Sheet1')
	nrows = table.nrows
	ncols = table.ncols

	return table.cell(idx, 0).value

if __name__ == "__main__":
	program_list = getProgramList(u"征片情况汇总0213.xlsx")
	print(program_list)


#!/usr/bin/env python3
import argparse
from pprint import pprint
import json

import hlib

def check_temp(cl, device_id):

    device_name = None

    # first, try and find the device as a device
    url = f"/clip/v2/resource/device/{device_id}"

    resp = cl.get(url)
    if resp.status_code == 200:
        data = resp.json()
        device = data['data'][0]

        # see if the device has a temperature device
        temp_id = None
        services = device.get('services', [])
        for service in services:
            if service['rtype'] == 'temperature':
                temp_id = service['rid']
                break

        if temp_id is None:
            print("Device has no temperature sensor")
            for service in services:
                print(f"- {service['rtype']}")
            return None, None

        device_name = device['metadata']['name']
        device_id = temp_id

    # now get the temperature
    url = f"/clip/v2/resource/temperature/{device_id}"

    resp = cl.get(url)
    if resp.status_code != 200:
        print(f"Request failed with {resp.status_code} {resp.reason}")
        return None

    data = resp.json()
    device = data['data'][0]

    temp = device['temperature']['temperature']
    return float(temp), device_name

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('device_id', help='id of the device to query', type=str, nargs='+')
    args = parser.parse_args()

    bridge = hlib.find_bridge()
    if bridge is None:
        print("Error: failed to locate a bridge")
        return

    cfg = hlib.load_config()
    cl = hlib.new_client(bridge, cfg['user_name'])

    print("Temperatures:")
    for idx, device_id in enumerate(args.device_id):
        temp, device_name = check_temp(cl, device_id)
        if temp is None:
            continue

        if device_name is None:
            print(f"{idx:02d} {temp}")
        else:
            print(f"{idx:02d} {temp} at {device_name}")

    # check for pushover configuratin
    # token = cfg.get('pushover_token', None)
    # clients = cfg.get('pushover_clients', None)
    # if token and clients:
    #     messages = []
    #     if len(red):
    #         messages.append("RED: incorrectly configured lights")
    #         for light_id, light_name in red.items():
    #             messages.append(f"  - {light_id} {light_name}")
    #
    #     if len(green):
    #         messages.append("GREEN: correctly configured lights")
    #         for light_id, light_name in green.items():
    #             messages.append(f"  - {light_id} {light_name}")
    #
    #     if len(red):
    #         title = "RED: incorrectly configured lights"
    #     else:
    #         title = "GREEN: correctly configured lights"
    #
    #     message = "\n".join(messages)
    #     hlib.send_message(token, clients, message, title)

if __name__ == "__main__":
    main()


import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os

def MyPlotSO(CompMethod,trials,MaxFuncEvals,method,problem):
    ##% Plot
    figcount=0
    Convergence = np.zeros((int(CompMethod.shape[0]/trials), MaxFuncEvals))
    count=-1
    for i in range(0,int(CompMethod.shape[0]),trials):
        count+=1
        for j in range(trials):
            a=np.array(CompMethod[i+j,4])
            b=np.array(Convergence[count,:])
            Convergence[count,:] = a + b
    Convergence=problem['Max/Min']*Convergence
    Convergence= Convergence / trials
    ##% Convergence curves
    PlotConvergence(Convergence,method)
    figcount+=1
    savefigures(figcount,plt)
    #savefigures(plots)
    ##% Violin Plots
    for i in range(2,4):
        #print(i)
        vec = CompMethod[:, i]
        matrix = vec.reshape((int(CompMethod.shape[0]/trials)),trials)
        matrix=matrix.T
        ViolinPlotting(matrix,trials,method)
        figcount+=1
        savefigures(figcount,plt)
    return

def PlotConvergence(Convergence,method):
    # Define colors, styles, and symbols for each line
    colors = ['red', 'blue', 'green', 'orange', 'purple']
    linestyles = ['-', '--', '-.', ':', '-']
    #markers = ['o', 's', 'D', '*', '^']
    # Create a figure and axes
    fig, ax = plt.subplots()
    # Loop through each row of the matrix and plot it as a separate line
    for i in range(Convergence.shape[0]):
        ax.plot(Convergence[i], color=colors[i], linestyle=linestyles[i], label=method[i])
    # Add a legend to the plot
    ax.legend()
    ax.grid()
    # Add title and axis labels
    ax.set_title('Convergence characteristic curves')
    ax.set_xlabel('Function evaluations')
    ax.set_ylabel('Objective function value')
    # Display the plot
    #ax.show()
    return plt

def ViolinPlotting(matrix,trials,method):
    df = pd.DataFrame(matrix, columns=method)
    sns.set_context("talk", font_scale=1)
    plt.figure(figsize=(7,2*matrix.shape[1]))
    plt.grid()
    sns.violinplot(data=df, palette='pastel', bw=.5,orient="h")
    sns.stripplot(data=df,color="black", edgecolor="gray", orient="h")
    return plt

def plot_3d(func, points_by_dim = 50, title = '', bounds = None, cmap = 'twilight', plot_surface = True, plot_heatmap = True):
    from matplotlib.ticker import MaxNLocator, LinearLocator
    """
    Plots function surface and/or heatmap
    Parameters
    ----------
    func : class callable object
        Object which can be called as function.
    points_by_dim : int, optional
        points for each dimension of plotting (50x50, 100x100...). The default is 50.
    title : str, optional
        title of plot with LaTeX notation. The default is ''.
    bounds : tuple, optional
        space bounds with structure (xmin, xmax, ymin, ymax). The default is None.
    save_as : str/None, optional
        file path to save image (None if not needed). The default is None.
    cmap : str, optional
        color map of plot. The default is 'twilight'.
    plot_surface : boolean, optional
        plot 3D surface. The default is True.
    plot_heatmap : boolean, optional
        plot 2D heatmap. The default is True.
    """
    assert (plot_surface or plot_heatmap), "should be plotted at least surface or heatmap!"
    xmin, xmax, ymin, ymax = bounds
    x = np.linspace(xmin, xmax, points_by_dim)
    y = np.linspace(ymin, ymax, points_by_dim)
    a, b = np.meshgrid(x, y)
    data = np.empty((points_by_dim, points_by_dim))
    for i in range(points_by_dim):
        for j in range(points_by_dim):
            data[i,j] = func(np.array([x[i], y[j]]))
    a = a.T
    b = b.T
    l_a, r_a, l_b, r_b = xmin, xmax, ymin, ymax
    l_c, r_c = data.min(), data.max()
    levels = MaxNLocator(nbins=15).tick_values(l_c,r_c)
    if plot_heatmap and plot_surface:
        fig = plt.figure(figsize=(16, 6))
        ax1 = fig.add_subplot(1,2,1)
        ax2 = fig.add_subplot(1,2,2, projection='3d')
    else:
        fig = plt.figure()
        if plot_heatmap:
            ax1 = fig.gca()
        else:
            ax2 = fig.gca(projection='3d')
    #title = r"$\bf{" + title+ r"}$"
    #min_title = title[::]
    def base_plot():
        c = ax1.contourf(a, b, data , cmap=cmap, levels = levels, vmin=l_c, vmax=r_c)
        name = title
        ax1.set_title( name, fontsize = 15)
        ax1.axis([l_a, r_a, l_b, r_b])
        fig.colorbar(c)
    if plot_surface:
        # Plot the surface.
        surf = ax2.plot_surface(a, b, data, cmap =  cmap,  linewidth=0, antialiased=False)
        ax2.contour(a, b, data, zdir='z', levels=30, offset=np.min(data), cmap=cmap)
        # Customize the z axis.
        ax2.set_xlabel('1st dim', fontsize=15)
        ax2.set_ylabel('2nd dim', fontsize=15)
        #ax2.set_zlabel('second dim', fontsize=10)
        ax2.set_zlim(l_c, r_c)
        ax2.zaxis.set_major_locator(LinearLocator(5))
        #ax2.zaxis.set_major_formatter(FormatStrFormatter('%.2f'))
        ax2.tick_params(axis='z', pad=10)
        # Add a color bar which maps values to colors.
        if not plot_heatmap: fig.colorbar(surf)#, shrink=0.5, aspect=5)
        ax2.contour(a, b, data, zdir='z', offset=0, cmap =  cmap)
        ax2.view_init(30, 50)
        #ax2.set_title( min_title , fontsize = 15, loc = 'right')
    if plot_heatmap: base_plot()
    fig.tight_layout()
    #if save_as != None:
    #    plt.savefig(save_as, dpi = 900)
    plt.show()
    return fig

def savefigures(figcount,plt):
    # create a directory to store the figures
    if not os.path.exists('results'):
        os.makedirs('results')
    plt.savefig('results/figure_{}.png'.format(figcount), dpi=900, bbox_inches="tight")
    return


# %%
from pprint import pprint

from datasets import load_dataset

test_file = "../data/ekonspacing/test_small.txt"
val_file = "../data/ekonspacing/val_small.txt"
train_file = "../data/ekonspacing/train_small.txt"

# %%
dataset = load_dataset(
    "ekonspacing.py",
    name="small",
    data_files={"train": str(train_file), "validation": str(val_file), "test": str(test_file)},
    download_mode="force_redownload",
)
print(dataset)
# %%
pprint(dataset["train"][0])
pprint(dataset["test"][0])
# %%


import logging

from .html import extract_html_text
from .pdf import extract_pdf_text

logger = logging.getLogger(__name__)

def extract_text(file_path: str) -> str:
    """Extract text from any kind of file as long as it's html or pdf"""
    try:
        if file_path.endswith('.html'):
            return extract_html_text(file_path)

        if file_path.endswith('.pdf'):
            return extract_pdf_text(file_path)

        raise ValueError(f'Unknown file type {file_path}')
    except Exception as an_exception:  # pylint: disable=W0718
        logger.warning(f'Failed to extract {file_path}: {an_exception}')
        return '' # don't return None or else you can't pipeline this


import numpy as np
try:
    import mc
except Exception:
    pass
import cv2
import os
from PIL import Image

import torch
from torch.utils.data import Dataset
import torchvision.transforms as transforms

import utils
from . import reader

class PartialCompDataset(Dataset):

    def __init__(self, config, phase):
        self.dataset = config['dataset']
        if self.dataset == 'COCOA':
            self.data_reader = reader.COCOADataset(config['{}_annot_file'.format(phase)])
        elif self.dataset == 'Mapillary':
            self.data_reader = reader.MapillaryDataset(
                config['{}_root'.format(phase)], config['{}_annot_file'.format(phase)])
        else:
            self.data_reader = reader.KINSLVISDataset(
                self.dataset, config['{}_annot_file'.format(phase)])
        if config['load_rgb']:
            self.img_transform = transforms.Compose([
                transforms.Normalize(config['data_mean'], config['data_std'])
            ])
        self.eraser_setter = utils.EraserSetter(config['eraser_setter'])
        self.sz = config['input_size']
        self.eraser_front_prob = config['eraser_front_prob']
        self.phase = phase

        self.config = config

        self.memcached = config.get('memcached', False)
        self.initialized = False
        self.memcached_client = config.get('memcached_client', None)

    def __len__(self):
        return self.data_reader.get_instance_length()

    def _init_memcached(self):
        if not self.initialized:
            assert self.memcached_client is not None, "Please specify the path of your memcached_client"
            server_list_config_file = "{}/server_list.conf".format(self.memcached_client)
            client_config_file = "{}/client.conf".format(self.memcached_client)
            self.mclient = mc.MemcachedClient.GetInstance(server_list_config_file, client_config_file)
            self.initialized = True

    def _load_image(self, fn):
        if self.memcached:
            try:
                img_value = mc.pyvector()
                self.mclient.Get(fn, img_value)
                img_value_str = mc.ConvertBuffer(img_value)
                img = utils.pil_loader(img_value_str)
            except:
                print('Read image failed ({})'.format(fn))
                raise Exception("Exit")
            else:
                return img
        else:
            return Image.open(fn).convert('RGB')

    def _get_inst(self, idx, load_rgb=False, randshift=False):
        modal, bbox, category, imgfn, _ = self.data_reader.get_instance(idx)
        centerx = bbox[0] + bbox[2] / 2.
        centery = bbox[1] + bbox[3] / 2.
        size = max([np.sqrt(bbox[2] * bbox[3] * self.config['enlarge_box']), bbox[2] * 1.1, bbox[3] * 1.1])
        if size < 5 or np.all(modal == 0):
            return self._get_inst(
                np.random.choice(len(self)), load_rgb=load_rgb, randshift=randshift)

        # shift & scale aug
        if self.phase  == 'train':
            if randshift:
                centerx += np.random.uniform(*self.config['base_aug']['shift']) * size
                centery += np.random.uniform(*self.config['base_aug']['shift']) * size
            size /= np.random.uniform(*self.config['base_aug']['scale'])

        # crop
        new_bbox = [int(centerx - size / 2.), int(centery - size / 2.), int(size), int(size)]
        modal = cv2.resize(utils.crop_padding(modal, new_bbox, pad_value=(0,)),
            (self.sz, self.sz), interpolation=cv2.INTER_NEAREST)

        # flip
        if self.config['base_aug']['flip'] and np.random.rand() > 0.5:
            flip = True
            modal = modal[:, ::-1]
        else:
            flip = False

        if load_rgb:
            rgb = np.array(self._load_image(os.path.join(
                self.config['{}_image_root'.format(self.phase)], imgfn))) # uint8
            rgb = cv2.resize(utils.crop_padding(rgb, new_bbox, pad_value=(0,0,0)),
                (self.sz, self.sz), interpolation=cv2.INTER_CUBIC)
            if flip:
                rgb = rgb[:, ::-1, :]
            rgb = torch.from_numpy(rgb.astype(np.float32).transpose((2, 0, 1)) / 255.)
            rgb = self.img_transform(rgb) # CHW

        if load_rgb:
            return modal, category, rgb
        else:
            return modal, category, None

    def __getitem__(self, idx):
        if self.memcached:
            self._init_memcached()
        randidx = np.random.choice(len(self))
        modal, category, rgb = self._get_inst(
            idx, load_rgb=self.config['load_rgb'], randshift=True) # modal, uint8 {0, 1}
        if not self.config.get('use_category', True):
            category = 1
        eraser, _, _ = self._get_inst(randidx, load_rgb=False, randshift=False)
        eraser = self.eraser_setter(modal, eraser) # uint8 {0, 1}

        # erase
        erased_modal = modal.copy().astype(np.float32)
        if np.random.rand() < self.eraser_front_prob:
            erased_modal[eraser == 1] = 0 # eraser above modal
        else:
            eraser[modal == 1] = 0 # eraser below modal
        erased_modal = erased_modal * category

        # shrink eraser
        max_shrink_pix = self.config.get('max_eraser_shrink', 0)
        if max_shrink_pix > 0:
            shrink_pix = np.random.choice(np.arange(max_shrink_pix + 1))
            if shrink_pix > 0:
                shrink_kernel = shrink_pix * 2 + 1
                eraser = 1 - cv2.dilate(
                    1 - eraser, np.ones((shrink_kernel, shrink_kernel), dtype=np.uint8),
                    iterations=1)
        eraser_tensor = torch.from_numpy(eraser.astype(np.float32)).unsqueeze(0) # 1HW
        # erase rgb
        if rgb is not None:
            rgb = rgb * (1 - eraser_tensor)
        else:
            rgb = torch.zeros((3, self.sz, self.sz), dtype=torch.float32) # 3HW
        erased_modal_tensor = torch.from_numpy(
            erased_modal.astype(np.float32)).unsqueeze(0) # 1HW
        target = torch.from_numpy(modal.astype(np.int)) # HW
        return rgb, erased_modal_tensor, eraser_tensor, target


import os
import argparse
import matplotlib.pyplot as plt
from matplotlib import cm
import torch
import numpy as np
import statistics as st
import csv
import seaborn as sns

from timeseries import EchoCard
from quality_classification import predict_single as predict_acq
from quality_classification import CardioNet
from heart_segmentation import QuickNat
from heart_segmentation import predict_single as predict_vol

def lighten_color(color, amount=0.5):
    """
    Lightens the given color by multiplying (1-luminosity) by the given amount.
    Input can be matplotlib color string, hex string, or RGB tuple.

    Examples:
    >> lighten_color('g', 0.3)
    >> lighten_color('#F034A3', 0.6)
    >> lighten_color((.3,.55,.1), 0.5)
    """
    import matplotlib.colors as mc
    import colorsys
    try:
        c = mc.cnames[color]
    except:
        c = color
    c = colorsys.rgb_to_hls(*mc.to_rgb(c))
    return colorsys.hls_to_rgb(c[0], 1 - amount * (1 - c[1]), c[2])

class Graph(object):
    '''
    This class collects all information needed to plot graphs and has functions which creates and saves graphs
    Parameters
    ----------
        time_per_pixel: float
            pixel resolution in x axis (time in seconds)
        labels: list of ints with values 0 or 1
            A list containing the model's quality assessment classification results
        sigmoids: list of floats between [0,1]
             A list containing the model's quality assessment sigmoid outputs
        BEtimes: A list of tuples of floats
            A list containing the Begin and End times (in pixels) of the windowing performed on the original timeseries image in
            order to perform classification
    Attributes
    ----------
         time_per_pixel: float
            pixel resolution in x axis (time in seconds)
        labels: list of ints with values 0 or 1
            A list containing the model's quality assessment classification results
        sigmoids: list of floats between [0,1]
             A list containing the model's quality assessment sigmoid outputs
        BEtimes: A list of tuples of floats
            A list containing the Begin and End times (in pixels) of the windowing performed on the original timeseries image in
            order to perform classification
        tot_time: float
            The total time of acquisition
        heatmap: numpy array with the same width as the original timeseries image and 1/3 of its height
            An "image" showing the sigmoid outputs of the network in the regions of the original image
    '''
    def __init__(self, time_per_pixel, labels, sigmoids, BEtimes):
        self.time_per_pixel = time_per_pixel
        self.BEtimes = BEtimes
        self.labels = labels
        self.sigmoids = sigmoids
        self.tot_time = (self.BEtimes[-1][1]-self.BEtimes[0][0])*self.time_per_pixel
        print('Total time of acquisition:', self.tot_time)

    def add_axvspan(self):
        """
        This function is called to add the classification results of the quality assessment as colors in regions of plots
        """
        i=0
        j=0
        for BEtime, label in zip(self.BEtimes, self.labels):
            timeB = BEtime[0]*self.time_per_pixel #int(BEtime[0]*time_per_pixel*len(volumes)/tot_time)
            timeE = BEtime[1]*self.time_per_pixel #int(BEtime[1]*time_per_pixel*len(volumes)/tot_time)
            if label == 1:
                plt.axvspan(timeB, timeE, facecolor='gold', alpha=1, label='_'*i +'Good')
                i+=1
            else:
                plt.axvspan(timeB, timeE, facecolor='midnightblue', alpha=1, label='_'*j +'Bad')
                j+=1

    def make_graph(self, points, volumes, lvids, title, output_path):
        """
        This function creates and saves a graph with two subplots. The first shows the LV Volume over time and
        the second shows the LVID over time. The quality of the image is represented with colors on the graph
        according to the classification results by calling the add_axvspan function.
        Parameters
        ----------
            points: numpy array
                contains the corresponding points of the occurences in volumes and lvids
            volumes: list of floats
                A list containing the LV Volume either for systole, diastole, or all points
            lvids: list of floats
                A list containing the LV Inner Diameters either for systole, diastole, or all points
            title: string
                The title of the figure to be saved
            output_path: string
                The name of the file to be saved
        """
        #f = plt.figure(figsize[12.8, 9.6])
        volume = np.array(volumes)
        lvid = np.array(lvids)

        plt.figure(figsize=[12.8, 9.6])
        # plot LV Vol
        plt.subplot(121) # plt
        #plt.plot(points*self.time_per_pixel, volume) #*10**(9) # [::3] to take every third
        sns.lineplot(points*self.time_per_pixel, volume)
        self.add_axvspan()
        plt.legend()
        plt.grid(True)
        plt.ylabel('LV Vol [mm^3]')
        plt.xlabel('Time [sec]')
        plt.xticks(np.arange(0, self.tot_time, 0.5))
        plt.title('LV Volume')
        # and LVID
        plt.subplot(122)
        #plt.plot(points*self.time_per_pixel, lvid)
        sns.lineplot(points*self.time_per_pixel, lvid)
        self.add_axvspan()
        plt.legend()
        plt.grid(True)
        plt.ylabel('LVID [mm]')
        plt.xlabel('Time [sec]')
        plt.xticks(np.arange(0, self.tot_time, 0.5))
        plt.title('LV Inner Diameters')

        plt.suptitle(title)
        plt.savefig(output_path)

        plt.close() #'all'

    def make_custom_heatmap(self, img):
        """
        This function creates a heatmap with the same width as the original timeseries image and 1/3 of its height
        It is an "image" showing the sigmoid outputs of the network in the regions of the original image
        Parameters
        ----------
            img: numpy array
                The original timeseries image
        """
        self.heatmap = np.zeros((img.shape[0]//3, img.shape[1]))
        for (Btime, Etime), label in zip(self.BEtimes, self.sigmoids):
            self.heatmap[:,Btime:Etime] = label #255*

    def map_sigs_to_colors(self, peaks):
        """
        This function calculates the sigmoid value (continuous value of quality acquisition) at each time point in the list peaks
        Parameters
        ----------
            peaks: numpy array
                A list containing points in time (represented in pixels) during which a diastole occurs
        Returns
        -------
            new_labels: list of floats
                A list containing the corresponding sigmoid value (quality of acquisition) for each point in peaks
        """
        new_labels = []
        for peak in peaks:
            for (Btime, Etime), label in zip(self.BEtimes, self.sigmoids):
                if peak >= Btime and peak < Etime:
                    new_labels.append(label)
                    break
        return new_labels

    def make_hr_graph(self, heartrate, peaks, vols, output_path):
        """
        This function creates a graph with two subplots. The first sublots shows the heartrate over time and the second subplot
        show the LV Vol;d over the heartrates. The quality of the image is represented as an image with continuos colors under the first
        sbuplot according to the classification results. In the second subplot the quality of the image is represented as a heatmap
        where each point in the plot is represented by a different color representing the quality of acquisition during the time the measurement
        was mede
        Parameters
        ----------
            heartrate: list of ints
                Contains a list of the heartrate calculated for each heart beat in [bpm]
            peaks: numpy array
                Contains the points (pixels) corresponding to when the heart is in diastole which were used for the heartrate calculation
            vols: list of floats
                Contains the LV Vol in diastole
            output_path: string
                The name of the file to be saved
        """
        plt.figure(figsize=[12.8, 9.6])
        grid = plt.GridSpec(6, 2, hspace=0.0, wspace=0.2)
        ax_hrt = plt.subplot(grid[:-1, 0])  # grid for graph heartrate-time
        ax_h = plt.subplot(grid[-1, 0])     # grid for classification regions
        ax_vhr = plt.subplot(grid[:, 1])    # grid for graph volume-heartrate
        sig_colors = self.map_sigs_to_colors(peaks)

        ax_hrt.set_xlabel('Time [sec]')
        ax_hrt.set_ylabel('Heart rate [bpm]]')
        ax_hrt.set_xticks(np.arange(0, peaks[-1]*self.time_per_pixel, 0.5))
        ax_hrt.grid()
        ax_hrt.plot(peaks*self.time_per_pixel, np.array(heartrate), '-o')

        ax_h.axis('off')
        h = ax_h.imshow(self.heatmap)
        plt.colorbar(h, ax=ax_h, orientation='horizontal')

        v = ax_vhr.scatter(heartrate, vols, c=sig_colors, cmap='viridis')
        ax_vhr.set_xlabel('Heart rate [bpm]]')
        ax_vhr.set_ylabel('LV Vol;d [mm^3]]')
        ax_vhr.grid()
        plt.colorbar(v, ax=ax_vhr, orientation='horizontal')

        plt.suptitle("Heart rate plots")
        plt.savefig(output_path)
        plt.close()

    def plot_img_mask(self, img, mask, output_path):
        """
        This function plots and saves the original timeseries image and the superimposed segmentation mask of the heart
        Parameters
        ----------
            img: numpy array
                The original timeseries image
            mask: numpy array
                The segmentation mask of the heart inner diameter
            output_path: string
                The name of the file to be saved
        """
        fig, ax = plt.subplots()
        ax.imshow(img, cmap='gray')
        ax.imshow(mask, cmap='winter', alpha=0.3)
        xt = np.arange(0, img.shape[1], step=int(0.5/self.time_per_pixel))
        ax.set_xticks(xt)
        xl = np.round_(xt*self.time_per_pixel, 1)
        ax.set_xticklabels(xl)
        ax.set_yticks([])
        plt.xlabel('Time [sec]')
        plt.savefig(output_path, bbox_inches = 'tight', dpi=1200)
        plt.close()

    def plot_img(self, img, output_path):
        """
        This function plots and saves the original timeseries image and above that the heatmap created by the function make_custom_heatmap
        Parameters
        ----------
            img: numpy array
                The original timeseries image
            output_path: string
                The name of the file to be saved
        """
        heights = [a.shape[0] for a in [self.heatmap, img]]
        widths = [self.heatmap.shape[1]]
        fig_width = 8
        fig_height = fig_width*sum(heights)/sum(widths)
        f, axarr = plt.subplots(2,1, figsize=(fig_width, fig_height+0.4), gridspec_kw={'height_ratios': heights})
        ax = axarr[0].imshow(self.heatmap, cmap='viridis')
        axarr[0].axis('off')
        axarr[1].imshow(img, cmap='gray')
        xt = np.arange(0, img.shape[1], step=int(0.5/self.time_per_pixel))
        axarr[1].set_xticks(xt)
        xl = np.round_(xt*self.time_per_pixel, 1)
        axarr[1].set_xticklabels(xl)
        axarr[1].set_yticks([])
        axarr[1].set_xlabel('Time [sec]')

        plt.subplots_adjust(wspace=0, hspace=0, left=0, right=1, bottom=0, top=1)
        plt.colorbar(ax, ax=axarr[:]) #, orientation='horizontal'
        plt.savefig(output_path, bbox_inches = 'tight', dpi=1200)
        plt.close()

''' ----------- DONE WITH GRAPH CLASS ----------- '''

''' ----------- NEXT COME HELPER FUNCTIONS FOR GETTING STATISTICS AND LOADING MODELS ----------- '''

def get_stats_good(labels, times, peaks, ds, time_res):
    """
    This function calculates various statistics for either the LVIDs in diastole or systole during good quality of acquisition
    Parameters
    ----------
        labels: list of ints, 0 or 1
            Holds the acquisition quality classification result fror each time window
        times: list of tuples
            Each tuple holds the begin and end time of the window cut from the timeseries for the acquisition quality classification
        peaks: numpy array
            Holds the time (in pixels) of the events in ds
        ds: list of floats
            Holds either LVID;d, LVID;s or heartrates
        time_res: float
            The resolution on the x axis, i.e. to how many seconds one pixel corresponds

    Returns
    -------
        med_ds: float
            The median value of all LVIDs in either systols or diastole, or heartrates, captured during good quality of acquisition.
            If no good acquisition regions were found 0 is returned
        avg_ds: float
            The average value of all LVIDs in either systols or diastole, or heartrates, captured during good quality of acquisition.
            If no good acquisition regions were found 0 is returned
        max(good_ds): float
            The maximum value of all LVIDs in either systols or diastole, or heartrates, captured during good quality of acquisition.
            If no good acquisition regions were found 0 is returned
        min(good_ds): float
            The minimum value of all LVIDs in either systols or diastole, or heartrates, captured during good quality of acquisition.
            If no good acquisition regions were found 0 is returned
        good_ds: list of floats
            Includes a list of LVIDs either in systole or diastole, or heartrates, only during good quality of acquisition.
            If no good acquisition regions were found an empty list is returned
        good_times: list of ints
            A list of corresponding times (in seconds) of the above good_ds
            If no good acquisition regions were found an empty list is returned
    """
    good_ds = []
    good_times = []
    for peak, ds in zip(peaks, ds):
        for label, (Btime, Etime) in zip(labels, times):
            if peak >=Btime and peak < Etime:
                if label == 1:
                    good_ds.append(ds)
                    good_times.append(peak*time_res)
                break
    try:
        med_ds = st.median(good_ds)
        avg_ds = sum(good_ds)/len(good_ds)
        return med_ds, avg_ds, max(good_ds), min(good_ds), good_ds, good_times
    except (ZeroDivisionError, st.StatisticsError):
        return 0, 0, 0, 0, good_ds, good_times

def load_model_device(network):
    """
    This function loads the appropriate model and gets the current device
    Parameters
    ----------
        network: string
            Should be either 'echo' or 'quicknat' defining the which model is to be loaded
    Returns
    -------
        net: model.QuickNat or model.CardioNet
            The network model instance
        device: torch.device
            The currently running divice, e.g. cpu, gpu0
    """
    # get device
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    if network=='quicknat':
        model_path = './heart_segmentation/checkpoints/heart-seg-net.pt'
         # load model
        params = {'num_channels':1,
                    'num_class':1,
                    'num_filters':64,
                    'kernel_h':5,
                    'kernel_w':5,
                    'kernel_c':1,
                    'stride_conv':1,
                    'pool':2,
                    'stride_pool':2,
                    'se_block': "NONE",
                    'drop_out':0.2}
        net = QuickNat(params)
        net.to(device=device)
        state_dict = torch.load(os.path.join(model_path), map_location=device)
        net.load_state_dict(state_dict['net_state_dict'])
    else:
        model_path = './quality_classification/checkpoints/quality-clas-net.pth'
        net = CardioNet(n_channels=1, n_classes=1)
        net.to(device=device)
        net.load_state_dict(torch.load(model_path, map_location=device))
    return net, device

''' ----------- DONE WITH HELPER FUNCTIONS ----------- '''

''' ----------- NEXT COMES THE FUNCTION WHERE EVERYTHING IS RUN ----------- '''

def run(input_path, output_path, weight, graphs=True, write=None, write_file=None):
    """
    This function is where the end2end framework is run.
    Parameters
    ----------
        input_path: string
            Path of file to be loaded
        output_path: string
            Directory to save results
        weight: int
            The weight of the mouse we are evaluating
        graphs: bool
            If true graphs will be created and saved in the ouput_path directory
        write: string
            If 'stats' then values such as max, min, median etc of the LVIDs etc are written to a csv file
            If 'all' then LVIDs etc. are written for all good classified regions
        write_file: string
            The csv file to write results to according to what has been given to write
    """
    labels = []
    sigs = []
    masks = []
    # create an echo card instance
    ec = EchoCard(input_path)
    # fill in timeseries attribute of class - a numpy array of entire time of acquisition
    ec.make_timeseries()
    # split timeseries to get images for segmentation network
    vol_windows = ec.make_seg_windows()
    # load models for testing
    echo_net, device = load_model_device('echo')
    quicknat_net, _ = load_model_device('quicknat')
    print("Using device ", device)
    print("Loaded models")
    print('Image shape:', ec.image.shape)

    '''-----------SEGMENTATION PART-----------'''
    # get masks
    for img in vol_windows:
        mask, _ = predict_vol(quicknat_net, device, img, 256)
        masks.append(mask)
    # connect back to one timeseries
    ec.connect_masks(masks)
    # compute volumes and lvids for all points in timeseries
    ec.get_vols()
    # get diastole and systole lvid, lv vol and time of occurence (in pixel values)
    #plt.plot(ec.lvids)

    dpeaks, dlvids, dvols = ec.get_diastoles()
    speaks, slvids, svols = ec.get_systoles()
    '''
    plt.scatter(dpeaks, dlvids, label='g')
    plt.scatter(speaks, slvids, label='r')
    plt.legend()
    plt.show()
    '''
    # get heartrate in [bpm]
    heartrate = ec.get_heartrate(dpeaks)
    '''
    print('AAAA', len(dpeaks), len(speaks), len(heartrate))
    for i in range(len(speaks)):
        if i<len(speaks)-1:
            print(speaks[i], dpeaks[i])
        else:
            print(speaks[i])
    '''
    '''-----------QUALITY ACQUISITION PART-----------'''
    # split timeseries to get images for quality classification
    # two lists are returned - one with numpy arrays (image) one with a tuple (startTime, endTime)
    ec.weight_to_size(weight)
    qual_windows, BEtimes = ec.make_quality_windows_man()
    # classify each window as good or bad
    for i,img in enumerate(qual_windows):
        label, _ = predict_acq(echo_net, device, img, 256)
        sigs.append(label)
        labels.append(np.round(label))

    '''-----------GRAPHS PART-----------'''
    if graphs:
        if not os.path.exists(output_path):
            os.mkdir(output_path)
        graphs = Graph(ec.time_res, labels, sigs, BEtimes)
        graphs.make_custom_heatmap(ec.image)
        #graphs.make_graph(np.arange(len(ec.vols)), ec.vols, ec.lvids, 'Heart Values Estimation', os.path.join(output_path, 'output_vol.png'))
        graphs.make_graph(dpeaks, dvols, dlvids, 'Diastole', os.path.join(output_path, 'output_diastole.png'))
        graphs.make_graph(speaks, svols, slvids, 'Systole', os.path.join(output_path, 'output_systole.png'))
        graphs.plot_img_mask(ec.image, ec.mask, os.path.join(output_path, 'output_img_mask.png'))
        graphs.plot_img(ec.image, os.path.join(output_path, 'output_img.png'))
        graphs.make_hr_graph(heartrate, dpeaks[:-1], dvols[:-1], os.path.join(output_path, 'output_heartrate.png'))

    '''-----------WRITING TO FILES PART-----------'''
    med_diastole, avg_diastole, max_diastole, min_diastole, good_lvid_d, times_lvid_d = get_stats_good(labels, BEtimes, dpeaks, dlvids, ec.time_res)
    med_systole, avg_systole, max_systole, min_systole, good_lvid_s, times_lvid_s = get_stats_good(labels, BEtimes, speaks, slvids, ec.time_res)
    med_heartrate, avg_heartrate, max_heartrate, min_heartrate, good_heartrates, times_hr = get_stats_good(labels, BEtimes, dpeaks[:-1], heartrate, ec.time_res)
    print('Average lvid;d is: ', avg_diastole, ' mm and average lvid;s is: ', avg_systole, ' mm')
    print('Median lvid;d is: ', med_diastole, ' mm and median lvid;s is: ', med_systole, ' mm')
    print('The average heart rate is: ', avg_heartrate, ' bpm and the median heart rate is: ', med_heartrate, ' bpm')
    # append results to file if a csv file has been given in write
    # either stats such as mean, median etc. (1 value for each file)
    if write=='stats':
        filename = input_path.split('/')[-1]
        # if the file doesn't already exist add first row with column names first
        if not os.path.isfile(write_file):
            with open(write_file, 'w', newline='') as csv_file:
                writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
                writer.writerow(['file','median_diastole', 'median_systole', 'median_heartrate', 'avg_diastole', 'avg_systole', 'avg_heartrate', 'max_diastole', 'max_systole', 'max_heartrate', 'min_diastole', 'min_systole', 'min_heartrate'])
        # append new line to file
        with open(write_file, 'a', newline='') as csv_file:
            writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            writer.writerow([filename, med_diastole, med_systole, med_heartrate, avg_diastole, avg_systole, avg_heartrate, max_diastole, max_systole, max_heartrate, min_diastole, min_systole, min_heartrate])
    # or heartrare, lvid;d etc. during all good acquisition regions
    elif write=='all':
        filename = input_path.split('/')[-1]
        # if the file doesn't already exist add first row with column names first
        if not os.path.isfile(write_file):
            with open(write_file, 'w', newline='') as csv_file:
                writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
                writer.writerow(['file','lvid;d', 'lvid;d time', 'lvid;s', 'lvid;s time', 'heart rate', 'heart rate time'])
        # append new lines to file
        with open(write_file, 'a', newline='') as csv_file:
            writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            i = 0
            # depending on windowing and classification these will not necessarily have the exact same length - take the smallest
            min_len = min([len(good_lvid_s), len(good_lvid_s), len(good_heartrates)])
            for i in range(min_len):
                writer.writerow([filename, good_lvid_d[i], times_lvid_d[i], good_lvid_s[i], times_lvid_s[i], good_heartrates[i], times_hr[i]])
                i += 1

def get_args():
    '''
    Required arguments
    ------------------
        -i: The path to the dicom file you wish to extract features from
        -m: The body mass in grams of the current mouse you wish to extract features from
    Optional arguments
    ------------------
        -o: The name of the directory to save graphs, images and csv to. Default is the current working directory.
        -g: if True output graphs and images will be created and saved, if False they will not. Default is True
        -w: If 'all' all features are extracted and saved to a csv, if 'stats' only statistics from echocardiogram are extracted and saved
            (one row per image). Default value is 'all'
        -f: The name of the csv file to write features to. The default value is 'output_all.csv'. If the file already exists then the new
            features will be appended as new rows to the file, but if the file doesn't already exist then it is automatically created.
    '''
    parser = argparse.ArgumentParser(description='Run the end 2 end framework to extract useful heart features from an echocardiogram',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--input', '-i', metavar='INPUT', required=True,
                        help='Specify path of input image - must be in DICOM format')
    parser.add_argument('--mass', '-m', type=int, required=True,
                        help='Specify the body mass of the mouse')
    parser.add_argument('--output', '-o', default='.',
                        help='Specify output path to save graphs')
    parser.add_argument('--graphs', '-g', default=True,
                        help='Specify True or False depending on whether you want to save figures')
    parser.add_argument('--write', '-w', default='all',
                        help='Specify wheter to save all good features or statistics of features. Give all or stars as input.')
    parser.add_argument('--writefile', '-f', default='output_all.csv',
                        help='Specify in which file to save features')
    return parser.parse_args()

if __name__ == '__main__':
    args = get_args()
    run(args.input, args.output, args.mass, graphs=args.graphs, write=args.write, write_file=args.writefile)