#Method 2------ Sort and Scan------------------------the below is O(N^2) like regular scanning like selection sort
#But this method can be improved if we use Merge Sort then N Log N for sorting plus N for scanning of uniqueness then whole algorithm will be
#--- N Log N--------here bubble sort is used
def sort_(temp):
    i=0
    f=len(temp)-1

    while (f-i)>0:
        k=0
        while k < (f-i):
            if temp[k] > temp[k+1]:
                t=temp[k]
                temp[k]=temp[k+1]
                temp[k+1]=t
            k=k+1
        i=i+1

    return temp

#Sort and Scan comparing 1 value with previous value
def check_unique(temp):
    temp=sort_(temp)
    i=1
    flag=1
    while i < len(temp) :
        if temp[i]==temp[i-1]:
            flag=0
            break
        else:
            pass
        i+=1
    return flag

# Main function starting
if __name__=="__main__":

    # Code Regarding the Hashing Function
    input_list=['S','E','A','R','C','H','X','M','P','L']
    pos_list=[]

    # Finding the positions of the characters in the alphabat list
    for i in range(len(input_list)):
        pos_list.append((ord(input_list[i])-ord('A')+1))

    # Two analytical aspects were -----

    #-----------One----------------
    # value in modulus operator with can result in value 0 to M-1
    # A can take value nM+(0 to M-1) because for values of n=(0,1,2.....)
    # we have taken the value of n=0 as others are just same as other n as both lead to same resulting value of the hash function

    #-------------Two-----------------
    # Minimum 10 values are needed in the Table for getting the 10 index in the table so minimum value of M can be 10
    # As we need M bins we need to have at least value of m as 10 so strating with value of m =10
    m_=10
    a_list=[]
    flag=0

    # Loop for checking all values of M which will stop for smallest value of M for which we find certain values of A so as all the hashed index values are unique
    while(1):
        #Iteration for the values of A which can range
        for j in range(m_):
            # Now we need to check that for partcular value of M and A we have unique values of hashed values for 10 input characters
            temp=[((j*x) % m_) for x in pos_list]

            #Method 1------using in buit function ---- we do not know the complexity so i used the method 2
            #To check whether all the values are unique i.e the set the unique values in the list have length as the initial list
            '''if(len(set(temp))==len(input_list)):
                a_list.append(j)
                flag=1'''

            if(check_unique(temp)==1):
                a_list.append(j)
                flag=1

        if(flag==0):
            m_=m_+1
        else:
            break

    #Print the Results
    print("The results are :")
    print("For the smallest value of M which is : ",m_)
    print("The  values of A are : ",a_list)


import os
import csv

us_state_abbrev = {

    'Alabama': 'AL',

    'Alaska': 'AK',

    'Arizona': 'AZ',

    'Arkansas': 'AR',

    'California': 'CA',

    'Colorado': 'CO',

    'Connecticut': 'CT',

    'Delaware': 'DE',

    'Florida': 'FL',

    'Georgia': 'GA',

    'Hawaii': 'HI',

    'Idaho': 'ID',

    'Illinois': 'IL',

    'Indiana': 'IN',

    'Iowa': 'IA',

    'Kansas': 'KS',

    'Kentucky': 'KY',

    'Louisiana': 'LA',

    'Maine': 'ME',

    'Maryland': 'MD',

    'Massachusetts': 'MA',

    'Michigan': 'MI',

    'Minnesota': 'MN',

    'Mississippi': 'MS',

    'Missouri': 'MO',

    'Montana': 'MT',

    'Nebraska': 'NE',

    'Nevada': 'NV',

    'New Hampshire': 'NH',

    'New Jersey': 'NJ',

    'New Mexico': 'NM',

    'New York': 'NY',

    'North Carolina': 'NC',

    'North Dakota': 'ND',

    'Ohio': 'OH',

    'Oklahoma': 'OK',

    'Oregon': 'OR',

    'Pennsylvania': 'PA',

    'Rhode Island': 'RI',

    'South Carolina': 'SC',

    'South Dakota': 'SD',

    'Tennessee': 'TN',

    'Texas': 'TX',

    'Utah': 'UT',

    'Vermont': 'VT',

    'Virginia': 'VA',

    'Washington': 'WA',

    'West Virginia': 'WV',

    'Wisconsin': 'WI',

    'Wyoming': 'WY',

}

EmpID =[]
Name=[]
DOB=[]
SSN=[]
State=[]
header=["EmpID","Name","DOB","SSN","State"]
# Read data_1 file and convert two columns into two lists without the header
with open('employee_data1.csv', encoding="ascii", errors="surrogateescape") as csvfile:
						csv_reader = csv.reader(csvfile)
						next(csv_reader)
						for row in csv_reader:
							EmpID.append(row[0])
							Name.append(row[1])
							DOB.append(row[2])
							SSN.append(row[3])
							State.append(row[4])
# Create a new object by zipping the two lists.
new1_csv=zip(EmpID,Name,DOB,SSN,State)

EmpID =[]
Name=[]
DOB=[]
SSN=[]
State=[]
# Read data_2 file and convert two columns into two lists including the header
with open('employee_data2.csv', encoding="ascii", errors="surrogateescape") as csvfile:
						csv_reader = csv.reader(csvfile)
						next(csv_reader)
						for row in csv_reader:

							EmpID.append(row[0])
							Name.append(row[1])
							DOB.append(row[2])
							SSN.append(row[3])
							State.append(row[4])
# Create a second object by zipping the above two lists.
new2_csv=zip(EmpID,Name,DOB,SSN,State)
#Open a new file and write the above two objects into the new file.
with open("Employee_final.csv", 'w', newline="") as new_file:
		csv_writer = csv.writer(new_file)

		csv_writer.writerow(header)
		#csv_writer=csv.writer(header)
		csv_writer.writerows(new2_csv)
				#csv_writer.writerows(row)
		csv_writer.writerows(new1_csv)

#Open and read the new file for further calculations
with open("employee_final.csv", encoding="ascii", errors="surrogateescape") as csvfile:
		csv_reader = csv.reader(csvfile)
		#Skip the header to do the calculations starting from the second row
		next(csv_reader)
	#Open six empty lists to be filled by data from the newly created CSV file.
		EmpID =[]
		Firstname=[]
		LastName=[]
		DOB=[]
		SSN=[]
		State=[]

		for row in csv_reader:
			EmpID.append(row[0])
			NewName =row[1].split(" ")
			Firstname.append(NewName[0])
			LastName.append(NewName[1])
			NewDOB=row[2].split("-")
			DOB.append(NewDOB[1]+"/"+NewDOB[2]+"/"+NewDOB[0])
			NewSSN=row[3].split("-")
			SSN.append("***-**-"+NewSSN[2])
			State.append(us_state_abbrev[row[4]])

# Zip lists together
cleaned_csv = zip(EmpID, Firstname, LastName, DOB,SSN,State)
# Set variable for output file
output_file = os.path.join("boss_final.csv")

#  Open the output file
with open(output_file, "w", newline="") as datafile:
    writer = csv.writer(datafile)

    # Write the header row
    writer.writerow(["EmpID","First Name","Last Name","DOB","SSN","State"])

    # Write in zipped rows
    writer.writerows(cleaned_csv)


# Use filter to get the list of positive numbers
def get_PosNumbers(input):
    input = [int(element) for element in input]
    output = filter(lambda x : x > 0, input)
    return list(output)

# Use filter to collect words that has first letter is capital
def initial_capsWord(input):
    output = filter(lambda x : x[0].isupper(), input)
    return list(output)

# Use filter to get dict that hasnot specific key or value
def filt_Dict(input):
    output = filter(lambda x: "a" not in x, input)
    return list(output)

dict_list = [] # Initialize an empty list to store dictionaries
user_input = input().split(";")

for item in user_input:
    dct = {}
    temp1 = item.split(" ")

    for pair in temp1:
        temp2 = pair.split(":")
        dct[temp2[0]] = temp2[1]
    dict_list.append(dct)
print(filt_Dict(dict_list))


import torch
import torch.nn as nn
import torch.nn.functional as F
from .GCN_layer import GCN as GraphConv_ordi

class ConvGCN(nn.Module):
    def __init__(self, in_features, num_class, hidden2):
        super(ConvGCN, self).__init__()

        # classification
        self.hidden2 = hidden2
        self.convs2 = nn.ModuleList()
        self.batch_norms2 = nn.ModuleList()
        for i in range(len(self.hidden2)):
            if i == 0:
                conv = GraphConv_ordi(in_features=in_features, out_features=self.hidden2[0])
            else:
                conv = GraphConv_ordi(in_features=self.hidden2[i - 1], out_features=self.hidden2[i])
            self.convs2.append(conv)
            self.batch_norms2.append(nn.BatchNorm1d(self.hidden2[i]))

        self.mlp = nn.Sequential(
            nn.Linear(sum(self.hidden2) * 2, self.hidden2[-1] * 4),
            # nn.BatchNorm1d(self.hidden2[-1]),
            nn.ReLU(True),
            nn.Linear(self.hidden2[-1] * 4, num_class)
        )

    def forward(self, x, edge):
        xs = []
        for conv, bn in zip(self.convs2, self.batch_norms2):
            x = conv(x, edge)
            x = F.relu(bn(x.transpose(1, 2)).transpose(1, 2))
            xs.append(x)
        graph = torch.cat([self.readout(xx) for xx in xs], dim=-1)  # [B,P,d*xx]
        out = self.mlp(graph)
        return out

    def readout(self, x):
        return torch.concat([global_max_pool(x), global_mean_pool(x)], dim=-1)

def global_max_pool(x):  # x:[B,P,d]
    return F.max_pool1d(x.transpose(1, 2), x.shape[1]).squeeze(2)

def global_mean_pool(x):  # x:[B,P,d]
    return F.avg_pool1d(x.transpose(1, 2), x.shape[1]).squeeze(2)


#!/use/bin/env python
from dataclasses import dataclass
from sys import argv
from typing import List

@dataclass
class Monkey(object):
    items: List[int]
    operand: str
    value: str
    test: int
    positive: int
    negative: int
    count: int = 0

    def operation(self, item: int, worry: int = 3, lcm: int = 1) -> int:
        try:
            value: int = int(self.value)
        except ValueError:
            value: int = item

        ret: int = 0

        if self.operand == '+':
            ret = item + value
        elif self.operand == '-':
            ret = item - value
        elif self.operand == '*':
            ret = item * value
        elif self.operand == '/':
            ret = item // value
        else:
            print("PROBLEM")

        return (ret // worry) % lcm

    def divisible(self, operation: int) -> int:
        # print("{} divisible by {} -> {}".format(operation, self.test, operation % self.test))
        if operation % self.test == 0:
            return self.positive
        else:
            return self.negative

def read_file(file: str) -> List[Monkey]:
    with open(file, 'r') as f:
        monkeys: List[Monkey] = []
        for monkey_line in f.read().split("\n\n"):
            parts: List[str] = monkey_line.splitlines()
            items: List[int] = [int(x) for x in parts[1].split(':')[1].split(',')]
            [operand, value] = parts[2].split(':')[1].split()[-2:]
            test = int(parts[3].split()[-1])
            positive: int = int(parts[4].split()[-1])
            negative: int = int(parts[5].split()[-1])

            monkeys.append(
                Monkey(items=items, operand=operand, value=value, test=test, positive=positive, negative=negative))
        return monkeys

def output(monkeys: List[Monkey]):
    for monkey in monkeys:
        print(monkey)

def round(monkeys: List[Monkey], rounds: int = 20, worry=3, mod=1) -> List[Monkey]:
    for _ in range(rounds):
        for _, monkey in enumerate(monkeys):
            for item in monkey.items:
                monkey.count += 1
                worry_level: int = monkey.operation(item, worry=worry, lcm=mod)
                to = monkey.divisible(worry_level)
                monkeys[to].items.append(worry_level)

            monkey.items = []
    return monkeys

def part_one(file: str) -> int:
    monkeys: List[Monkey] = read_file(file)
    lcm: int = 1
    for monkey in monkeys:
        lcm = (lcm * monkey.test)
    monkeys = round(monkeys, worry=3, mod=lcm)
    counts: List[int] = [x.count for x in monkeys]
    counts.sort()
    return counts[-1] * counts[-2]

def part_two(file: str) -> int:
    monkeys: List[Monkey] = read_file(file)

    lcm: int = 1

    for monkey in monkeys:
        lcm = (lcm * monkey.test)

    monkeys = round(monkeys, 10000, worry=1, mod=lcm)
    counts: List[int] = [x.count for x in monkeys]
    counts.sort()
    return counts[-1] * counts[-2]

def main():
    print("Part One: {}".format(part_one(argv[1])))
    print("Part Two: {}".format(part_two(argv[1])))

if __name__ == '__main__':
    main()


import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import skimage.io
import functools
from loguru import logger

logger.info('Import OK')

input_folder = f'SEM-internal-cell-texture/python_results/SEM-internal-cell-texture_Analyzed/feature_collection/'
output_folder = f'SEM-internal-cell-texture/python_results/SEM-internal-cell-texture_Analyzed/summary_calculations/'
scale_folder = f'SEM-internal-cell-texture/python_results/SEM-internal-cell-texture_Analyzed/napari_scale/'

if not os.path.exists(output_folder):
    os.mkdir(output_folder)

# --------------Initialise file lists--------------
# read in calculated pixel data
features = pd.read_csv(f'{input_folder}feature_summary.csv')
features.drop([col for col in features.columns.tolist() if 'Unnamed: ' in col], axis=1, inplace=True)
image_scaled = pd.read_csv(f'{scale_folder}scale_assigned.csv')

# --------------Add pixes per µm factor--------------
# make pixel per µm dictionary
image_scaled_dict = dict(
    zip(image_scaled['image_name'], image_scaled['scaled_pixels_um']))

# map dictionary to features df
features['scaled_pixels_um'] = features['image_name'].map(image_scaled_dict)

features['pixels_nm'] = features['scaled_pixels_um']/1000

# scale: area and axes lengths
features['area_nm'] = (features['area'] / features['pixels_nm'])
features['major_axis_length_nm'] = (
    features['major_axis_length'] / features['pixels_nm'])
features['minor_axis_length_nm'] = (
    features['minor_axis_length'] / features['pixels_nm'])

# --------------Grab major and minor_axis_length for knobs--------------
minor_axis = features[features['ROI_type'] == 'knob'].groupby(
    ['image_name']).mean()['minor_axis_length_nm']
major_axis = features[features['ROI_type'] == 'knob'].groupby(
    ['image_name']).mean()['major_axis_length_nm']

# ----------Calculate average size of knobs per cell----------
av_size = features[features['ROI_type'] == 'knob'].groupby(
    ['image_name']).mean()['area_nm'].reset_index()

# -----------Calculate proportion of area in 'knobs'-----------
knob_area = features[features['ROI_type'] == 'knob'].groupby(['image_name']).sum()['area_nm']

# --------------Calculate number of 'knobs' per cell--------------
# NOTE: due to focus issues, cell shape changes etc, this measure is likely not valid. Generating it just in case!
knob_count = features[features['ROI_type'] == 'knob'].groupby(['image_name']).count()['area_nm']
knobs_per_area = (knob_count / (10000)).reset_index().rename(columns={'area_nm': 'knobs_per_100_nm2'})

# ------------------Summarise, save to csv------------------
summary = functools.reduce(lambda left, right: pd.merge(left, right, on=['image_name'], how='outer'), [av_size, knob_area.reset_index(), knob_count.reset_index(), knobs_per_area, minor_axis, major_axis])
summary.columns = ['image_name', 'mean_knob_area', 'total_knob_area', 'knob_count', 'knobs_per_100_nm2', 'mean_minor_axis', 'mean_major_axis']

# make 'cell_line' column
summary['cell_line'] = summary['image_name'].str.split('-').str[1]

summary.to_csv(f'{output_folder}calculated_properties_summary.csv')

# -------------------visualise calculated parameters-------------------
palette_ko = {
    'CS2': 'midnightblue',
    'OC03': 'palevioletred',
}

for parameter in ['mean_knob_area', 'knob_count', 'knobs_per_100_nm2', 'mean_minor_axis', 'mean_major_axis']:
    fig, ax = plt.subplots()
    sns.swarmplot(x='cell_line', y=parameter,
                  data=summary, palette=palette_ko)
    plt.xlabel('cell_line')
    plt.ylabel(parameter)
    plt.show()

sns.histplot(x=summary['mean_major_axis'],
             kde=True, hue=summary['cell_line'], palette=palette_ko)
plt.show()


from PyQt5 import QtCore
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import *

## ==> SPLASH SCREEN
from ui_SplashScreen import Ui_SplashScreen

## ===> Design's Functions
from ui_functions import UIFunctions

counter = 0

# SPLASH SCREEN
class SplashScreen(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_SplashScreen()
        self.setGeometry(400, 150, 0, 0)
        self.ui.setupUi(self)

        self.functions = UIFunctions(self)

        ## UI ==> INTERFACE CODES
        ########################################################################

        ## REMOVE TITLE BAR
        self.functions.removeTileBare()

        ## DROP SHADOW EFFECT
        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(20)
        self.shadow.setXOffset(0)
        self.shadow.setYOffset(0)
        self.shadow.setColor(QColor(0, 0, 0, 60))
        self.ui.dropShadowFrame.setGraphicsEffect(self.shadow)

        ## QTIMER ==> START
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.progress)
        # TIMER IN MILLISECONDS
        self.timer.start(35)

        ## SHOW ==> MAIN WINDOW
        ########################################################################
        self.show()
        ## ==> END ##


# 17 dic 2014
# aim: use a swich to change the LED status

# imports
import RPi.GPIO as GPIO
import time

# Initializing elements
LED = 4
SWITCH = 27

# Configuration of GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)   	# definimos el GPIO 4 como salida
GPIO.setup(SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# Here a function is defined
def switchingTime():
	print('Iniciamos la ejecucion'.encode('utf-8').decode('utf-8')) # encode-decode is for avoid error when printing into the lxterminal
	while True:

		input_state=GPIO.input(SWITCH)
		if input_state == True:
			GPIO.output(LED, True)		# turn on GPIO 4
		else:
			GPIO.output(LED, False) 	# turn off GPIO 4

		time.sleep(0.05)				# slight delay to avoid debouncing

	print('Ejecucion finalizada'.encode('utf-8').decode('utf-8'))
	GPIO.cleanup()	#clean GPIO

# Call the function
try:
        switchingTime()

# exception that happens when the user presses CTRL+C
except KeyboardInterrupt:
	print("CTRL+C was pressed")

# all other exceptions
except:
        print("Ha habido un error y ha saltado la excepcion".encode('utf-8').decode('utf-8'))

finally:
        GPIO.cleanup()	# to leave the GPIO free for future use


#tupas con la informacion de las cajas
cajas= (
 ("Caja 1", 9, 150), ("Caja 2", 9, 160), ("Caja 3", 153, 200), ("Caja 4", 50, 160),
 ("Caja 5", 15, 60), ("Caja 6", 66, 45), ("Caja 7", 27, 60), ("Caja 8", 39, 40),
 ("Caja 9", 230, 591), ("Caja 10", 520, 10), ("Caja 11", 110, 70), ("Caja 12", 32, 30))

#halla la relacion precio/peso y las ordena de mayor a menor teniendo como key la relacion
def relaciones_preio_peso():
    relaciones = list()#lista donde se almacena cada caja con su respectiva relacion
    for x in cajas:
        relacion = x[2]/x[1]#halla la relacion de cada cja
        relaciones.append([x[0],x[1],x[2],relacion])#la almacena en la lsta auxiliar
    relaciones.sort(key= lambda x: -x[3])#ordena la lista respecto a valor de la relacion de mayor a menor
    for y in relaciones:
        del y[3]#elmina el valor de la relacion de cada caja
    return relaciones

def resultado(rela, peso_t):
    suma_pesos = 0 #variable que suma los pesos de las cajas ingresadas al camion
    dinero_total = 0#variable que suma el valor de cada caja ingresada
    carga = []
    for x in rela:
        suma_pesos += x[1]
        if suma_pesos <= peso_t:
            dinero_total += x[2]
            carga.append(x)
        else:
            suma_pesos -= x[1]

    for z in carga:
        print(z[0],' Peso:',z[1],' Valor:',z[2])
    print('peso total carga= ', suma_pesos, ' ganancia total = ', dinero_total)

peso_total = int(input('ingrese el peso maximo: '))#variable que almacena el peso total del camion
relacion = relaciones_preio_peso()#almacena el arreglo que retorna la funcion
resultado(relacion,peso_total)#muestra los resultados en consola


import random
import os
import sys
import json
import pickle as cp
import numpy as np
import csv
import multiprocessing

from tqdm import tqdm
from rdkit import Chem
from collections import Counter, defaultdict
from gln.common.cmd_args import cmd_args
from gln.common.mol_utils import cano_smarts, smarts_has_useless_parentheses
from gln.mods.rdchiral.template_extractor import extract_from_reaction
from gln.data_process.data_info import DataInfo, load_train_reactions
from gln.common.reactor import Reactor
from gln.mods.mol_gnn.mol_utils import SmartsMols, SmilesMols

def get_dataset(phase):
    file_name = "%s_dataset.json" %phase
    products_list = []
    reactants_list = []
    retro_reaction_set = set()
    with open(file_name, 'r') as f:
        dataset = json.load(f)
        for _, reaction_trees in dataset.items():
            max_num_materials = 0
            final_retro_routes_list = None
            for i in range(1, int(reaction_trees['num_reaction_trees'])+1):
                if len(reaction_trees[str(i)]['materials']) > max_num_materials:
                    max_num_materials = len(reaction_trees[str(i)]['materials'])
                    final_retro_routes_list = reaction_trees[str(i)]['retro_routes']

            for retro_route in final_retro_routes_list:
                for retro_reaction in retro_route:
                    if retro_reaction not in retro_reaction_set:
                        retro_reaction_set.add(retro_reaction)
                        products_list.append(retro_reaction.split('>>')[0])
                        reactants_list.append(retro_reaction.split('>>')[1])

    return products_list, reactants_list

'''build raw template'''
def get_writer(fname, header):
    output_name = os.path.join(cmd_args.save_dir, fname)
    fout = open(output_name, 'w')
    writer = csv.writer(fout)
    writer.writerow(header)
    return fout, writer

def get_tpl(task):
    idx, rxn_smiles = task
    react, reagent, prod = rxn_smiles.split('>')
    reaction = {'_id': idx, 'reactants': react, 'products': prod}
    template = extract_from_reaction(reaction)
    return idx, template

'''get cannonical smarts'''
def process_centers():
    prod_cano_smarts = set()
    react_cano_smarts = set()

    smarts_cano_map = {}
    pbar = tqdm(retro_templates)
    for template in pbar:
        sm_prod, _, sm_react = template.split('>')
        if smarts_has_useless_parentheses(sm_prod):
            sm_prod = sm_prod[1:-1]

        smarts_cano_map[sm_prod] = cano_smarts(sm_prod)[1]
        prod_cano_smarts.add(smarts_cano_map[sm_prod])

        for r_smarts in sm_react.split('.'):
            smarts_cano_map[r_smarts] = cano_smarts(r_smarts)[1]
            react_cano_smarts.add(smarts_cano_map[r_smarts])
        pbar.set_description('# prod centers: %d, # react centers: %d' % (len(prod_cano_smarts), len(react_cano_smarts)))
    print('# prod centers: %d, # react centers: %d' % (len(prod_cano_smarts), len(react_cano_smarts)))

    with open(os.path.join(cmd_args.save_dir, 'prod_cano_smarts.txt'), 'w') as f:
        for s in prod_cano_smarts:
            f.write('%s\n' % s)
    with open(os.path.join(cmd_args.save_dir, 'react_cano_smarts.txt'), 'w') as f:
        for s in react_cano_smarts:
            f.write('%s\n' % s)
    with open(os.path.join(cmd_args.save_dir, 'cano_smarts.pkl'), 'wb') as f:
        cp.dump(smarts_cano_map, f, cp.HIGHEST_PROTOCOL)

'''find centers'''
def find_edges(task):
    idx, rxn_type, smiles = task
    smiles = smiles_cano_map[smiles]

    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return idx, rxn_type, smiles, None
    list_centers = []
    for i, (sm_center, center_mol) in enumerate(prod_center_mols):
        if center_mol is None:
            continue
        if not rxn_type in smarts_type_set[sm_center]:
            continue
        if mol.HasSubstructMatch(center_mol):
            list_centers.append(str(i))
    if len(list_centers) == 0:
        return idx, rxn_type, smiles, None
    centers = ' '.join(list_centers)
    return idx, rxn_type, smiles, centers

'''build all reactions'''
def find_tpls(cur_task):
    idx, (rxn_type, rxn) = cur_task
    reactants, _, raw_prod = rxn.split('>')

    prod = DataInfo.get_cano_smiles(raw_prod)

    if not (rxn_type, prod) in DataInfo.prod_center_maps:
        return None
    reactants = DataInfo.get_cano_smiles(reactants)
    prod_center_cand_idx = DataInfo.prod_center_maps[(rxn_type, prod)]

    neg_reactants = set()
    pos_tpl_idx = {}
    tot_tpls = 0
    for center_idx in prod_center_cand_idx:
        c = DataInfo.prod_cano_smarts[center_idx]
        assert c in DataInfo.unique_tpl_of_prod_center

        tpl_indices = DataInfo.unique_tpl_of_prod_center[c][rxn_type]
        tot_tpls += len(tpl_indices)
        for tpl_idx in tpl_indices:
            cur_t, tpl = DataInfo.unique_templates[tpl_idx]
            assert cur_t == rxn_type
            pred_mols = Reactor.run_reaction(prod, tpl)
            if pred_mols is None or len(pred_mols) == 0:
                continue
            for pred in pred_mols:
                if pred != reactants:
                    neg_reactants.add(pred)
                else:
                    pos_tpl_idx[tpl_idx] = (len(tpl_indices), len(pred_mols))
    return (idx, pos_tpl_idx, neg_reactants)

def get_writer_build_all_reactions(fname, header):
    f = open(os.path.join(cmd_args.save_dir, 'np-%d' % cmd_args.num_parts, fname), 'w')
    writer = csv.writer(f)
    writer.writerow(header)
    return f, writer

##Start processing data

'''build raw template'''
if not os.path.exists(os.path.join(cmd_args.save_dir, 'proc_train_singleprod.csv')):
    rows = []
    products_list, reactants_list = get_dataset('train')
    for i in range(len(reactants_list)):
        rows.append(reactants_list[i]+">>"+products_list[i])

    pool = multiprocessing.Pool(cmd_args.num_cores)
    tasks = []
    for idx, row in tqdm(enumerate(rows)):
        tasks.append((idx, row))

    fout, writer = get_writer('proc_train_singleprod.csv', ['id', 'class', 'rxn_smiles', 'retro_templates'])
    fout_failed, failed_writer = get_writer('failed_template.csv', ['id', 'class', 'rxn_smiles', 'err_msg'])

    for result in tqdm(pool.imap(get_tpl, tasks), total=len(tasks)):
        idx, template = result
        rxn_smiles = rows[idx]

        if 'reaction_smarts' in template:
            writer.writerow([idx, "UNK", rxn_smiles, template['reaction_smarts']])
            fout.flush()
        else:
            failed_writer.writerow([idx, "UNK", rxn_smiles, template['err_msg']])
            fout_failed.flush()

    fout.close()
    fout_failed.close()

'''filter template'''
if not os.path.exists(os.path.join(cmd_args.save_dir, 'templates.csv')):
    proc_file = os.path.join(cmd_args.save_dir, 'proc_train_singleprod.csv')

    unique_tpls = Counter()
    tpl_types = defaultdict(set)
    with open(proc_file, 'r') as f:
        reader = csv.reader(f)
        header = next(reader)
        print(header)
        for row in tqdm(reader):
            tpl = row[header.index('retro_templates')]
            rxn_type = row[header.index('class')]
            tpl_types[tpl].add(rxn_type)
            unique_tpls[tpl] += 1

    print('total # templates', len(unique_tpls))

    used_tpls = []
    for x in unique_tpls:
        if unique_tpls[x] >= cmd_args.tpl_min_cnt:
            used_tpls.append(x)
    print('num templates after filtering', len(used_tpls))

    out_file = os.path.join(cmd_args.save_dir, 'templates.csv')
    with open(out_file, 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['class', 'retro_templates'])
        for x in used_tpls:
            for t in tpl_types[x]:
                writer.writerow([t, x])

'''get canonical smarts'''
if not os.path.exists(os.path.join(cmd_args.save_dir, 'cano_smarts.pkl')):
    tpl_file = os.path.join(cmd_args.save_dir, 'templates.csv')

    retro_templates = []

    with open(tpl_file, 'r') as f:
        reader = csv.reader(f)
        header = next(reader)

        for row in tqdm(reader):
            retro_templates.append(row[header.index('retro_templates')])

    rxn_smiles = []
    rxn_smiles_set = set()
    for phase in ['train', 'valid', 'test']:
        products_list, reactants_list = get_dataset(phase)
        for i in range(len(reactants_list)):
            if reactants_list[i]+">>"+products_list[i] not in rxn_smiles_set:
                rxn_smiles.append(reactants_list[i]+">>"+products_list[i])
                rxn_smiles_set.add(reactants_list[i]+">>"+products_list[i])

    process_centers()

'''find centers'''
if cmd_args.num_parts <= 0:
    num_parts = cmd_args.num_cores
else:
    num_parts = cmd_args.num_parts

if not os.path.exists(os.path.join(os.path.join(cmd_args.save_dir, 'np-%d' % num_parts), '%s-prod_center_maps-part-%d.csv' % ('test', num_parts-1))):
    with open(os.path.join(cmd_args.save_dir, 'cano_smiles.pkl'), 'rb') as f:
        smiles_cano_map = cp.load(f)

    with open(os.path.join(cmd_args.save_dir, 'cano_smarts.pkl'), 'rb') as f:
        smarts_cano_map = cp.load(f)

    with open(os.path.join(cmd_args.save_dir, 'prod_cano_smarts.txt'), 'r') as f:
        prod_cano_smarts = [row.strip() for row in f.readlines()]

    prod_center_mols = []
    for sm in tqdm(prod_cano_smarts):
        prod_center_mols.append((sm, Chem.MolFromSmarts(sm)))

    print('num of prod centers', len(prod_center_mols))
    print('num of smiles', len(smiles_cano_map))

    csv_file = os.path.join(cmd_args.save_dir, 'templates.csv')

    smarts_type_set = defaultdict(set)
    with open(csv_file, 'r') as f:
        reader = csv.reader(f)
        header = next(reader)
        tpl_idx = header.index('retro_templates')
        type_idx = header.index('class')
        for row in reader:
            rxn_type = row[type_idx]
            template = row[tpl_idx]
            sm_prod, _, _ = template.split('>')
            if smarts_has_useless_parentheses(sm_prod):
                sm_prod = sm_prod[1:-1]
            sm_prod = smarts_cano_map[sm_prod]
            smarts_type_set[sm_prod].add(rxn_type)

    if cmd_args.num_parts <= 0:
        num_parts = cmd_args.num_cores
    else:
        num_parts = cmd_args.num_parts

    pool = multiprocessing.Pool(cmd_args.num_cores)

    for out_phase in ['train', 'valid', 'test']:
        rxn_smiles = []
        products_list, reactants_list = get_dataset(out_phase)
        for i in range(len(reactants_list)):
            rxn_smiles.append(("UNK", reactants_list[i]+">>"+products_list[i]))

        part_size = min(len(rxn_smiles) // num_parts + 1, len(rxn_smiles))

        for pid in range(num_parts):
            idx_range = range(pid * part_size, min((pid + 1) * part_size, len(rxn_smiles)))

            local_results = [None] * len(idx_range)

            tasks = []
            for i, idx in enumerate(idx_range):
                rxn_type, rxn = rxn_smiles[idx]
                reactants, _, prod = rxn.split('>')
                tasks.append((i, rxn_type, prod))
            for result in tqdm(pool.imap(find_edges, tasks), total=len(tasks)):
                i, rxn_type, smiles, centers = result
                local_results[i] = (rxn_type, smiles, centers)
            out_folder = os.path.join(cmd_args.save_dir, 'np-%d' % num_parts)
            if not os.path.isdir(out_folder):
                os.makedirs(out_folder)
            fout = open(os.path.join(out_folder, '%s-prod_center_maps-part-%d.csv' % (out_phase, pid)), 'w')
            writer = csv.writer(fout)
            writer.writerow(['smiles', 'class', 'centers'])

            for i in range(len(local_results)):
                rxn_type, smiles, centers = local_results[i]
                if centers is not None:
                    writer.writerow([smiles, rxn_type, centers])
            fout.close()

'''build all reactions'''
if not os.path.exists(os.path.join(cmd_args.save_dir, 'np-%d' % cmd_args.num_parts, 'pos_tpls-part-0.csv')):
    random.seed(cmd_args.seed)
    np.random.seed(cmd_args.seed)
    DataInfo.init(cmd_args)

    fn_pos = lambda idx: get_writer_build_all_reactions('pos_tpls-part-%d.csv' % idx, ['tpl_idx', 'pos_tpl_idx', 'num_tpl_compete', 'num_react_compete'])
    fn_neg = lambda idx: get_writer_build_all_reactions('neg_reacts-part-%d.csv' % idx, ['sample_idx', 'neg_reactants'])

    if cmd_args.num_parts <= 0:
        num_parts = cmd_args.num_cores
        DataInfo.load_cooked_part('train', load_graphs=False)
    else:
        num_parts = cmd_args.num_parts

    train_reactions = load_train_reactions(cmd_args)
    n_train = len(train_reactions)
    part_size = n_train // num_parts + 1

    if cmd_args.part_num > 0:
        prange = range(cmd_args.part_id, cmd_args.part_id + cmd_args.part_num)
    else:
        prange = range(num_parts)
    for pid in prange:
        f_pos, writer_pos = fn_pos(pid)
        f_neg, writer_neg = fn_neg(pid)
        if cmd_args.num_parts > 0:
            DataInfo.load_cooked_part('train', part=pid, load_graphs=False)
        part_tasks = []
        idx_range = list(range(pid * part_size, min((pid + 1) * part_size, n_train)))
        for i in idx_range:
            part_tasks.append((i, train_reactions[i]))

        pool = multiprocessing.Pool(cmd_args.num_cores)
        for result in tqdm(pool.imap(find_tpls, part_tasks), total=len(idx_range)):
            if result is None:
                continue
            idx, pos_tpl_idx, neg_reactions = result
            idx = str(idx)
            neg_keys = neg_reactions

            if cmd_args.max_neg_reacts > 0:
                neg_keys = list(neg_keys)
                random.shuffle(neg_keys)
                neg_keys = neg_keys[:cmd_args.max_neg_reacts]
            for pred in neg_keys:
                writer_neg.writerow([idx, pred])
            for key in pos_tpl_idx:
                nt, np = pos_tpl_idx[key]
                writer_pos.writerow([idx, key, nt, np])
            f_pos.flush()
            f_neg.flush()
        f_pos.close()
        f_neg.close()
        pool.close()
        pool.join()

'''dump graphs'''
if not os.path.exists(os.path.join(cmd_args.save_dir, 'graph_smarts')):
    file_root = cmd_args.save_dir
    if cmd_args.fp_degree > 0:
        SmilesMols.set_fp_degree(cmd_args.fp_degree)
        SmartsMols.set_fp_degree(cmd_args.fp_degree)

    if cmd_args.retro_during_train:
        part_folder = os.path.join(file_root, 'np-%d' % cmd_args.num_parts)
        if cmd_args.part_num > 0:
            prange = range(cmd_args.part_id, cmd_args.part_id + cmd_args.part_num)
        else:
            prange = range(cmd_args.num_parts)
        for pid in prange:
            with open(os.path.join(part_folder, 'neg_reacts-part-%d.csv' % pid), 'r') as f:
                reader = csv.reader(f)
                header = next(reader)
                for row in tqdm(reader):
                    reacts = row[-1]
                    for t in reacts.split('.'):
                        SmilesMols.get_mol_graph(t)
                    SmilesMols.get_mol_graph(reacts)
            SmilesMols.save_dump(os.path.join(part_folder, 'neg_graphs-part-%d' % pid))
            SmilesMols.clear()
        sys.exit()

    with open(os.path.join(file_root, 'cano_smiles.pkl'), 'rb') as f:
        smiles_cano_map = cp.load(f)

    with open(os.path.join(file_root, 'prod_cano_smarts.txt'), 'r') as f:
        prod_cano_smarts = [row.strip() for row in f.readlines()]

    with open(os.path.join(file_root, 'react_cano_smarts.txt'), 'r') as f:
        react_cano_smarts = [row.strip() for row in f.readlines()]

    for mol in tqdm(smiles_cano_map):
        SmilesMols.get_mol_graph(smiles_cano_map[mol])
    SmilesMols.save_dump(os.path.join(cmd_args.save_dir, 'graph_smiles'))

    for smarts in tqdm(prod_cano_smarts + react_cano_smarts):
        SmartsMols.get_mol_graph(smarts)
    SmartsMols.save_dump(os.path.join(cmd_args.save_dir, 'graph_smarts'))


from typing import TYPE_CHECKING, Optional, Tuple

from sydent.users.accounts import Account

if TYPE_CHECKING:
    from sydent.sydent import Sydent

class AccountStore:
    def __init__(self, sydent: "Sydent") -> None:
        self.sydent = sydent

    def getAccountByToken(self, token: str) -> Optional[Account]:
        """
        Select the account matching the given token, if any.

        :param token: The token to identify the account, if any.

        :return: The account matching the token, or None if no account matched.
        """
        cur = self.sydent.db.cursor()
        res = cur.execute(
            "select a.user_id, a.created_ts, a.consent_version from accounts a, tokens t "
            "where t.user_id = a.user_id and t.token = ?",
            (token,),
        )

        row: Optional[Tuple[str, int, Optional[str]]] = res.fetchone()
        if row is None:
            return None

        return Account(*row)

    def storeAccount(
        self, user_id: str, creation_ts: int, consent_version: Optional[str]
    ) -> None:
        """
        Stores an account for the given user ID.

        :param user_id: The Matrix user ID to create an account for.
        :param creation_ts: The timestamp in milliseconds.
        :param consent_version: The version of the terms of services that the user last
            accepted.
        """
        cur = self.sydent.db.cursor()
        cur.execute(
            "insert or ignore into accounts (user_id, created_ts, consent_version) "
            "values (?, ?, ?)",
            (user_id, creation_ts, consent_version),
        )
        self.sydent.db.commit()

    def setConsentVersion(self, user_id: str, consent_version: Optional[str]) -> None:
        """
        Saves that the given user has agreed to all of the terms in the document of the
        given version.

        :param user_id: The Matrix ID of the user that has agreed to the terms.
        :param consent_version: The version of the document the user has agreed to.
        """
        cur = self.sydent.db.cursor()
        cur.execute(
            "update accounts set consent_version = ? where user_id = ?",
            (consent_version, user_id),
        )
        self.sydent.db.commit()

    def addToken(self, user_id: str, token: str) -> None:
        """
        Stores the authentication token for a given user.

        :param user_id: The Matrix user ID to save the given token for.
        :param token: The token to store for that user ID.
        """
        cur = self.sydent.db.cursor()
        cur.execute(
            "insert into tokens (user_id, token) values (?, ?)",
            (user_id, token),
        )
        self.sydent.db.commit()

    def delToken(self, token: str) -> int:
        """
        Deletes an authentication token from the database.

        :param token: The token to delete from the database.
        """
        cur = self.sydent.db.cursor()
        cur.execute(
            "delete from tokens where token = ?",
            (token,),
        )
        deleted = cur.rowcount
        self.sydent.db.commit()
        return deleted


str_lst = list(map(str,input().strip()))
holes = ['A','B','D','O','P','Q','R']
count = 0
for i in str_lst:
    for j in holes:
        if i == j == 'B':
            count += 2
        elif i == j:
            count += 1
print(count,end="")


# 1582. Special Positions in a Binary Matrix
# Easy
# 770
# 29
# Companies
# Given an m x n binary matrix mat, return the number of special positions in mat.

# A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).

# Example 1:

# Input: mat = [[1,0,0],[0,0,1],[1,0,0]]
# Output: 1
# Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
# Example 2:

# Input: mat = [[1,0,0],[0,1,0],[0,0,1]]
# Output: 3
# Explanation: (0, 0), (1, 1) and (2, 2) are special positions.

class Solution:
    def numSpecial(self, mat: List[List[int]]) -> int:
        import numpy as np
        cnt = 0
        tr_mat = np.array(mat).T
        for i in range(len(mat)):
            if sum(mat[i])==1:
                index = mat[i].index(1)
                if sum(tr_mat[index])==1:
                    cnt += 1

        return cnt


# -*- coding: utf-8 -*-
"""
Created on Fri Sep 18 16:11:01 2020

@author: Григорий
"""

# =============================================================================
# Класс модем - используется для формирования заданного типа сигнала и
# модуляции его некоторой последовательностью. Является наследником Model.

# Сигнал как объект создан в Model и является переменной класса.

# code_type определяет тип последовательности: случайная или последовательная в
# размер созвездия.

# Для случайной требуется указать время символа unit_time.
# =============================================================================

import random
from Model import Model
from Signal import Garmonic
from math import pi, log2, ceil
import numpy as np

###############################################################################
class Modem(Model):

    def __init__(self):

        # Параметры модулятора:
        self.sym_number = 10  # Число анализируемых символов
        self.number = 0  # Размерность созвездия
        self.unit_time = 0  # Длительность символа в сигнале
        self.unit_dots = 0  # Количество точек на символ
        self.mod_code = []  # Модуляционная последовательность
        self.code_type = "prop"  # Тип последовательности:
        # "full" в размер созвездия
        # "prob" случайная, количество
        # задается sym_number
        # """время сигнала/время символа"""

    # ------------------------------------------------------------------------------
    # Формирование мод.последовательности в размер созвездия (кодировщик1):
    # Выставляет время символа, так чтобы число символов соотвествовало числу
    # cостояний созвездия

    def Code1(self):

        self.mod_code.clear()
        unit_number = int(log2(self.number))

        # (TODO) Предлагаю рассчитывать длительность всего сигнала, а не длительность
        # одного символа
        self.signal.time = self.unit_time * self.number
        # self.unit_time = self.signal.time/self.number                            # Определение времени символа в зависимости
        # от их количества и времени всего сигнала.
        array = [i for i in range(0, self.number)]  # Массив символов.
        if self.code_type == "full_mix":  # Перемешивание массива по требованию.
            random.shuffle(array)

        for i in range(0, self.number):

            symbol = bin(array[i])[2:]
            symbol = list(map(int, symbol))  # Двоичный номер состояния созвездия
            # т.е символ.
            if len(symbol) < unit_number:
                for i in range(0, unit_number - len(symbol)):  # Дополнение нулями до размера символа.
                    symbol.insert(0, 0)

            self.mod_code.append(symbol)

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

    # Формирование слуайной мод.последовательности (кодировщик2):
    # Время символа задается самостоятельно, размер последовательности
    # количеству поместившихся символов.

    def Code2(self):

        self.mod_code.clear()
        number = int(log2(self.number))

        # Аналогично предыдущему пункту
        self.signal.time = self.unit_time * self.sym_number

        for i in range(0, self.sym_number):
            unit = []
            for j in range(0, number):
                unit.append(random.randint(0, 1))
            self.mod_code.append(unit)

    # ------------------------------------------------------------------------------
    # Вывод мод.последовательности:

    def CodePrint(self):

        print("")
        print("Кодирующая последовательность: ")
        print("")
        for i in range(0, len(self.mod_code)):
            print('{0:4}'.format(i), " ", self.mod_code[i])

    # ==============================================================================
    # Отладка кодировщиков:

    def CodeTest(self, n=1):

        self.number = 16
        self.signal.time = 10

        if n == 1:
            self.Code1()
        if n == 2:
            self.unit_time = self.signal.time / self.number  # Время символа выбрано так, чтобы поместилось
            self.Code2()  # self.number символов

        # self.CodePrint()

    ###############################################################################
    # Инициализация модулятора:
    # Инициализация сигнала и расчет мод.последовательности.

    def Init(self):

        # (TODO) Расчет длительности символа
        # self.unit_time = 2/self.signal.frequency
        if self.code_type == "full" or self.code_type == "full_mix":
            self.Code1()
        if self.code_type == "prob":
            self.Code2()

        self.signal.Init()

        # self.CodePrint()

        self.unit_dots = self.signal.dots_num(self.unit_time)  # Расчет количества точек на символ

    # ------------------------------------------------------------------------------
    # Номер состояния в десятичной системе:

    def State(self, In):

        check = "".join(map(str, In))
        return (int(check, 2))

    # ------------------------------------------------------------------------------
    # Фазоманипулированный сигнал:

    def PM(self):

        self.Init()

        for i in range(0, len(self.mod_code)):  # Цикл по символам.

            state = self.State(self.mod_code[i])  # Перевод символа в десятичную систему.
            phase_shift = 2 * pi * state / self.number  # Расчет фазы.

            # times = np.linspace(i * self.unit_time,
            #                      (i+1) * self.unit_time,
            #                      self.unit_dots, 0)
            # calc_block = Garmonic(
            #       in_i = 1,
            #       in_phase = phase_shift,
            #       in_f = self.signal.frequency,
            #       in_time = times).calc_with_time()
            # self.signal.data = np.hstack((self.signal.data, calc_block))

            for j in range(0, self.unit_dots):  # Цикл по точкам символа.
                now = self.signal.now(j) + i * self.unit_time
                self.signal.Point(now, phase_shift)

    # ------------------------------------------------------------------------------
    # Амплитудофазоманипулированный сигнал:

    def APM(self):

        self.Init()
        phase_shift = 0;

        amp_inc = 0
        phs_shift = 0
        for i in range(0, len(self.mod_code)):  # Схема аналогична PM.

            state = self.State(self.mod_code[i])

            if state < 4:
                amp_inc = 1  # Отличие: фазовый круг обнуляется
                phs_shift = 2 * pi * state / 4  # через 4 символа, а амлитуда увеличивается
                # вдвое.
            if state >= 4 and state < 16:
                amp_inc = 2
                if self.number == 8:
                    phs_shift = 2 * pi * (state - 4) / 4
                if self.number == 16:
                    phs_shift = 2 * pi * (state - 4) / 12

            for j in range(0, self.unit_dots):
                now = self.signal.now(j) + i * self.unit_time
                self.signal.Point(now, phs_shift, [amp_inc, amp_inc])

    # ------------------------------------------------------------------------------
    # Квадратурный сигнал
    def QAM(self):

        self.Init()

        self.signal.modul = '16QAM'

        for i in range(len(self.mod_code)):
            # print(self.State(self.mod_code[i]))
            bin_i = np.array(self.mod_code[i][0:ceil(len(self.mod_code[i]) / 2)])
            bin_q = np.array(self.mod_code[i][ceil(len(self.mod_code[i]) / 2):len \
                (self.mod_code[i])])

            times = np.linspace(i * self.unit_time,
                                (i + 1) * self.unit_time,
                                self.unit_dots, 0)
            calc_block = Garmonic(
                in_i=2 * (self.State(bin_i.tolist()) - \
                          ((2 ** bin_i.size) - 1.0) / 2),
                in_q=2 * (self.State(bin_q.tolist()) - \
                          ((2 ** bin_q.size) - 1.0) / 2),
                in_f=self.signal.frequency,
                in_time=times).calc_with_time()
            self.signal.data = np.hstack((self.signal.data, calc_block))

    # ------------------------------------------------------------------------------
    # Частотномодулированный сигнал:

    def FM(self):

        self.Init()

        for i in range(0, len(self.mod_code)):  # Схема аналогична PM

            state = self.State(self.mod_code[i])
            deviation = (1 - 2 * state) * self.signal.frequency / 4  # Отличие: вместо фазы меняется частота

            for j in range(0, self.unit_dots):
                now = self.signal.now(j) + i * self.unit_time
                self.signal.Point(now, freq_dev=deviation)

    def OFM(self):
        self.Init()
        for i in range(len(self.mod_code)):
            state = self.State(self.mod_code[i])
            deviation = 2 * state / self.unit_time

            times = np.linspace(i * self.unit_time,
                                (i + 1) * self.unit_time,
                                self.unit_dots, 0)
            calc_block = Garmonic(
                in_i=1,
                in_f=self.signal.frequency + deviation,
                in_time=times).calc_with_time()
            self.signal.data = np.hstack((self.signal.data, calc_block))

    # ------------------------------------------------------------------------------
    # Сигнал с минимальным сдвигом:

    def MSK(self, shift=pi / 2):

        self.signal.phase = shift
        self.PM()

        self.signal.phase = 0

    # ==============================================================================
    # Отладка модулятора:

    def Test(self):

        self.number = 16
        #    self.signal.time = 10
        self.signal.frequency = 1

        self.unit_time = self.signal.time / self.number  # Время символа требуется указывать
        self.code_type = "full"  # для случайной последовательности

        #    self.PM()
        self.APM()
        #    self.FM()
        #    self.MSK()

        self.signal.Plot()

# ==============================================================================


from csv import DictReader

from django.core.management import BaseCommand
from reviews.models import Review

class Command(BaseCommand):
    def handle(self, *args, **options):
        if Review.objects.exists():
            print('Все данные в таблицу Review уже загружены')
            return

        for row in DictReader(open('static/data/review.csv')):
            review = Review(id=row['id'],
                            title_id=row['title_id'],
                            text=row['text'],
                            author_id=row['author'],
                            score=row['score'],
                            pub_date=row['pub_date'],
                            )
            review.save()

        print('Данные в таблицу Review успешно загружены')


#!/usr/bin/env python3

"""
KISS device factory unit tests.
"""

from aioax25.kiss import \
        SerialKISSDevice, \
        SubprocKISSDevice, \
        TCPKISSDevice, \
        make_device

def test_serial_kiss():
    """
    Test we can create a ``SerialKISSDevice``.
    """
    dev = make_device(type='serial', device='/dev/ttyS0', baudrate=9600)
    assert isinstance(dev, SerialKISSDevice)
    assert dev._device == '/dev/ttyS0'
    assert dev._baudrate == 9600

def test_subproc_kiss():
    """
    Test we can create a ``SubprocKISSDevice``.
    """
    dev = make_device(type='subproc', command=['somecmd', 'a', 'b', 'c'])
    assert isinstance(dev, SubprocKISSDevice)
    assert dev._command == ['somecmd', 'a', 'b', 'c']
    assert dev._shell is False

def test_tcp_kiss():
    """
    Test we can create a ``TCPKISSDevice``.
    """
    dev = make_device(type='tcp', host='localhost', port=10000)
    assert isinstance(dev, TCPKISSDevice)
    assert dev._conn_args == dict(
            host='localhost', port=10000,
            ssl=None, family=0, proto=0, flags=0,
            sock=None, local_addr=None, server_hostname=None
    )

def test_unknown_kiss_type():
    """
    Test that unknown KISS device types are rejected
    """
    try:
        dev = make_device(type='bogus')
        assert False, 'Should not have worked, got a %r' % (dev,)
    except ValueError as e:
        assert str(e) == ("Unrecognised type=%r" % ("bogus",))


#!/bin/python

import os
import subprocess

from gopest.common import config as cfg
from gopest.common import runtime

"""
Run this script to submit BeoPEST jobs on NeSI using Slurm.  This includes
master and slaves jobs.  Note the BeoPEST here uses TCP communication.

1. Modify the following paragraph of settings.
2. Set up PEST directory properly.
3. Run "python submit_beopest.py" from the main PEST directory.

Angus Yeh
a.yeh@auckland.ac.nz
June 2015
"""

PROJECT = cfg['nesi']['project']
PROJECT_MAUI = cfg['nesi']['maui']['project']
PROJECT_MAHUIKA = cfg['nesi']['mahuika']['project']

WALLTIME_MASTER = cfg['nesi']['walltime_master']
WALLTIME_SLAVES = WALLTIME_MASTER

WALLTIME_FORWARD = cfg['nesi']['walltime_forward']

MAUI_NTASKS = cfg['nesi']['maui']['ntasks']
MAHUIKA_NTASKS = cfg['nesi']['mahuika']['ntasks']

NUM_SLAVES = cfg['pest']['num_slaves']

MEM_PER_SLAVE = "5000" # MB
MEM_MASTER = "500"
MEM_FORWARD = "4000"

# SIMULATOR = "~/bin/autough2_6b" # specify absolute path if not in system path
# SIMULATOR = "waiwera-Mahuika" # specify absolute path if not in system path
# 'waiwera': local native waiwera, installed in path
# 'waiwera-dkr': running locally using Docker with pywaiwera installed
# 'waiwera-Maui': calling submit_beopest.py and use Maui
# 'waiwera-Mahuika': calling submit_beopest.py and use Mahuika
SIMULATOR = cfg['simulator']['executable']

PST_NAME = cfg['pest']['case-name']
PESTDIR = cfg['pest']['dir']
SLAVEDIR = cfg['pest']['slave_dirs']
BEOPEST = cfg['pest']['executable']
PORT = cfg['pest']['port']

SWITCHES = " ".join(cfg['pest']['switches'])
# additional swiches for beopest, eg /s for restart, /p1 for parallise 1st model run
# should not use /p1 with svda, no /p1 with obsreref_10 either
# /hpstart, make sure PST_NAME.hp exists
# /i jco reuse, make sure PST_NAME.jco exists
# NOTE it's possible to use /hpstart and /i together, will start update tests right away
# NOTE working directory is assumed to be where this script is launched

# if use /f, PEST_HP runs a sets of forward runs using .par files
F_PAR_SETS = ["prandom", "11", "50", "40", PST_NAME+".rrf"]
# Enter filename base of parameter value files:
# Enter first index to use:
# Enter last index to use:
# Enter parallel run packet size:
# Enter name for run results file:

# used for slurm to redirect as standard input
use_input = False
with open('_input', 'w') as f:
    # if PEST_HP asks name of jacobian file, then use file named PST_NAME + ".jco"
    if "/i" in SWITCHES:
        f.write(PST_NAME + ".jco\n")
        use_input = True
    if "/f" in SWITCHES:
        f.write("\n".join(F_PAR_SETS))
        use_input = True

SLAVES_ON_SCRATCH = False
KEEP_TARGZ_SLAVES = False
# normally when a slave ends, the slave directory will be tar-gz-ed for debugging
# but this can be huge due to TOUGH2 listing files, these files can be summarised
# (using head and tail) to save space
SUMMARY_LARGE_FILES = "" #"*.listing"

MAIN_DIR = os.getcwd()

# use absolute path if known
if PESTDIR:
    BEOPEST = os.path.join(PESTDIR,BEOPEST)

# list NeSI modules that needs to load for running scripts/AUTOUGH2 etc.
ENV_MODULES = [
    'module load gimkl/2018b',
    'module load Python-Geo/3.7.3-gimkl-2018b',
    'source /nesi/project/uoa00124/env-py3-gopest/bin/activate',
    # 18/12/2022 3:34:08 a.m.
    # # "module GCC/4.9.2",
    # # "module Python/2.7.11-foss-2015a",
    # "module load gimkl/2017a",
    # "module load GCC/7.1.0",
    # "module load Python-Geo/2.7.14-gimkl-2017a",
]
# user can load these modules by command 'source _load_modules.sh'
with open('_load_modules.sh', 'w') as f:
    f.write('\n'.join([
        "echo !!! Please source this file to modify environment in calling shell",
        "echo \"    'source _load_modules.sh'\"",
        ] + ENV_MODULES))

ENV_MODULES_MAUI = cfg['nesi']['maui']['env_init']
ENV_MODULES_MAHUIKA = cfg['nesi']['mahuika']['env_init']

# communication files:
# pest files
# goPEST*
# pest_model*
# real_model*       (from gopest.common.runtime)
# data_*            (from toml)
# gs_*              (from toml)

pst_name = cfg['pest']['case-name']
slave_files = [
    pst_name + '.pst',
    # communication files:
    "_input",
    "_pest_dir",
    "_tough2",
    "_logfile",
    "_master_out",
    # goPEST working files
    "pest_model.ins",
    "pest_model.tpl",
    "goPESTconfig.toml",
    "goPESTpar.list",
    "goPESTobs.list",
    # user supplied function, may not exist
    "goPESTuser.py",
]
# forward run files -> generated from toml
slave_files += [
    # runtime['filename']['save'],
    runtime['filename']['incon'],
    runtime['filename']['dat_orig'],
]
slave_files += runtime['filename']['all_geoms']
slave_files += runtime['filename']['dat_seq']
# slave_files += runtime['filename']['lst_seq']
# user files -> copied from toml
slave_files += cfg['files']['slave']

master_files = slave_files
if "/hpstart" in cfg['pest']['switches']:
    master_files.append(pst_name + '.hp')
if "/i" in cfg['pest']['switches']:
    master_files.append(pst_name + '.jco')

master_files += cfg['files']['master']

# All files needed in slave directory
# TODO: should have both master_files and slave_files
FILE_LIST = " ".join(slave_files)

"""
Details
-------

This explains the working solution of using BeoPEST on NeSI, which is
implemented by this script.

This version of BeoPEST works uses TCP, MPI not supported with some SVD stuff
John Doherty added (11/12/2014).

Slurm job files generated for submission:

    _master_job.sl   -> this is job file for launching master beopest along with
                        a set of slaves, using N+1 cpus
    _slaves_job.sl   -> job file for slaves, using N cpus
    _multi.conf      -> used by slaves job file, calls _run_a_slave.sh
    _run_master.sh   -> record which host it is running on, run master beopest
    _run_a_slave.sh  -> copy required files to unique slave directories, then
                        launch single beopest in slave mode.

    _master_dir      -> so slave jobs know where master directory is
    _master_out      -> so slave jobs know where to print information
    _pest_dir        -> so slaves know where to find PEST/BeoPEST utilities
    _tough2          -> so slaves know which SIMULATOR simulator to use

This set of scripts/job files basically launch a beopest as master in the main
directory by calling:

    beopest case /h :4004

In the master job file, a command is used to detect which host it is run on. The
info is saved to a file _master_host in the main direcotry.  Hence the slaves
are set to run with a small delay, to ensure the existence _master_host file.

Now a sbatch --dependency after:JOB_ID is used to launch slaves.  Note it's
after: not afterok: as seen in the examples.  This runs the slaves after master
STARTED (instead of finished).

The slaves job file uses srun --multi-prog, which reads _multi.conf file that
tells how to launch each slave.  Each slave is launched by using a bash script.
The script includes the code to:
    1. create unique slave dir, and copy required files
    2. change directory
    3. obtain the hostname of master beopest (from file _master_host)
    4. launch beopest case /h masterhost:4004

Also something bad might happen if more than one BeoPEST master uses the same
communication port.  So it's a good idea to set PORT to something different
when you receive this script.  Usually some large number is okay.

Tips For Users
--------------
    - You can launch more slave jobs by copy commands shown on screen after
      submission.  Note the dependency jobid is important.
    - Job ids are shown on the screen, you can cencel jobs by "scancel JOBID"
    - cleanups can be done by:
          rm -r slave_*
          rm *.out
          rm _*
      (be carefule not to delete your own files, if they match the names here.)
    - On linux, you have to change a script into an 'executable' if you want to
      run it directly like "./myscript.sh", instead of "bash myscript.sh".
      This requires you to issue the command:
          chmod +x myscript.sh
    - Within each of the slave directory, you can find a file called
      _master_dir, this file contains the master's working directory.  This is
      useful if some scripts running on a slave need to know where the master
      job is located.  (I use this to aid the transfer of updated .incon files
      across all slaves.)  You can use a command like this to get it back into
      a bash variable:
          MASTERDIR=`cat _master_dir`
          cp ${MASTERDIR}/a ./b
    - I use rsync to copy everything in a dir to another.  This is useful when
      you want to make some changes to a set of PEST setup/files.  Be careful
      with the slashes here.  eg.
          rsync -ar --progress case_1/ case_wai_2

NeSI related:
    - Read the first page when ssh into nesi login node.
    - use command show_my_projects to see what projects you are on, these
      proejcts directory should be where you keep model files etc.
    - compiling must be done on the build nodes, go to build node by ssh
      build-wm.
    - use private keys to simplify the login/scp procedure, on Windows I use
      Putty and related tools such as WinSCP etc.

TODOs
-----
    - make cleaning up easier

"""

# python 2.6 does not support the new/easier subprocess.check_output
def check_output_old(cmd):
    ''' so use os.system with "> /dev/null 2>&1" '''
    import tempfile
    handle, fname = tempfile.mkstemp(prefix='tmpout', dir='.', text=True)
    code = os.system(cmd + "> " + fname)
    with open(fname, 'r') as f:
        out = f.readlines()
    os.close(handle)
    os.remove(fname)
    if code != 0:
        print("Command %s failed: %s" % (cmd, "".join(out)))
    return "".join(out)

def check_output(cmd):
    return subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).decode()

def write_to(filename, line):
    with open(filename, 'w') as f:
        f.write(line)

def get_master_dir():
    """ _master_dir is created and copied to slave dirs """
    try:
        with open('_master_dir', 'r') as f:
            line = f.readlines()[0].strip()
            return line
    except:
        return '..'

def get_slave_id():
    """ returns a string as id, needs to be called from within a 'slave' directory.
    NOTE _procid should be written by the master program that invokes slaves.
         Otherwise the current (sub) directory name will be used. """
    try:
        with open('_procid', 'r') as f:
            line = f.readlines()[0].strip()
            return line
    except:
        # get get only the last part of current working dir
        return os.path.basename(os.path.normpath(os.getcwd()))

def get_master_out():
    try:
        with open('_master_out', 'r') as f:
            line = f.readlines()[0].strip()
            return line
    except:
        return 'STDOUT'

def create_job_name():
    """ use directory's name, and make sure spaces replaced by _, for now. """
    name = os.path.split(check_output("pwd"))[-1].replace(' ','').replace('_','')
    if name.startswith('case'):
        name = 'c' + name[4:]
    return name.strip()

def gen_master_sl(fname="_master_job.sl"):
    jobname = create_job_name()
    txt = [
        "#!/bin/bash",
        "#SBATCH -J %s" % jobname,
        "#SBATCH -A %s         # Project Account" % PROJECT,
        "#SBATCH --time=%s     # Walltime" % WALLTIME_MASTER,
        # "#SBATCH --ntasks=1          # number of tasks",
        # "#SBATCH --mem=%s  # memory/cpu (in MB)" % MEM_MASTER,
        # # "#SBATCH --workdir=%s  # working dir" % MAIN_DIR,
        # "#SBATCH --ntasks=%i         # number of tasks" % int(NUM_SLAVES / 5),
        "#SBATCH --ntasks=1          # number of tasks",
        "#SBATCH --cpus-per-task=%i  # number of CPUs" % max(int(NUM_SLAVES / 5),1),
        "#SBATCH --overcommit        # allow many tasks on one CPU",
        "#SBATCH --mem=%i  # memory/cpu (in MB)" % (int(MEM_PER_SLAVE) * int(NUM_SLAVES / 5) + int(MEM_MASTER)),
        "#SBATCH --profile task",
        "#SBATCH --acctg-freq=1",
        ]
    if use_input:
        txt += ["#SBATCH --input=_input"]
    txt += [
        "",
        "echo running %s..." % fname,
        "",
        "rm -rf _jobs",
        "mkdir _jobs",
        "",
        "function finish {",
        "  cd _jobs",
        "  for f in *",
        "  do",
        "    echo EXIT $SLURM_JOB_ID, master script cancelling child job: $f",
        "    scancel --clusters=maui,mahuika $f",
        "  done",
        "  cd ..",
        "}",
        "trap finish EXIT",
        "",
        ]
    txt += ENV_MODULES
    txt += [
        "",
        "MASTERDIR=`pwd`",
        "echo ${MASTERDIR} > _master_dir",
        "",
        # "rm -f run_ns_pr.log",
        # "echo ${MASTERDIR}/run_ns_pr.log > _logfile",
        "",
        "# run master",
        # "srun --exclusive -n1 bash %s/_run_master.sh" % MAIN_DIR,
        "bash %s/_run_master.sh &" % MAIN_DIR,
        "",
        "",
        "# run slaves",
        "echo starting %i PEST slaves on single node..." % NUM_SLAVES,
        "for i in {1..%i}" % NUM_SLAVES,
        "do",
        "    bash _run_a_slave.sh ${i} &",
        "done",
        "",
        "# for both background shell jobs (master and slaves srun) to finish",
        "wait",
        "",
        ]
    with open(fname, 'w') as f:
        f.write("\n".join(txt))

def gen_slaves_sl(fname="_slaves_job.sl"):
    # be careful, might not work, if str contains txt such as 'MB'
    jobname = 'S_' + create_job_name()
    txt = "\n".join([
        "#!/bin/bash",
        "#SBATCH -J %s" % jobname,
        "#SBATCH -A %s         # Project Account" % PROJECT,
        "#SBATCH --time=%s     # Walltime" % WALLTIME_SLAVES,
        "#SBATCH --ntasks=1          # number of tasks",
        "#SBATCH --cpus-per-task=%i  # number of CPUs" % max(int(NUM_SLAVES / 10),1),
        "#SBATCH --overcommit        # allow many tasks on one CPU",
        "#SBATCH --mem=%i  # memory/cpu (in MB)" % (int(MEM_PER_SLAVE) * int(NUM_SLAVES / 5)),
        "#SBATCH --profile task",
        "#SBATCH --acctg-freq=1",
        # "#SBATCH --workdir=%s  # working dir" % MAIN_DIR,
        "echo running %s..." % fname,
        ] + ENV_MODULES + [
        "",
        "# run slaves",
        "echo starting %i PEST slaves on single node..." % NUM_SLAVES,
        "for i in {1..%i}" % NUM_SLAVES,
        "do",
        # "    echo slave id ${i}",
        "    bash _run_a_slave.sh ${i} &",
        "done",
        "",
        "# for both background shell jobs (master and slaves srun) to finish",
        "wait",
        "",
        ])
    f = open(fname, 'w')
    f.write(txt)
    f.close()

def gen_forward_sl(cmd, fname="_forward.sl"):
    cwd = os.getcwd()
    txt = "\n".join([
        "#!/bin/bash",
        "#SBATCH -J %s" % get_slave_id(),
        "#SBATCH -A %s    # Project Account" % PROJECT,
        "#SBATCH --time=%s    # Walltime" % WALLTIME_FORWARD,
        "#SBATCH --ntasks=1    # number of tasks",
        "#SBATCH --mem=%s    # memory/cpu (in MB)" % MEM_FORWARD,
        # "#SBATCH --workdir=%s    # working dir" % cwd,
        # "#SBATCH --output=%s # print to master .out" % get_master_out(),
        # "#SBATCH --error=%s # print to master .out" % get_master_out(),
        # "#SBATCH --open-mode=append # make sure don't overwrite master out",
        "",
        "function finish {",
        "  echo -- forward job $SLURM_JOB_ID exiting at $(pwd)",
        "  rm _status_on_nesi",
        "  rm %s/_jobs/$SLURM_JOB_ID" % get_master_dir(),
        "}",
        "trap finish EXIT",
        "",
        "",
        "echo -- forward job $SLURM_JOB_ID starting at $(pwd)",
        "touch %s/_jobs/$SLURM_JOB_ID" % get_master_dir(),
        "",
        ] + ENV_MODULES + [
        "",
        "srun %s" % cmd,
        "",
        ])
    with open(fname, 'w') as f:
        f.write(txt)

def gen_forward_mahuika_sl(cmd, fname="_forward.sl"):
    cwd = os.getcwd()
    txt = "\n".join([
        "#!/bin/bash -e",
        "#SBATCH -J %s" % get_slave_id(),
        "#SBATCH -A %s    # Project Account" % PROJECT_MAHUIKA,
        "#SBATCH --export=NONE      # don't carry env over",
        "#SBATCH --time=%s    # Walltime" % WALLTIME_FORWARD,
        "#SBATCH --ntasks=%i    # number of tasks" % MAHUIKA_NTASKS,
        "#SBATCH --mem=%s    # memory/cpu (in MB)" % MEM_FORWARD,
        #"#SBATCH --mem-per-cpu=%i  # memory/cpu (in MB)" % int(float(MEM_FORWARD)/float(MAHUIKA_NTASKS)),
        #"#SBATCH --mem=%s    # memory/cpu (in MB)" % MEM_FORWARD,
        #"#SBATCH --output=%s # print to master .out" % get_master_out(),
        #"#SBATCH --error=%s # print to master .out" % get_master_out(),
        #"#SBATCH --open-mode=append # make sure don't overwrite master out",
        "",
        "function finish {",
        "  echo -- forward job $SLURM_JOB_ID exiting at $(pwd)",
        "  rm _status_on_nesi",
        "  rm %s/_jobs/$SLURM_JOB_ID" % get_master_dir(),
        "}",
        "trap finish EXIT",
        "",
        "",
        "echo -- forward job $SLURM_JOB_ID starting at $(pwd)",
        "touch %s/_jobs/$SLURM_JOB_ID" % get_master_dir(),
        "",
        ] + ENV_MODULES_MAHUIKA + [
        "",
        "export SLURM_EXPORT_ENV=ALL",
        "srun %s" % cmd,
        "",
        ])
    with open(fname, 'w') as f:
        f.write(txt)

def gen_forward_maui_sl(cmd, fname="_forward.sl"):
    cwd = os.getcwd()
    txt = "\n".join([
        "#!/bin/bash -e",
        "#SBATCH -J %s" % get_slave_id(),
        "#SBATCH -A %s    # Project Account" % PROJECT_MAUI,
        "#SBATCH --clusters=maui    # from mahuika to maui",
        "#SBATCH --export=NONE      # don't carry env over",
        "#SBATCH --time=%s    # Walltime" % WALLTIME_FORWARD,
        "#SBATCH --ntasks=%i    # number of tasks" % MAUI_NTASKS,
        #"#SBATCH --mem-per-cpu=%i  # memory/cpu (in MB)" % int(float(MEM_FORWARD)/float(MAUI_NTASKS)),
        "#SBATCH --mem=%s    # memory/cpu (in MB)" % MEM_FORWARD,
        #"#SBATCH --output=%s # print to master .out" % get_master_out(),
        #"#SBATCH --error=%s # print to master .out" % get_master_out(),
        #"#SBATCH --open-mode=append # make sure don't overwrite master out",
        "#SBATCH --partition=nesi_research",
        #"#SBATCH --qos=nesi_debug",
        "",
        "function finish {",
        "  echo -- forward job $SLURM_JOB_ID exiting at $(pwd)",
        "  rm _status_on_nesi",
        "  rm %s/_jobs/$SLURM_JOB_ID" % get_master_dir(),
        "}",
        "trap finish EXIT",
        "",
        "",
        "echo -- forward job $SLURM_JOB_ID starting at $(pwd)",
        "touch %s/_jobs/$SLURM_JOB_ID" % get_master_dir(),
        "",
        ] + ENV_MODULES_MAUI + [
        "",
        "export SLURM_EXPORT_ENV=ALL",
        "srun %s" % cmd,
        "",
        ])
    with open(fname, 'w') as f:
        f.write(txt)

def gen_run_master(fname="_run_master.sh"):
    if use_input:
        cmd = "%s %s %s /h :%s < _input" % (BEOPEST, PST_NAME, SWITCHES, PORT)
    else:
        cmd = "%s %s %s /h :%s" % (BEOPEST, PST_NAME, SWITCHES, PORT)
    txt = "\n".join([
        "#!/bin/bash",
        "",
        "echo Master working at $(hostname)",
        "echo $(hostname) > _master_host",
        "",
        "echo $(pwd)/slurm-${SLURM_JOB_ID}.out > _master_out",
        "",
        "echo Master running command: %s" % cmd,
        cmd,
        "",
        ])
    f = open(fname, 'w')
    f.write(txt)
    f.close()

def gen_run_single_slave(fname="_run_a_slave.sh"):
    # use jobid and procid to ensure slave directories are unique.
    # add ./ into PATH so that PEST can run model files without ./
    # "slave_${SLURM_JOB_ID}_${SLURM_STEP_ID}_${SLURM_PROCID}_${SLURM_LOCALID}_$1"
    slave_name = "slave_${SLURM_JOB_ID}_$1"
    slave_dir = os.path.join(SLAVEDIR, slave_name)
    cmd = "%s %s /h ${MASTERHOST}:%s" % (BEOPEST, PST_NAME, PORT)
    lines = [
        "#!/bin/bash",
        "",
        "echo running %s $1 in %s..." % (fname, slave_dir),
        "",
        "sleep 5",
        "mkdir -pv %s" % slave_dir,
        # "cp * %s/" % slave_dir,
        "cp %s %s/" % (FILE_LIST, slave_dir),
        "cd %s" % slave_dir,
        "echo A slave working at $(hostname):%s" % slave_dir,
        "echo %s > _master_dir" % MAIN_DIR, # needed for model scripts to copy incon/save
        "echo ${SLURM_JOB_ID}_$1 > _procid", # needed for obsreref book keeping
        "MASTERHOST=`awk '{print $1}' %s/_master_host`" % MAIN_DIR,
        "export PATH=./:$PATH",
        "echo Slave $(hostname)_${1} running command: %s" % cmd,
        # "sysctl fs.file-nr",
        # "chmod a+x model.bat",
        # "chmod a+x r_model.bat",
        # "chmod a+x d_model.bat",
        cmd,
        "",]
    if SUMMARY_LARGE_FILES:
        lines += [
            "# head and tail big result files",
            "for f in %s" % SUMMARY_LARGE_FILES,
            "do",
            "  head --lines=20 $f > $f.head",
            "  tail --lines=20 $f > $f.tail",
            "  echo Removing $f...",
            "  rm $f",
            "done",
            "",]
    if KEEP_TARGZ_SLAVES:
        # lines += [
        #     "cd ..",
        #     "",]
        lines += [
            "tar -zcf %s/%s.tar.gz %s" % (MAIN_DIR, slave_name, slave_dir),
            "",]
    with open(fname, 'w') as f:
        f.write("\n".join(lines))

def gen_test_dir(fname="_copy_to_test.sh"):
    # make a test directory, used for manually checking slave directory
    slave_dir = "%s/test" % MAIN_DIR
    txt = "\n".join([
        "#!/bin/bash",
        "",
        "mkdir -pv %s" % slave_dir,
        "cp %s %s/" % (FILE_LIST, slave_dir),
        ])
    f = open(fname, 'w')
    f.write(txt)
    f.close()

def sbatch_check(cmd, retry_sec=None, retry_limit=10):
    """ this runs sbatch and detects errors, only return slurm job id if
    successful.  If retry is not None, sbatch will be called again after
    sleeping retry seconds, with a limit number of tries.
    """
    import sys
    if retry_sec is None:
        retry_limit = 0
    i = 0
    jobid = None
    while i < (retry_limit + 1):
        print("running %s ..." % cmd)
        out = check_output(cmd)
        i += 1
        try:
            jobid = out.strip().split()[3]
            break
        except IndexError:
            print("sbatch error, try no. %i: %s" % (i, out.strip()))
        except:
            print("sbatch unexpected error:", sys.exc_info()[0])
            raise
        #if 'error' in out:
        #    print("%i: %s" % (i, out.strip()))
        #else:
        #    jobid = out.strip().split()[3]
        #    break
    return jobid

def proc_args():
    # couldn't use argparse because NeSI Python still default to 2.6
    import sys
    def get_opt(o):
        for i,s in enumerate(sys.argv[1:]):
            if s == o:
                return sys.argv[1:][i+1]
    def usage():
        print('\n'.join([
            "This script submits BeoPEST/PEST_HP job on NeSI.",
            "  -f, --forward COMMAND",
            "      Submit single forward run, COMMAND must be supplied.",
            "      Optional, if not supplied the whole PEST master/slaves run",
            "      will be submitted instead",
            "  -f2, --forward2 COMMAND",
            "      This is the same as -f/--forward.  But instead using the",
            "      less reliable 'sbatch --wait', now it uses 'swait'.",
            "  -f3, --forward3 COMMAND",
            "      This is the same as -f/--forward.  But file locking",
            "      mechanism instead of swait/sbatch that replies on good",
            "      communication",
            "  -f3mahuika, --forward3mahuika COMMAND",
            "      This is the same as -f3/--forward3, but submit to Mahuika",
            "      instead.",
            "  -f3maui, --forward3maui COMMAND",
            "      This is the same as -f3/--forward3, but submit to Maui",
            "      instead.",
            "  -f3x, --forward3x COMMAND",
            "      This is the same as -f3/--forward3, but submission to",
            "      Maui or Mahuika depends on settings in goPESTconfig.toml",
            "  --dirs DIR_PATTERNi --jobnowait COMMAND",
            "      Used to submit many jobs of COMMAND in directories specified",
            "      by DIR_PATTERN",
            "  --cancel",
            "      Cancel ALL jobs originated from current directory (_jobs).",
            ]))
    option = {
        "forward": None,
        "forward2": None,
        "forward3": None,
        "forward3mahuika": None,
        "forward3maui": None,
        "forward3x": None,
        "dirs": None,
        "jobnowait": None,
        "cancel": False,
    }
    if '--help' in sys.argv[1:]:
        usage()
    elif '-h' in sys.argv[1:]:
        usage()
    elif '--forward' in sys.argv[1:]:
        option['forward'] = get_opt('--forward')
    elif '-f' in sys.argv[1:]:
        option['forward'] = get_opt('-f')
    elif '--forward2' in sys.argv[1:]:
        option['forward2'] = get_opt('--forward2')
    elif '-f2' in sys.argv[1:]:
        option['forward2'] = get_opt('-f2')
    elif '--forward3' in sys.argv[1:]:
        option['forward3'] = get_opt('--forward3')
    elif '-f3' in sys.argv[1:]:
        option['forward3'] = get_opt('-f3')
    elif '--forward3mahuika' in sys.argv[1:]:
        option['forward3mahuika'] = get_opt('--forward3mahuika')
    elif '-f3mahuika' in sys.argv[1:]:
        option['forward3mahuika'] = get_opt('-f3mahuika')

    elif '--forward3maui' in sys.argv[1:]:
        option['forward3maui'] = get_opt('--forward3maui')
    elif '-f3maui' in sys.argv[1:]:
        option['forward3maui'] = get_opt('-f3maui')
    elif '--forward3x' in sys.argv[1:]:
        option['forward3x'] = get_opt('--forward3x')
    elif '-f3x' in sys.argv[1:]:
        option['forward3x'] = get_opt('-f3x')
    elif '--dirs' in sys.argv[1:]:
        option['dirs'] = get_opt('--dirs')
        option['jobnowait'] = get_opt('--jobnowait')
    elif '--cancel' in sys.argv[1:]:
        option['cancel'] = True
    return option

def submit_cli(argv=[]):
    if cfg['mode'] != 'nesi':
        raise Exception('Error! gopest submit can only run with mode = "nesi" ')

    from time import sleep
    # You can use chain jobs to create dependencies between jobs.
    # SLURM has an option -d or "--dependency" that allows to
    # specify that a job is only allowed to start if another job finished.
    #
    # use 'after:' instead of 'afterok:' to start slaves after master STARTED
    # (not after it's done)

    SCRIPT_DIR = os.getcwd()
    option = proc_args()

    ### print basic info
    print("You are working under directory: %s" % SCRIPT_DIR)
    write_to('_pest_dir', PESTDIR)
    write_to('_tough2', SIMULATOR)
    write_to('_master_dir', SCRIPT_DIR)

    # for job management, keeps a list of jobs submitted from this directory
    if not os.path.exists('_jobs'):
        os.makedirs('_jobs')

    ### if forward only submit single run job
    if option['forward'] is not None:
        gen_forward_sl(option['forward'])
        # important to have --wait here to block until job finish
        jobid = sbatch_check("sbatch --wait _forward.sl", retry_sec=30)
        if jobid is not None:
            ttime = check_output("sacct -j %s.0 -o totalcpu -n" % jobid).strip()
            print("\nForward job %s finished after %s" % (jobid, ttime))
        else:
            print("\nFailed to submit _forward.sl")
        exit()
    elif option['forward2'] is not None:
        """ The is the same as -f/--forward, but use the more reliable swait.

        It happened a few times that sbatch --wait returned early with error:
        sbatch: error: slurm_receive_msg: Socket timed out on send/recv
        operation. This may be triggered when slurm daemon is acutely busy.  The
        actual job in this case can still be running.  So Gene (from NeSI)
        suggested using swait script.
        """
        gen_forward_sl(option['forward2'])
        jobid = check_output("sbatch _forward.sl").strip().split()[3]
        print("\nJob %s submitted." % jobid)
        print("/share/bin/swait %s" % jobid)
        os.system("/share/bin/swait %s" % jobid)
        ttime = check_output("sacct -j %s.0 -o totalcpu -n" % jobid).strip()
        print("\nForward job %s finished after %s" % (jobid, ttime))
        exit()
    elif option['forward3'] is not None:
        """ The is the same as -f/--forward, but use lock files instead of
        swait or sbatch --wait, which replies on NeSI's communications.
        """
        gen_forward_sl(option['forward3'])
        # _status_on_nesi will be removed once the _forward.sl finish
        # regardless how it terminated.
        with open('_status_on_nesi', 'w') as f:
            pass
        import random
        wait_t = random.random() # * 20.0 * 60.0
        print('.. waiting %f sec before submit ..' % wait_t)
        sleep(wait_t)
        jobid = sbatch_check("sbatch _forward.sl", retry_sec=30, retry_limit=50)
        if jobid is not None:
            while os.path.isfile('_status_on_nesi'):
                sleep(120)
            ttime = check_output("sacct -j %s.0 -o totalcpu -n" % jobid).strip()
            print("\nForward job %s finished after %s" % (jobid, ttime))
        else:
            os.remove('_status_on_nesi')
            print("\nFailed to submit _forward.sl")
        exit()
    elif option['forward3x'] is not None:
        """ The is the same as -f/--forward, but use lock files instead of
        swait or sbatch --wait, which replies on NeSI's communications.
        """
        if cfg['nesi']['cluster_forward'] == 'mahuika':
            cmd = cfg['nesi']['mahuika']['executable'] + ' ' + option['forward3x']
            print('submit_beopest.py runs command (mahuika)' + cmd)
            gen_forward_mahuika_sl(cmd)
        elif cfg['nesi']['cluster_forward'] == 'maui':
            cmd = cfg['nesi']['maui']['executable'] + ' ' + option['forward3x']
            print('submit_beopest.py runs command (maui)' + cmd)
            gen_forward_maui_sl(cmd)
        else:
            raise Exception('only supports mahuika or maui')
        # _status_on_nesi will be removed once the _forward.sl finish
        # regardless how it terminated.
        with open('_status_on_nesi', 'w') as f:
            pass
        import random
        wait_t = random.random() * 5.0 # * 20.0 * 60.0
        print('.. waiting %f sec before submit ..' % wait_t)
        sleep(wait_t)
        jobid = sbatch_check("sbatch _forward.sl", retry_sec=30, retry_limit=50)
        if jobid is not None:
            while os.path.isfile('_status_on_nesi'):
                sleep(30)
            ttime = check_output("sacct --clusters=mahuika -j %s.0 -o totalcpu -n" % jobid).strip()
            print("\nForward job %s finished after %s" % (jobid, ttime))
        else:
            os.remove('_status_on_nesi')
            print("\nFailed to submit _forward.sl")
        exit()
    elif option['forward3mahuika'] is not None:
        """ The is the same as -f/--forward, but use lock files instead of
        swait or sbatch --wait, which replies on NeSI's communications.
        """
        gen_forward_mahuika_sl(option['forward3mahuika'])
        # _status_on_nesi will be removed once the _forward.sl finish
        # regardless how it terminated.
        with open('_status_on_nesi', 'w') as f:
            pass
        import random
        wait_t = random.random() # * 20.0 * 60.0
        print('.. waiting %f sec before submit ..' % wait_t)
        sleep(wait_t)
        jobid = sbatch_check("sbatch _forward.sl", retry_sec=30, retry_limit=50)
        if jobid is not None:
            while os.path.isfile('_status_on_nesi'):
                sleep(120)
            ttime = check_output("sacct --clusters=mahuika -j %s.0 -o totalcpu -n" % jobid).strip()
            print("\nForward job %s finished after %s" % (jobid, ttime))
        else:
            os.remove('_status_on_nesi')
            print("\nFailed to submit _forward.sl")
        exit()
    elif option['forward3maui'] is not None:
        """ The is the same as -f/--forward, but use lock files instead of
        swait or sbatch --wait, which replies on NeSI's communications.
        """
        gen_forward_maui_sl(option['forward3maui'])
        # _status_on_nesi will be removed once the _forward.sl finish
        # regardless how it terminated.
        with open('_status_on_nesi', 'w') as f:
            pass
        import random
        wait_t = random.random() # * 20.0 * 60.0
        print('.. waiting %f sec before submit ..' % wait_t)
        sleep(wait_t)
        jobid = sbatch_check("sbatch _forward.sl", retry_sec=30, retry_limit=50)
        if jobid is not None:
            while os.path.isfile('_status_on_nesi'):
                sleep(120)
            ttime = check_output("sacct --clusters=maui -j %s.0 -o totalcpu -n" % jobid).strip()
            print("\nForward job %s finished after %s" % (jobid, ttime))
        else:
            os.remove('_status_on_nesi')
            print("\nFailed to submit _forward.sl")
        exit()
    elif option['dirs'] is not None and option['jobnowait'] is not None:
        import glob
        import shutil
        write_to('_submit.out', '\n'.join([
            'Running --dirs "%s" --jobnowait "%s"' % (option['dirs'], option['jobnowait']),
            '',
            ]))
        for d in glob.glob(option['dirs']):
            shutil.copy('_master_dir', d)
            shutil.copy('_tough2', d)
            cwd = os.getcwd()
            os.chdir(d)
            write_to('_master_out', cwd+'/_submit.out')
            gen_forward_sl(option['jobnowait'])
            import random
            wait_t = random.random() * 20.0 * 60.0
            print('.. waiting %f sec before submit ..' % wait_t)
            sleep(wait_t)
            jobid = sbatch_check("sbatch _forward.sl", retry_sec=30, retry_limit=50)
            os.chdir(cwd)
        exit()
    elif option['cancel'] is True:
        import glob
        for f in glob.glob('_jobs/*'):
            print('Cancelling %s' % os.path.basename(f))
            os.system('scancel --clusters=maui,mahuika %s' % os.path.basename(f))
        exit()

    ### generate slurm scripts etc.
    gen_master_sl()
    gen_slaves_sl()
    gen_run_master()
    gen_run_single_slave()
    gen_test_dir()

    # slave files and dir will be handled within _run_a_slave.sh

    ### submit beopest master job, and get job id, master.sl will record hostname
    out = check_output("sbatch _master_job.sl").strip()
    print("BeoPEST/PEST_HP Master and Slaves: ", out)
    dependency = out.split()[3]

    write_to('_master_slurm_id', dependency)

    ### submit beopest slaves job, depend on run after master started
    cmd = "sbatch --dependency after:%s _slaves_job.sl" % dependency
    #out = check_output(cmd).strip()
    #print("BeoPEST Slaves: ", out)
    print("Add more slaves (after master job is submitted) by using command:")
    print("    %s" % cmd)

if __name__ == "__main__":
    submit_cli()


from flask_app.config.mysqlconnection import connectToMySQL
from flask_app.models import book

class Author:
    def __init__( self , data ):
        self.id = data['id']
        self.name = data['name']
        self.created_at = data['created_at']
        self.updated_at = data['updated_at']
        self.favorite_books = []

    @classmethod
    def get_all(cls):
        query = f"""
            SELECT *
            FROM authors;
        """
        results = cls.run_query(query)
        items = []
        for row in results:
            items.append( cls(row) )
        return items

    @classmethod
    def get_by_id(cls, id):
        query = f"""
            SELECT *
            FROM authors
            WHERE id=%(id)s;
        """
        data = { "id": id }
        results = cls.run_query(query, data)
        item = cls(results[0])
        return item

    @classmethod
    def get_authors_fav_books(cls, id):
        query = f"""
            SELECT authors.*, book_results.*
            FROM  ( SELECT favorites.*, books.*
                    FROM books
                    LEFT JOIN favorites on books.id = favorites.book_id
                    WHERE author_id = %(id)s

                    UNION

                    SELECT null as "author_id", null as "book_id", books.*
                    FROM books
                    WHERE books.id not in ( SELECT book_id
                                            FROM favorites
                                            WHERE author_id = %(id)s)
                    ) as book_results
            LEFT JOIN authors on authors.id = book_results.author_id
            union
            select authors.*, null as "author_id", null as "book_id", null as "book_results.id", null as "title", null as "num_of_pages", null as "book_results.created_at", null as "book_results.updated_at"
            from authors
            where authors.id = %(id)s and authors.id not in (select author_id from favorites);
        """

        data = { "id": id }
        results = cls.run_query(query, data)

        not_favorited_books = []

        if not results[0]["id"] == None:
            author = cls(results[0])
        else:
            for row_id in range(0, len(results)):
                if not results[row_id]["id"] == None:
                    author = cls(results[row_id])
                    break

        for row in results:
            if row['.id'] is None:
                continue
            book_data = {
                'id': row['.id'],
                'title': row['title'],
                'num_of_pages': row['num_of_pages'],
                'created_at': row['.created_at'],
                'updated_at': row['.updated_at']
            }

            if row['id'] == data['id']:
                author.favorite_books.append( book.Book(book_data) )
            else:
                not_favorited_books.append( book.Book(book_data) )

        return (author, not_favorited_books)

    @classmethod
    def save(cls, data ):
        query = f"""
            INSERT INTO authors ( name, created_at , updated_at )
            VALUES ( %(name)s , NOW() , NOW() );
        """
        return cls.run_query(query, data)

    @classmethod
    def run_query(cls, query, data=None):
        return connectToMySQL('books').query_db( query, data )


import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
import django
django.setup()
from SiyApp.models import Team,Player,Tournament,Fixture,Result

def getDataFromCsv(path,entity):
        path = path + "\\dbo." + entity + ".csv"
        fd = open(path)
        i = 0
        List = []

        if (entity == 'Teams'):
                for line in fd:
                        if(i != 0):
                                elems = line.split(',')
                                tournament      = Tournament(name="Default",maxStages=5,maxTeams=16,maxGames=32)
                                nameTrimed      = elems[1].split('"')[1]
                                kasiTrimed      = elems[2].split('"')[1]
                                List.append(Team(name = nameTrimed,kasi = kasiTrimed))
                                team = Team.objects.get_or_create(tournament = tournament,name = nameTrimed,kasi = kasiTrimed)
                        i = i + 1
        if (entity == 'Players'):
                for line in fd:
                        if(i != 0):
                                elems   = line.split(',')
                                ageInt  = int(elems[2].split('"')[1])
                                player  = Player.objects.get_or_create(name = elems[1],age = ageInt, position = elems[3])
                                List.append(Player(name = elems[1],age = ageInt,position = elems[3]))
                        i = i + 1
        if (entity == 'Tournaments'):
                for line in fd:
                        if(i != 0):
                                elems           = line.split(',')
                                maxGamesInt     = int(elems[1].split('"')[1])
                                maxTeamsInt     = int(elems[2].split('"')[1])
                                maxStagesInt    = int(elems[3].split('"')[1])
                                nameTrimed      = elems[4].split('"')[1]
                                List.append(Tournament(name = nameTrimed,maxStages = maxStagesInt,maxGames = maxGamesInt,maxTeams = maxTeamsInt))
                                tournament      = Tournament.objects.get_or_create(name = nameTrimed,maxStages = maxStagesInt,maxGames = maxGamesInt,maxTeams = maxTeamsInt)

                        i = i + 1
        if (entity == 'Fixtures'):
                for line in fd:
                        if(i != 0):
                                elems = line.split(',')
                                List.append(Fixture(stage = elems[1],homeTeam = elems[2],
                                awayTeam = elems[3],date = elems[4],pitch = elems[5]))
                                fixture = Fixture.objects.get_or_create(stage = elems[1],homeTeam = elems[2],
                                awayTeam = elems[3],date = elems[4],pitch = elems[5])

                        i = i + 1
        if (entity == 'Results'):
                for line in fd:
                        if(i != 0):
                                elems = line.split(',')
                                homeGoalsInt = int(elems[1].split('"')[1])
                                awayGoalsInt = int(elems[2].split('"')[1])
                                List.append(Result(homeGoals = homeGoalsInt,awayGoals = awayGoalsInt))
                                result = Result.objects.get_or_create(homeGoals = homeGoalsInt,awayGoals = awayGoalsInt)

                        i = i + 1

        return List

if __name__ == '__main__':
    print("Population Script Running")
    enties = {"Teams","Players","Tournaments","Results","Fixtures"}
    for entity in enties:
                getDataFromCsv("C:\\Users\\Siya\\Documents\\ProOnePalCSV","Teams")
    print("Population Complete!!")


from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances_argmin

def find_clusters(X, n_clusters, rseed=2):
    rng = np.random.RandomState(rseed)
    i = rng.permutation(X.shape[0])[:n_clusters]
    centers = X[i]

    while True:
        labels = pairwise_distances_argmin(X, centers)
        new_centers = np.array([X[labels==i].mean(axis=0) for i in range(n_clusters)])
        if np.all(centers == new_centers):
            break
        centers = new_centers

    sum_squad_err = sum([np.linalg.norm(centers[i] - j) ** 2 for i in range(n_clusters) for j in X[labels==i]])
    print(f"{sum_squad_err} - средняя квадратичная ошибка в моей для числа классов {n_clusters}")
    return centers, labels, sum_squad_err

def replaceWithCentroid(labels, centers):
    new_pixels = []
    for label in labels:
        pixel_as_centroid = list(centers[label])
        new_pixels.append(pixel_as_centroid)
    new_pixels = np.array(new_pixels).reshape(*ori_img.size, -1)
    return new_pixels

def plotImage(img_array, size):
    return np.array(img_array / 255).reshape(*size)

ori_img = Image.open("lena.png")
X = np.array(ori_img.getdata())
n_clusters = 2
kmeans_my = find_clusters(X, n_clusters)
kmeans_sk = KMeans(n_clusters=n_clusters).fit(X)
print(f"{kmeans_sk.inertia_}  - средняя квадратичная ошибка в sklearn для числа классов {n_clusters}")
labels_my = kmeans_my[1]
centers_my = kmeans_my[0]
new_pixels_my = replaceWithCentroid(labels_my, centers_my)
labels_sk = kmeans_sk.labels_
centers_sk = kmeans_sk.cluster_centers_
new_pixels_sk = replaceWithCentroid(labels_sk, centers_sk)

fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
my = plotImage(new_pixels_my, new_pixels_my.shape)
ax1.imshow(my)
ax1.set_title('My')
ax1.axis("off")
sk = plotImage(new_pixels_sk, new_pixels_sk.shape)
ax2.imshow(sk)
ax2.set_title('Sklearn')
ax2.axis("off")
orig = X.reshape(*ori_img.size, -1)
ax3.imshow(orig)
ax3.set_title('Original')
ax3.axis("off")
plt.show()