from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys
from AssignmentCategoryDict import AssignmentCategoryDict
from Assignment import Assignment
import uuid

class EditCategories(object):

    def __init__(self, course, reload_gradesheet):

        col_headers = ['Category Name', 'Drop Count']

        self.ECategories = QtWidgets.QDialog()
        self.ui = uic.loadUi('../assets/ui/EditCategories.ui', self.ECategories)
        self.ECategories.categoryTable.setHorizontalHeaderLabels(col_headers)

        self.course = course
        self.ECategories.show()
        self.category_uuids = []
        self.setup_display()
        self.reload_gradesheet = reload_gradesheet

        self.original_row_count = self.ECategories.categoryTable.rowCount()

        self.ECategories.removeSelectedCategoryButton.clicked.connect(self.remove_category)
        self.ECategories.addCategoryButton.clicked.connect(self.add_category)
        self.ECategories.saveCategoriesButton.clicked.connect(self.save_table_data)

    def setup_display(self):
        for category in self.course.assignment_category_dict.assignment_categories.values():
            row_insert = self.ECategories.categoryTable.rowCount()
            self.add_category()
            self.ECategories.categoryTable.setItem(row_insert, 0, QtWidgets.QTableWidgetItem(category.categoryName))
            self.ECategories.categoryTable.setItem(row_insert, 1, QtWidgets.QTableWidgetItem(category.drop_count))
            self.category_uuids.append(category.category_uuid)

    def add_category(self):
        row_insert = self.ECategories.categoryTable.rowCount()
        self.ECategories.categoryTable.insertRow(self.ECategories.categoryTable.rowCount())
        self.ECategories.categoryTable.setItem(row_insert, 0, QtWidgets.QTableWidgetItem(""))
        self.ECategories.categoryTable.setItem(row_insert, 1, QtWidgets.QTableWidgetItem(""))

    def remove_category(self):
        if self.ECategories.categoryTable.rowCount() <= 0:
            return
        row = self.ECategories.categoryTable.currentRow()
        if row > self.original_row_count:
            self.ECategories.categoryTable.removeRow(row)
        else:
            choice = QtWidgets.QMessageBox.question(self.ECategories, "Warning",
                                                    "You are about to delete one of your original categories.  Continue?",
                                                    QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
            if choice == QtWidgets.QMessageBox.Yes:
                cat_to_delete_uuid = self.category_uuids[row]
                self.course.assignment_category_dict.delete_category(self.course, cat_to_delete_uuid)
                self.original_row_count = self.original_row_count - 1
                del self.category_uuids[row]
                self.ECategories.categoryTable.removeRow(row)
                self.reload_gradesheet()

    def save_table_data(self):
        row_count = self.ECategories.categoryTable.rowCount()
        output = []
        for row in range(0, row_count):
            cat_name = self.ECategories.categoryTable.item(row, 0).text()
            cat_drop_count = self.ECategories.categoryTable.item(row, 1).text()
            output.append([cat_name, cat_drop_count])

        valid = self.error_checking(output)

        if valid:
            self.course.assignment_category_dict.reload_categories()
            for i in range(len(output)):
                if i < self.original_row_count:
                    self.course.assignment_category_dict.save_category_info(output[i][0], output[i][1], self.category_uuids[i])
                    # Add the database update function
                else:
                    self.course.assignment_category_dict.add_category(str(uuid.uuid4()), output[i][0], output[i][1], self.course.student_list)
                    # Add the database create function
            self.reload_gradesheet()

    def error_checking(self, user_input):

        category_names = [user_input[i][0] for i in range(len(user_input))]
        category_drop_counts = [user_input[i][1] for i in range(len(user_input))]

        for i in category_names:
            if i == "":
                self.bad_input('Error', 'Please enter a category name for all categories')
                return False

        for i in category_drop_counts:

            if "." in i:
                return False

            try:
                x = int(i.strip())
                if x < 0:
                    return False
            except ValueError:
                self.bad_input('Error', 'You have a drop count that is a nonnegative integer.  Please try again.')
                return False

        return True
    """
        Function for telling the user they entered bad input
        Parameters:
            window_text: (string) the name of the window
            error_message: (string) the error message that is displayed to the user
    """

    def bad_input(self, window_text, error_message):
        choice = QtWidgets.QMessageBox.question(self.ECategories, window_text, error_message,
                                                QtWidgets.QMessageBox.Cancel)
        if choice:
            pass


from pyecharts import options as opts
from typing import Any,Optional
from pyecharts.charts import Radar
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from easy_pyechart import constants,baseParams,radar_base_config,round_radar_base_config

class eRadar():
    def __init__(
        self,
        lableList:Optional[list] = [],
        valueList:Optional[list] = [],
    ):
        self.opts: dict = {
                           "lengend":Radar,
                           "lableList":lableList,
                           "valueList":valueList,
                           }

    #基本雷达图
    def basic_radar_chart(self,baseParams):
        self.opts.update(baseParams.opts)
        return radar_base_config(self)

    #单选模式
    def radar_selected_mode(self,baseParams):
        self.opts.update(baseParams.opts)
        c=radar_base_config(self)
        c.set_global_opts(
                legend_opts=opts.LegendOpts(selected_mode="single"),
                title_opts=opts.TitleOpts(title=self.opts['title'],subtitle=self.opts['subTitle'],))
        return c

    #
    def radar_air_quality(self,baseParams):
        self.opts.update(baseParams.opts)
        return radar_base_config(self)

    #设置带有阴影区域的雷达图
    def radar_angle_radius_axis(self,baseParams):
        self.opts.update(baseParams.opts)
        return     round_radar_base_config(self)


import flask
import grpc
import search_pb2_grpc as pb2_grpc
import search_pb2 as pb2
import redis
import json
from google.protobuf.json_format import MessageToJson
from flask import request, jsonify

app = flask.Flask(__name__)
app.config["DEBUG"] = True

class SearchClient(object):
    """
    Client for gRPC functionality
    """

    def __init__(self):
        self.host = 'localhost'
        self.server_port = 50051

        self.channel = grpc.insecure_channel(
            '{}:{}'.format(self.host, self.server_port))

        self.stub = pb2_grpc.SearchStub(self.channel)

    def get_results(self, message):
        """
        Client function to call the rpc for GetServerResponse
        """
        message = pb2.Message(message=message)
        print(f'{message}')
        return self.stub.GetServerResponse(message)

@app.route('/inventory/search', methods=['GET'])
def busqueda():
    if 'q' in request.args:
        busqueda= request.args['q']

        r= redis.Redis(host='localhost', port=6379, db=0)
        resultado = (r.get(busqueda))

        if(resultado!=None):
            products= json.loads(resultado)
            return jsonify(products)

        else:
            client = SearchClient()
            result = client.get_results(busqueda)
            print(result.product[0].name + "*******")
            serialized = MessageToJson(result)
            r.set(busqueda, serialized)
            return serialized
    else:
        return "Error, porfavor especifique la busqueda a realizar"

app.run()


'''
Created on 16/ago/2011

@author: Marco
'''
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from math import sqrt
import ModelsCache
import Configuration

class PdfStructure(object):
    '''
    classdocs
    '''

    __markerList = []
    __modelsCache = ModelsCache.ModelsCache()

    @staticmethod
    def AddMarker(digitalMarker):
        PdfStructure.__markerList.append(digitalMarker)

    @staticmethod
    def RemoveMarker(tagName):
        for tag in PdfStructure.__markerList:
            if tag.name == tagName:
                PdfStructure.__markerList.remove(tag)

    @staticmethod
    def GeneratePDF(fileName):
        c = canvas.Canvas(fileName);
        for digitalMarker in PdfStructure.__markerList:
            inputFile = open(Configuration.TAG_DIR()+digitalMarker.name+".txt","r")
            tagDefinition = inputFile.read()

            lines = tagDefinition.split("\n")

            (x,y) = digitalMarker.GetCenter();
            tX = (float(x)/424)*21
            tY = (float(y)/600)*29.7

            for line in lines:
                ellipse = line.split(" ")
                if len(ellipse) == 10:
                    xCenter = -1*float(ellipse[3])
                    xCenter = (float(xCenter)/digitalMarker.defaultSize)*digitalMarker.size

                    yCenter = -1*float(ellipse[6])
                    yCenter = (float(yCenter)/digitalMarker.defaultSize)*digitalMarker.size

                    radius = ((0.5*sqrt((float(ellipse[3])*2)*(float(ellipse[3])*2)+(float(ellipse[6])*2)*(float(ellipse[6])*2)-4*float(ellipse[9])))/224)*digitalMarker.size
                    c.circle(xCenter/10*cm+tX*cm, yCenter/10*cm+tY*cm, radius/10*cm, fill=True)

        c.save()

    @staticmethod
    def SaveModel(modelName):
        out_file = open(Configuration.MODEL_DIR()+modelName+".model","a")
        if not modelName:
            raise Exception("ERROR: name is empty")
        if not PdfStructure.__markerList:
            raise Exception("ERROR: nothing to save as model")
        for model in PdfStructure.__modelsCache.models:
            if modelName == model.name:
                raise Exception("ERROR: duplicated name")

        model_names_file = open("ModelNames","a")
        model_names_file.write(modelName+"\n")
        model_names_file.close()

        runeNames = []
        runePositions = []
        runeSizes = []
        runeDefaultSizes = []

        for rune in PdfStructure.__markerList:
            runeNames.append(rune.name)
            runePositions.append((rune.x, rune.y))
            runeSizes.append(rune.size)
            runeDefaultSizes.append(rune.defaultSize)
            out_file.write(rune.name+" "+str(rune.x)+" "+str(rune.y)+" "+str(rune.size)+" "+str(rune.defaultSize)+"\n")
        out_file.close()

        PdfStructure.__modelsCache.AddModel(modelName, runeNames, runePositions, runeSizes, runeDefaultSizes)

    @staticmethod
    def GetModelNames():
        modelNames = []
        for model in PdfStructure.__modelsCache.models:
            modelNames.append(model.name)
        return modelNames

    @staticmethod
    def GetModel(modelName):
        return PdfStructure.__modelsCache.GetModel(modelName)

    @staticmethod
    def DeleteModel(name):
        model_names_file = open("ModelNames","r")
        modelNames = model_names_file.read()
        model_names_file.close()
        model_names_file = open("ModelNames","w")
        modelNames = modelNames.replace(name, "")
        model_names_file.write(modelNames)
        model_names_file.close()

        for model in PdfStructure.__modelsCache.models:
            if model.name == name:
                PdfStructure.__modelsCache.models.remove(model)


from elasticsearch import Elasticsearch, helpers
import csv
import json
import time

mvar = "clara"

matching_query = { "query_string": {
                    "query": mvar
                    }
                 }

def main():
    #sundesh
    es = Elasticsearch(host = "localhost", port = 9200)
    #anagnwsh arxeiou
    f = open('BX-Books.csv',"r",encoding="utf8")
    reader = csv.DictReader(f)
    #pairnw ws list o,ti paizei mesa se reader
    #lst = list(reader)
    #dhmiourgeia arxeiou ann auto den yparxei
    helpers.bulk(es, reader, index="bx_books_2")

if __name__ == "__main__":
    main()


import re
from collections import Counter, defaultdict
from itertools import combinations
from typing import Dict, List, Tuple, Set

import numpy as np
from helper import load_input

def create_input():
    '''Extract puzzle input and transform'''

    # creates pattern for extracting replcements
    pattern = r"(\w+) => (\w+)"

    # splits puzzle input into replacements and molecule
    replacements, molecule = load_input(day=19).read().strip("\n").split("\n\n")

    # regex and init empty dict of lists
    matches = re.findall(pattern, replacements)
    replacements_dict = defaultdict(list)

    # converts the replacements into dictionary of lists
    for match in matches:
        replacements_dict[match[0]].append(match[1])

    return replacements_dict, molecule

def insert_replacements(start: str, end: str, replacements: List[str]) -> List[str]:
    '''
    Given start & end of molecule and a list of replacements,
    incrementally inserts replacements between start and end to create new molecules.
    Returns this as a list.
    '''
    return [
        start + replacement + end
        for replacement in replacements
    ]

def generate_molecules(replacements_dict: Dict[str, List[str]], molecule: str) -> Set[str]:
    '''
    Given the replacements and starting molecule,
    generates all the possible molecules after replacement,
    and returns as a set.
    '''

    # Prep storage for generated molecules
    generated_molecules = set()

    # loop through each element in starting molecule
    for i, element in enumerate(molecule):

        # extract replacements if a match
        replacement1 = replacements_dict.get(element, None)
        replacement2 = replacements_dict.get(molecule[i:i+2], None)

        # slice the correct end of molecule, dependent on length
        if replacement1:
            end = molecule[i+1:]
        elif replacement2:
            end = molecule[i+2:]
        else:
            continue

        # Updates the generated molecules set with new molecules after replacement
        generated_molecules.update(insert_replacements(
                start = molecule[:i],
                end = end,
                replacements = replacement1 or replacement2)
        )

    return generated_molecules

def part1():
    '''
    How many distinct molecules can be created
    after all the different ways you can do one replacement on the medicine molecule
    '''
    replacements_dict, molecule = create_input()
    return len(generate_molecules(replacements_dict, molecule))

def part2():
    ...

if __name__ == '__main__':
    print(part1())
    print(part2())


import http

import requests
import telegram
from flask import Blueprint, Response, request
from sqlalchemy_utils import create_database, database_exists

from config import BUILD_NUMBER, DATABASE_URL, REBASE_URL, VERSION

from .bot import dispatcher
from .db import db, test_db
from .utils import log

routes = Blueprint('routes', __name__, url_prefix='/')

@routes.get('/health')
def health_check() -> Response:
  try:
    if not database_exists(DATABASE_URL):
      create_database(DATABASE_URL)
    db.create_all()
  except Exception as exc:
    log.exception('Health checking database... %s: %s', 'ERR', exc)

  return {
    'bot': 'up' if dispatcher is not None else 'down',
    'version': f'{VERSION}-{BUILD_NUMBER}',
    'db': 'up' if test_db() else 'down',
  }, http.HTTPStatus.OK

@routes.get('/rebase')
def reset() -> Response:
  if REBASE_URL is None:
    return { 'error': 'No rebase URL provided' }, http.HTTPStatus.INTERNAL_SERVER_ERROR

  return requests.get(
    f'https://api.telegram.org/bot{dispatcher.bot.token}/setWebhook?url={REBASE_URL}'
  ).content

@routes.post('/')
def index() -> Response:
  if dispatcher is None:
    return 'Bot is inactive', http.HTTPStatus.INTERNAL_SERVER_ERROR

  update = telegram.Update.de_json(request.get_json(force=True), dispatcher.bot)
  dispatcher.process_update(update)

  return '', http.HTTPStatus.NO_CONTENT


from django.urls import path
from card import views

urlpatterns = [
    path('create/', views.CreateFlashCardView.as_view(), name="create-flash-card"),
    path('update/<id>/', views.UpdateFlashCardView.as_view(), name="update-flash-card"),
    path('dalete/<id>/', views.DeleteFlashCardView.as_view(), name="delete-flash-card"),
    path('list/<user_id>/', views.ListFlashCardView.as_view(), name="list-user-flash-card"),
]


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

# plots intensity time series for MDRE model
def plotIntensity ():

	# index boundaries for time 3D plot
	nStart = 0
	nEnd   = 10000

	with open("time_series.txt", "r") as file:
		lines = file.readlines()

	time           = []
	intensity      = []
	rho_GS_e_act   = []
	rho_GS_h_act   = []
	rho_GS_e_inact = []
	rho_GS_h_inact = []
	rho_ES_e       = []
	rho_ES_h       = []
	E_real         = []
	E_imag         = []

	for line in lines:
		time.append(float((line.split('	')[0])))
		intensity.append(float((line.split('	')[1])))
		E_real.append(float((line.split('	')[2])))
		E_imag.append(float((line.split('	')[3])))
		rho_GS_e_act.append(float((line.split('	')[6])))
		rho_GS_h_act.append(float((line.split('	')[7])))
		rho_GS_e_inact.append(float((line.split('	')[8])))
		rho_GS_h_inact.append(float((line.split('	')[9])))
		rho_ES_e.append(float((line.split('	')[10])))
		rho_ES_h.append(float((line.split('	')[11])))

	time           = np.array(time)
	intensity      = np.array(intensity)
	E_real         = np.array(E_real)
	E_imag         = np.array(E_imag)
	rho_GS_e_act   = np.array(rho_GS_e_act)
	rho_GS_h_act   = np.array(rho_GS_h_act)
	rho_GS_e_inact = np.array(rho_GS_e_inact)
	rho_GS_h_inact = np.array(rho_GS_h_inact)
	rho_ES_e       = np.array(rho_ES_e)
	rho_ES_h       = np.array(rho_ES_h)

	# calculation of inversion
	inversion_GS_act   = rho_GS_e_act   + rho_GS_h_act   - 1.0
	inversion_GS_inact = rho_GS_e_inact + rho_GS_h_inact - 1.0
	inversion_ES       = rho_ES_e       + rho_ES_h       - 1.0

	fig, (ax1, ax2) = plt.subplots(1, 2) #sharey=True
	ax12 = ax1.twinx()
	fig.set_size_inches(5.9, 3.2)
	plt.rcParams.update({"font.size": 9})
	fig.subplots_adjust(wspace=0.7, top=0.99, bottom=0.22, left=0.08, right=0.99)

	fig.text(0.005, 0.93, "a)")
	ax1.plot(time[nStart:nEnd], intensity[nStart:nEnd], color="crimson")
	ax1.set_xlabel(r"time $t$ / ns", size=9.0)
	ax1.set_ylabel(r"intensity $|E|^2$", color="crimson", size=9.0)
	ax1.set_ylim(np.min(intensity) - 0.1, np.max(intensity) + 0.3)
	ax1.set_xticks([0.0, 5.0, 10.0])
	ax1.set_yticks([0.0, 1.0, 2.0, 3.0])
	ax1.tick_params(axis="x", labelsize=9.0)
	ax1.tick_params(axis="y", colors="crimson", labelsize=9.0)
	ax1.set_zorder(1)
	ax1.set_facecolor("none")

	ax12.plot(time[nStart:nEnd], inversion_GS_act[nStart:nEnd], color="orange", label="GS act")
	ax12.plot(time[nStart:nEnd], inversion_GS_inact[nStart:nEnd], color="gray", linestyle="--", label="GS inact")
	ax12.plot(time[nStart:nEnd], inversion_ES[nStart:nEnd], color="cornflowerblue", label="ES")
	ax12.set_ylabel(r"population inversion" + "\n" + r"$\rho_{m,e}^{(in)act} + \rho_{m,h}^{(in)act} - 1$", size=9.0)
	ax12.set_ylim(-1.075, 1.075)
	ax12.set_yticks([-1.0, 0.0, 1.0])
	ax12.tick_params(axis="y", labelsize=9.0)
	ax12.set_zorder(2)

	ax12.legend(bbox_to_anchor=(0.44, 0.33))

	# ~ fig, ax = plt.subplots()
	# ~ fig.set_size_inches(5.9, 4.8)
	# ~ fig.subplots_adjust(top=0.99, bottom=0.15, left=0.10, right=0.99)

	fig.text(0.575, 0.93, "b)")
	ax2.plot(inversion_GS_act, intensity, color="orange", label="GS act")
	ax2.plot(inversion_GS_inact, intensity, color="gray", linestyle="--", label="GS inact")
	ax2.plot(inversion_ES, intensity, color="cornflowerblue", label="ES")
	ax2.set_xlabel(r"population inversion" + "\n" + r"$\rho_{m,e}^{(in)act} + \rho_{m,h}^{(in)act} - 1$", size=9.0)
	ax2.set_ylabel(r"intensity $|E|^2$", color="crimson", size=9.0)
	ax2.set_xlim(-1.075, 1.075)
	ax2.set_ylim(-0.15, 3.15)
	ax2.set_xticks([-1.0, 0.0, 1.0])
	ax2.set_yticks([0.0, 1.0, 2.0, 3.0])
	ax2.tick_params(axis="x", labelsize=9.0)
	ax2.tick_params(axis="y", colors="crimson", labelsize=9.0)
	ax2.grid(color="lightgray")

	ax2.legend(loc="upper left")

	plt.show()

plotIntensity()


from motor import motor_asyncio
from .model import Guild
import os

class Database:
    def __init__(self, *, letty):
        self.letty = letty
        self.connection = motor_asyncio.AsyncIOMotorClient(os.environ['DB_URL'])
        self.db = db = self.connection[os.environ['DB_NAME']]
        self.guild = db.guilds

    async def get_guild(self, guild_id):
        data = await self.guild.find_one({"_id": guild_id})
        if data != None:
           return Guild(data, self.guild)
        else:
          return await self.register_guild(guild_id)

    async def register_guild(self, guild_id):
        data = {
                "_id": guild_id,
                "config":{"prefix":"lt.","language":"pt_BR"},
                "disable":{"command":[],"channel":[],"role":[],"member":[]}
                }
        await self.guild.insert_one(data)
        return Guild(data, self.guild)


# -*- coding: utf-8 -*
#该程序用于模型测试
import os
import torch
import numpy as np
import torch.nn as nn
from evaluation import HKOEvaluation
from ium_data.bj_iterator import BJIterator

if __name__ == "__main__":

    #最佳的模型
    test_model = torch.load('./checkpoints/trained_model_12000.pkl' )
    test_model.eval()

    test_bj_iter = BJIterator(datetime_set="bj_test_set.txt",sample_mode="sequent",
                           seq_len=15,width=600,height=600,
                           begin_idx=None, end_idx=None)

    for i in range(10):

        frame_data, mask_dat, datetime_batch, _ = test_bj_iter.sample(batch_size=2)

        frame_data = torch.from_numpy(frame_data)
        frame_data = frame_data.permute(1, 2, 0, 3, 4).contiguous()

        test_input = frame_data[:, :, 0:5, :, :].cuda()
        test_label = frame_data[:, :, 5:15, :, :].cuda()

        #通过5帧预测之后的10帧，即预测后面一小时
        output1 = test_model(test_input)
        output2 = test_model(output1)
        output = torch.cat((output1,output2),2)

        test_label = test_label * 80
        output = output * 80

        print('testing dataset {}'.format(i))

        #计算评价指标
        evaluation = HKOEvaluation(seq_len=10, use_central=False)

        test_label = test_label.cpu().detach().numpy().transpose(2, 0, 1, 3, 4)
        output = output.cpu().detach().numpy().transpose(2, 0, 1, 3, 4)

        evaluation.update(test_label, output, mask=None)

    POD, CSI, FAR = evaluation.calculate_stat()

    #将结果写进txt文件
    evaluation.print_stat_readable()
    evaluation.save_txt_readable('./results/test_evaluation.txt')


import docker

class MicroDockerClient:
    def __init__(self, micro_configuration):
       self.client = docker.from_env()
       self.config = micro_configuration

    def pull(self):
        self.client.images.pull(self.config.image_name)

    def run(self):
        self.client.containers.run(
            self.config.image_name,
            ports={F'{self.config.container_port}/tcp':str(self.config.exposed_port)},
            name=self.config.name,
            detach=True)

    def delete(self):
        try:
            ctr = self.client.containers.list(filters={'name':self.config.name})[0]
            ctr.kill()
            ctr.remove()
        except Exception :
            print("No ctr to delete")


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import ensemble

def data_accuracy(predictions, real):
    """
    Check the accuracy of the estimated prices
    """
    # This will be a list, the ith element of this list will be abs(prediction[i] - real[i])/real[i]
    differences = list(map(lambda x: abs(x[0] - x[1]) / x[1], zip(predictions, real)))

    # Find the value for the bottom t percentile and the top t percentile
    f = 0
    t = 90
    percentiles = np.percentile(differences, [f, t])
    differences_filter = []
    for diff in differences:
        # Keep only values in between f and t percentile
        if percentiles[0] < diff < percentiles[1]:
            differences_filter.append(diff)

    print(f"Differences excluding outliers: {np.average(differences_filter)}")

# clf = ensemble.GradientBoostingRegressor(n_estimators = 1100, max_depth = 15, min_samples_split = 9,learning_rate = 0.5, loss = 'squared_error')
# clf = ensemble.GradientBoostingRegressor(n_estimators = 1000, max_depth = 15, min_samples_split = 9, learning_rate = 0.2, loss = 'squared_error')
clf = ensemble.GradientBoostingRegressor(n_estimators = 600, max_depth = 7, min_samples_split = 5, learning_rate = 0.7, loss = 'squared_error')

data = pd.read_csv("PROJECTS/house-prices/HousePriceDataTRAINING.csv")
data.columns = ["long", "lat", "date", "price", "bed"]

# conv_dates = [0 if ("2011" in values or "2012" in values or "2013" in values or "2014" in values or "2015" in values or "2016" in values) else 1 for values in data.date ]

conv_dates = []

for i in range(data.date.size):
    conv_dates.append(abs(int(data.at[i, "date"].split("/")[0]) + int(data.at[i, "date"].split("/")[1])*31 + int(data.at[i, "date"].split("/")[2])*366 - 737691))

data['date'] = conv_dates

labels = data['price']
train1 = data.drop('price', axis=1)

x_train, x_test, y_train, y_test = train_test_split(
    train1, labels, test_size=0.10)

# y_train = list(map(lambda p: np.log2(p), y_train))

clf.fit(x_train, y_train)

# x_pred = list(map(lambda p: 2**p, clf.predict(x_test)))
x_pred = clf.predict(x_test)

# print(clf.get_params())

print(data_accuracy(y_test, x_pred))


# Subgroup enumeration for cyclic, dicyclic, and tricyclic integer groups.
# PM Larsen, 2019
#
# The theory implemented here is described for two-dimensional groups in:
#     Representing and counting the subgroups of the group Z_m x Z_n
#     Mario Hampejs, Nicki Holighaus, László Tóth, and Christoph Wiesmeyr
#     Journal of Numbers, vol. 2014, Article ID 491428
#     http://dx.doi.org./10.1155/2014/491428
#     https://arxiv.org/abs/1211.1797
#
# and for three-dimensional groups in:
#     On the subgroups of finite Abelian groups of rank three
#     Mario Hampejs and László Tóth
#     Annales Univ. Sci. Budapest., Sect. Comp. 39 (2013), 111–124
#     https://arxiv.org/abs/1304.2961

import itertools
import numpy as np
from math import gcd

def get_divisors(n):
    return [i for i in range(1, n + 1) if n % i == 0]

def get_subgroup_elements(orders, H):

    size = 1
    for e, x in zip(np.diag(H), orders):
        if e != 0:
            size *= x // e

    dimension = len(orders)
    indices = np.zeros((size, dimension), dtype=int)
    indices[:, 0] = H[0, 0] * np.arange(size)

    for i, order in enumerate(orders):
        if i > 0 and H[i, i] != 0:
            k = np.prod(orders[:i]) // np.prod(np.diag(H)[:i])
            p = np.arange(size) // k
            for j in range(i + 1):
                indices[:, j] += H[i, j] * p

    return indices % orders

def consistent_first_rows(dimension, dm, ffilter):
    for a in dm:
        H = np.zeros((dimension, dimension), dtype=int)
        H[0, 0] = a
        if ffilter is None or ffilter(H):
            yield a

def solve_linear_congruence(r, a, b, c, s, v):
    for u in range(a + 1):
        if (r // c * u) % a == (r * v * s // (b * c)) % a:
            return u
    raise Exception("u not found")

def enumerate_subgroup_bases(orders, ffilter=None,
                             min_index=1, max_index=float("inf")):
    """Get the subgroup bases of a cyclic/dicyclic/tricyclic integer group.

    Parameters:

    orders: list-like integer object
        Orders of the constituent groups.
        [m] if the group is a cyclic group Zm
        [m, n] if the group is a dicyclic group Zm x Zn
        [m, n, r] if the group is a tricyclic group Zm x Zn x Zr

    ffilter: function, optional
        A boolean filter function. Avoids generation of unwanted subgroups by
        rejecting partial bases.

    Returns iterator object yielding:

    H: integer ndarray
        Subgroup basis.
    """
    dimension = len(orders)
    assert dimension in [1, 2, 3]
    if dimension == 1:
        m = orders[0]
    elif dimension == 2:
        m, n = orders
    else:
        m, n, r = orders
    dm = get_divisors(m)

    if dimension == 1:
        for d in consistent_first_rows(dimension, dm, ffilter):
            group_index = m // d
            if group_index >= min_index and group_index <= max_index:
                yield np.array([[d]])

    elif dimension == 2:
        dn = get_divisors(n)

        for a in consistent_first_rows(dimension, dm, ffilter):
            for b in dn:
                group_index = m * n // (a * b)
                if group_index < min_index or group_index > max_index:
                    continue

                for t in range(gcd(a, n // b)):
                    s = t * a // gcd(a, n // b)

                    H = np.array([[a, 0], [s, b]])
                    if ffilter is None or ffilter(H):
                        yield H

    elif dimension == 3:
        dn = get_divisors(n)
        dr = get_divisors(r)

        for a in consistent_first_rows(dimension, dm, ffilter):
            for b, c in itertools.product(dn, dr):
                group_index = m * n * r // (a * b * c)
                if group_index < min_index or group_index > max_index:
                    continue

                A = gcd(a, n // b)
                B = gcd(b, r // c)
                C = gcd(a, r // c)
                ABC = A * B * C
                X = ABC // gcd(a * r // c, ABC)

                for t in range(A):
                    s = a * t // A

                    H = np.zeros((dimension, dimension), dtype=int)
                    H[0] = [a, 0, 0]
                    H[1] = [s, b, 0]
                    H[2, 2] = r
                    if ffilter is not None and not ffilter(H):
                        continue

                    for w in range(B * gcd(t, X) // X):
                        v = b * X * w // (B * gcd(t, X))
                        u0 = solve_linear_congruence(r, a, b, c, s, v)

                        for z in range(C):
                            u = u0 + a * z // C
                            H = np.array([[a, 0, 0], [s, b, 0], [u, v, c]])
                            if ffilter is None or ffilter(H):
                                yield H

def count_subgroups(orders):
    """Count the number of subgroups of a cyclic/dicyclic/tricyclic integer
    group.

    Parameters:

    orders: list-like integer object
        Orders of the constituent groups.
        [m] if the group is a cyclic group Zm
        [m, n] if the group is a dicyclic group Zm x Zn
        [m, n, r] if the group is a tricyclic group Zm x Zn x Zr

    Returns:

    n: integer
        Subgroup basis.
    """
    def P(n):
        return sum([gcd(k, n) for k in range(1, n + 1)])

    dimension = len(orders)
    assert dimension in [1, 2, 3]
    if dimension == 1:
        m = orders[0]
    elif dimension == 2:
        m, n = orders
    else:
        m, n, r = orders
    dm = get_divisors(m)

    if dimension == 1:
        return len(dm)
    elif dimension == 2:
        dn = get_divisors(n)
        return sum([gcd(a, b) for a in dm for b in dn])
    else:
        dn = get_divisors(n)
        dr = get_divisors(r)

        total = 0
        for a, b, c in itertools.product(dm, dn, dr):
            A = gcd(a, n // b)
            B = gcd(b, r // c)
            C = gcd(a, r // c)
            ABC = A * B * C
            X = ABC // gcd(a * r // c, ABC)
            total += ABC // X**2 * P(X)
        return total


# -*- coding: utf-8 -*-

""" #+begin_org
* *[Summary]* :: A =CmndLib= for providing currents configuration to CS-s.
#+end_org """

####+BEGIN: b:py3:cs:file/dblockControls :classification "cs-u"
""" #+begin_org
* [[elisp:(org-cycle)][| /Control Parameters Of This File/ |]] :: dblk ctrls classifications=cs-u
#+BEGIN_SRC emacs-lisp
(setq-local b:dblockControls t) ; (setq-local b:dblockControls nil)
(put 'b:dblockControls 'py3:cs:Classification "cs-u") ; one of cs-mu, cs-u, cs-lib, bpf-lib, pyLibPure
#+END_SRC
#+RESULTS:
: cs-u
#+end_org """
####+END:

####+BEGIN: b:prog:file/proclamations :outLevel 1
""" #+begin_org
* *[[elisp:(org-cycle)][| Proclamations |]]* :: Libre-Halaal Software --- Part Of BISOS ---  Poly-COMEEGA Format.
** This is Libre-Halaal Software. © Neda Communications, Inc. Subject to AGPL.
** It is part of BISOS (ByStar Internet Services OS)
** Best read and edited  with Blee in Poly-COMEEGA (Polymode Colaborative Org-Mode Enhance Emacs Generalized Authorship)
#+end_org """
####+END:

####+BEGIN: b:prog:file/particulars :authors ("./inserts/authors-mb.org")
""" #+begin_org
* *[[elisp:(org-cycle)][| Particulars |]]* :: Authors, version
** This File: /bisos/git/auth/bxRepos/bisos-pip/currents/py3/bisos/currents/currentsConfig.py
** Authors: Mohsen BANAN, http://mohsen.banan.1.byname.net/contact
#+end_org """
####+END:

####+BEGIN: b:python:file/particulars-csInfo :status "inUse"
""" #+begin_org
* *[[elisp:(org-cycle)][| Particulars-csInfo |]]*
#+end_org """
import typing
csInfo: typing.Dict[str, typing.Any] = { 'moduleName': ['currentsConfig'], }
csInfo['version'] = '202209290819'
csInfo['status']  = 'inUse'
csInfo['panel'] = 'currentsConfig-Panel.org'
csInfo['groupingType'] = 'IcmGroupingType-pkged'
csInfo['cmndParts'] = 'IcmCmndParts[common] IcmCmndParts[param]'
####+END:

""" #+begin_org
* /[[elisp:(org-cycle)][| Description |]]/ :: [[file:/bisos/git/auth/bxRepos/blee-binders/bisos-core/COMEEGA/_nodeBase_/fullUsagePanel-en.org][BISOS COMEEGA Panel]]
Module description comes here.
** Relevant Panels:
** Status: In use with blee3
** /[[elisp:(org-cycle)][| Planned Improvements |]]/ :
*** TODO complete fileName in particulars.
#+end_org """

####+BEGIN: b:prog:file/orgTopControls :outLevel 1
""" #+begin_org
* [[elisp:(org-cycle)][| Controls |]] :: [[elisp:(delete-other-windows)][(1)]] | [[elisp:(show-all)][Show-All]]  [[elisp:(org-shifttab)][Overview]]  [[elisp:(progn (org-shifttab) (org-content))][Content]] | [[elisp:(blee:ppmm:org-mode-toggle)][Nat]] | [[elisp:(bx:org:run-me)][Run]] | [[elisp:(bx:org:run-me-eml)][RunEml]] | [[elisp:(progn (save-buffer) (kill-buffer))][S&Q]]  [[elisp:(save-buffer)][Save]]  [[elisp:(kill-buffer)][Quit]] [[elisp:(org-cycle)][| ]]
** /Version Control/ ::  [[elisp:(call-interactively (quote cvs-update))][cvs-update]]  [[elisp:(vc-update)][vc-update]] | [[elisp:(bx:org:agenda:this-file-otherWin)][Agenda-List]]  [[elisp:(bx:org:todo:this-file-otherWin)][ToDo-List]]
#+end_org """
####+END:

####+BEGIN: b:python:file/workbench :outLevel 1
""" #+begin_org
* [[elisp:(org-cycle)][| Workbench |]] :: [[elisp:(python-check (format "/bisos/venv/py3/bisos3/bin/python -m pyclbr %s" (bx:buf-fname))))][pyclbr]] || [[elisp:(python-check (format "/bisos/venv/py3/bisos3/bin/python -m pydoc ./%s" (bx:buf-fname))))][pydoc]] || [[elisp:(python-check (format "/bisos/pipx/bin/pyflakes %s" (bx:buf-fname)))][pyflakes]] | [[elisp:(python-check (format "/bisos/pipx/bin/pychecker %s" (bx:buf-fname))))][pychecker (executes)]] | [[elisp:(python-check (format "/bisos/pipx/bin/pycodestyle %s" (bx:buf-fname))))][pycodestyle]] | [[elisp:(python-check (format "/bisos/pipx/bin/flake8 %s" (bx:buf-fname))))][flake8]] | [[elisp:(python-check (format "/bisos/pipx/bin/pylint %s" (bx:buf-fname))))][pylint]]  [[elisp:(org-cycle)][| ]]
#+end_org """
####+END:

####+BEGIN: b:py3:cs:orgItem/basic :type "=PyImports= " :title "*Py Library IMPORTS*" :comment "-- with classification based framework/imports"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  =PyImports=  [[elisp:(outline-show-subtree+toggle)][||]] *Py Library IMPORTS* -- with classification based framework/imports  [[elisp:(org-cycle)][| ]]
#+end_org """
####+END:

####+BEGIN: b:py3:cs:framework/imports :basedOn "classification"
""" #+begin_org
** Imports Based On Classification=cs-u
#+end_org """
from bisos import b
from bisos.b import cs
from bisos.b import b_io

import collections
####+END:

import os
import collections
#import enum

import shutil

import sys

####+BEGIN: blee:bxPanel:foldingSection :outLevel 1 :title "Obtain Package Bases"  :extraInfo ""
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*       [[elisp:(outline-show-subtree+toggle)][| *Obtain Package Bases:* |]]    [[elisp:(org-shifttab)][<)]] E|
#+end_org """
####+END:

####+BEGIN: b:py3:cs:func/typing :funcName "configBaseDir_obtain" :deco "track"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-T-       [[elisp:(outline-show-subtree+toggle)][||]] /configBaseDir_obtain/  deco=track  [[elisp:(org-cycle)][| ]]
#+end_org """
@cs.track(fnLoc=True, fnEntry=True, fnExit=True)
def configBaseDir_obtain(
####+END:

) -> str:
    """ #+begin_org
** [[elisp:(org-cycle)][| *DocStr | ]
    #+end_org """

    outcome =  b.subProc.WOpW(invedBy=None, log=0).bash(
        f"""usgBpos.sh -i usgBpos_usageEnvs_fullUse_bxoPath""")

    if outcome.isProblematic():
        b_io.eh.badOutcome(outcome)
        return ""

    retVal = outcome.stdout.rstrip('\n')

    return retVal

####+BEGIN: bx:cs:python:func :funcName "configUsgCursBaseDir_obtain" :funcType "anyOrNone" :retType "bool" :deco "" :argsList "configBaseDir"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /configUsgCursBaseDir_obtain/ retType=bool argsList=(configBaseDir)  [[elisp:(org-cycle)][| ]]
#+end_org """
def configUsgCursBaseDir_obtain(
    configBaseDir,
):
####+END:
    if not configBaseDir:
        configBaseDir = configBaseDir_obtain()

    return os.path.abspath(os.path.join(configBaseDir, "control/currents"))

####+BEGIN: bx:cs:python:func :funcName "configUsgCursFpBaseDir_obtain" :funcType "anyOrNone" :retType "bool" :deco "" :argsList "configBaseDir"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /configUsgCursFpBaseDir_obtain/ retType=bool argsList=(configBaseDir)  [[elisp:(org-cycle)][| ]]
#+end_org """
def configUsgCursFpBaseDir_obtain(
    configBaseDir,
):
####+END:
    if not configBaseDir:
        configBaseDir = configBaseDir_obtain()

    return os.path.abspath(os.path.join(configBaseDir,"control/currents/fp"))

####+BEGIN: blee:bxPanel:foldingSection :outLevel 1 :title "File Parameters Obtain"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*       [[elisp:(outline-show-subtree+toggle)][| *File Parameters Obtain:* |]]    [[elisp:(org-shifttab)][<)]] E|
#+end_org """
####+END:

####+BEGIN: bx:cs:python:func :funcName "bxoId_fpObtain" :comment "Configuration Parameter" :funcType "anyOrNone" :retType "bool" :deco "" :argsList "configBaseDir"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /bxoId_fpObtain/ =Configuration Parameter= retType=bool argsList=(configBaseDir)  [[elisp:(org-cycle)][| ]]
#+end_org """
def bxoId_fpObtain(
    configBaseDir,
):
####+END:
    if not configBaseDir:
        configBaseDir = configBaseDir_obtain()

    return(
        b.fp.FileParamValueReadFrom(
            parRoot= os.path.abspath("{}/usgCurs/fp".format(configBaseDir)),
            parName="bxoId")
    )

####+BEGIN: bx:cs:python:func :funcName "sr_fpObtain" :comment "Configuration Parameter" :funcType "anyOrNone" :retType "bool" :deco "" :argsList "configBaseDir"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /sr_fpObtain/ =Configuration Parameter= retType=bool argsList=(configBaseDir)  [[elisp:(org-cycle)][| ]]
#+end_org """
def sr_fpObtain(
    configBaseDir,
):
####+END:
    if not configBaseDir:
        configBaseDir = configBaseDir_obtain()

    return(
        b.fp.FileParamValueReadFrom(
            parRoot= os.path.abspath("{}/usgCurs/fp".format(configBaseDir)),
            parName="sr")
    )

####+BEGIN: blee:bxPanel:foldingSection :outLevel 1 :title "Common Command Parameter Specification"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*       [[elisp:(outline-show-subtree+toggle)][| *Common Command Parameter Specification:* |]]    [[elisp:(org-shifttab)][<)]] E|
#+end_org """
####+END:

####+BEGIN: bx:cs:python:func :funcName "commonParamsSpecify" :funcType "void" :retType "bool" :deco "" :argsList "csParams"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-void     [[elisp:(outline-show-subtree+toggle)][||]] /commonParamsSpecify/ retType=bool argsList=(csParams)  [[elisp:(org-cycle)][| ]]
#+end_org """
def commonParamsSpecify(
    csParams,
):
####+END:

    csParams.parDictAdd(
        parName='configBaseDir',
        parDescription="Root Of usgCurs/fp from which file parameters will be read",
        parDataType=None,
        parDefault=None,
        parChoices=["any"],
        # parScope=cs.CmndParamScope.TargetParam,
        argparseShortOpt=None,
        argparseLongOpt='--configBaseDir',
    )

    csParams.parDictAdd(
        parName='bxoId',
        parDescription="BISOS Default UserName",
        parDataType=None,
        parDefault=None,
        parChoices=["any"],
        # parScope=cs.CmndParamScope.TargetParam,
        argparseShortOpt=None,
        argparseLongOpt='--bxoId',
    )

    csParams.parDictAdd(
        parName='sr',
        parDescription="BISOS Default GroupName",
        parDataType=None,
        parDefault=None,
        parChoices=["any"],
        # parScope=cs.CmndParamScope.TargetParam,
        argparseShortOpt=None,
        argparseLongOpt='--sr',
    )

####+BEGIN: blee:bxPanel:foldingSection :outLevel 1 :title "Common Command Examples Sections"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*       [[elisp:(outline-show-subtree+toggle)][| *Common Command Examples Sections:* |]]    [[elisp:(org-shifttab)][<)]] E|
#+end_org """
####+END:

####+BEGIN: bx:cs:python:func :funcName "examples_usgCursParsFull" :funcType "anyOrNone" :retType "bool" :deco "" :argsList "configBaseDir"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /examples_usgCursParsFull/ retType=bool argsList=(configBaseDir)  [[elisp:(org-cycle)][| ]]
#+end_org """
def examples_usgCursParsFull(
    configBaseDir,
):
####+END:
    """
** Auxiliary examples to be commonly used.
"""

    def cpsInit(): return collections.OrderedDict()
    def menuItem(verbosity): cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity=verbosity,
                             comment='none', icmWrapper=None, icmName=None) # verbosity: 'little' 'basic' 'none'
    def execLineEx(cmndStr): cs.examples.execInsert(execLine=cmndStr)

    cs.examples.menuChapter(' =FP Values=  *usgCurs Clear InfoBase --- Deletes All FPs*')

    cmndName = "usgCursParsDelete" ; cmndArgs = "" ;
    cps = collections.OrderedDict() ;  cps['configBaseDir'] = configBaseDir
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    cmndName = "usgCursParsDelete" ; cmndArgs = "" ; cps=cpsInit(); menuItem(verbosity='none')

    cmndName = "usgCursParsDelete" ; cmndArgs = "anyName" ;
    cps = collections.OrderedDict() ;
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, icmWrapper="echo", verbosity='little')

    cs.examples.menuChapter(' =FP Values=  *usgCurs Get Parameters*')

    cmndName = "usgCursParsGet" ; cmndArgs = "" ;
    cps = collections.OrderedDict() ;  cps['configBaseDir'] = configBaseDir
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    cmndName = "usgCursParsGet" ; cmndArgs = "" ; cps=cpsInit(); menuItem(verbosity='none')

    cs.examples.menuChapter(' =FP Values=  *UsgCurs Defaults ParsSet  --*')

    cmndName = "usgCursParsDefaultsSet" ; cmndArgs = "bxoPolicy /" ;
    cpsInit(); menuItem('none')

    cmndName = "usgCursParsDefaultsSet" ; cmndArgs = "bxoPolicy /tmp" ;
    cpsInit(); menuItem('none')

    cs.examples.menuChapter(' =FP Values=  *UsgCurs ParsSet -- Set Parameters Explicitly*')

    cmndName = "usgCursParsSet" ; cmndArgs = "" ;
    cps = collections.OrderedDict() ;  cps['bxoId'] = "mcm"
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    cmndName = "usgCursParsSet" ; cmndArgs = "" ;
    cps = collections.OrderedDict() ;  cps['bxoId'] = "ea-59043"
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    cmndName = "usgCursParsSet" ; cmndArgs = "" ;
    cps = collections.OrderedDict() ;  cps['sr'] = "marme/dsnProc"
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    cmndName = "usgCursParsSet" ; cmndArgs = "" ;
    cps = collections.OrderedDict() ;  cps['sr'] = "apache2/plone3"
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    # cmndName = "usgCursParsSet" ; cmndArgs = "" ;
    # cps = collections.OrderedDict() ;  cps['configBaseDir'] = configBaseDir ; cps['platformControlBaseDir'] = "${HOME}/bisosControl"
    # cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    cmndName = "usgCursParsSet" ; cmndArgs = "anyName=anyValue" ;
    cps = collections.OrderedDict() ;
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, verbosity='little')

    cmndName = "usgCursParsSet" ; cmndArgs = "anyName=anyValue" ;
    cps = collections.OrderedDict() ;
    cs.examples.cmndInsert(cmndName, cps, cmndArgs, icmWrapper="echo", verbosity='little')

####+BEGIN: blee:bxPanel:foldingSection :outLevel 1 :title "File Parameters Get/Set -- Commands"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*       [[elisp:(outline-show-subtree+toggle)][| *File Parameters Get/Set -- Commands:* |]]    [[elisp:(org-shifttab)][<)]] E|
#+end_org """
####+END:

####+BEGIN: bx:cs:python:func :funcName "FP_readTreeAtBaseDir_CmndOutput" :funcType "anyOrNone" :retType "bool" :deco "" :argsList "interactive fpBaseDir cmndOutcome"
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /FP_readTreeAtBaseDir_CmndOutput/ retType=bool argsList=(interactive fpBaseDir cmndOutcome)  [[elisp:(org-cycle)][| ]]
#+end_org """
def FP_readTreeAtBaseDir_CmndOutput(
    interactive,
    fpBaseDir,
    cmndOutcome,
):
####+END:
    """Invokes FP_readTreeAtBaseDir.cmnd as interactive-output only."""
    #
    # Interactive-Output + Chained-Outcome Command Invokation
    #
    FP_readTreeAtBaseDir = icm.FP_readTreeAtBaseDir()
    FP_readTreeAtBaseDir.cmndLineInputOverRide = True
    FP_readTreeAtBaseDir.cmndOutcome = cmndOutcome

    return FP_readTreeAtBaseDir.cmnd(
        interactive=interactive,
        FPsDir=fpBaseDir,
    )

####+BEGIN: b:py3:cs:cmnd/classHead :cmndName "usgCursParsDelete" :comment "" :parsMand "" :parsOpt "configBaseDir" :argsMin 0 :argsMax 9999 :pyInv ""
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  CmndSvc-   [[elisp:(outline-show-subtree+toggle)][||]] <<usgCursParsDelete>>  =verify= parsOpt=configBaseDir argsMax=9999 ro=cli   [[elisp:(org-cycle)][| ]]
#+end_org """
class usgCursParsDelete(cs.Cmnd):
    cmndParamsMandatory = [ ]
    cmndParamsOptional = [ 'configBaseDir', ]
    cmndArgsLen = {'Min': 0, 'Max': 9999,}

    @cs.track(fnLoc=True, fnEntry=True, fnExit=True)
    def cmnd(self,
             rtInv: cs.RtInvoker,
             cmndOutcome: b.op.Outcome,
             configBaseDir: typing.Optional[str]=None,  # Cs Optional Param
             argsList: typing.Optional[list[str]]=None,  # CsArgs
    ) -> b.op.Outcome:

        callParamsDict = {'configBaseDir': configBaseDir, }
        if self.invocationValidate(rtInv, cmndOutcome, callParamsDict, argsList).isProblematic():
            return b_io.eh.badOutcome(cmndOutcome)
        cmndArgsSpecDict = self.cmndArgsSpec()
####+END:
        self.cmndDocStr(f""" #+begin_org
** [[elisp:(org-cycle)][| *CmndDesc:* | ]]  Remove The entire infoBaseDir
        #+end_org """)

        if not configBaseDir:
            configBaseDir = configUsgCursFpBaseDir_obtain(None)

        cmndArgs = self.cmndArgsGet("0&-1", cmndArgsSpecDict, argsList)

        if len(cmndArgs) == 0:
            try:
                shutil.rmtree(configBaseDir)
            except OSError as e:
                print(f"Error: {configBaseDir} : {e.strerror}")
            b.dir.createIfNotThere(configBaseDir)

        else:
            for each in cmndArgs:
                parNameFullPath = os.path.join(
                        configBaseDir,
                        each
                )
                try:
                    shutil.rmtree(parNameFullPath)
                except OSError as e:
                    print(f"Error: {parNameFullPath} : {e.strerror}")

        return cmndOutcome

####+BEGIN: b:py3:cs:method/args :methodName "cmndArgsSpec" :methodType "anyOrNone" :retType "bool" :deco "default" :argsList "self"
    """ #+begin_org
**  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  Mtd-T-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /cmndArgsSpec/ deco=default  deco=default  [[elisp:(org-cycle)][| ]]
    #+end_org """
    @cs.track(fnLoc=True, fnEntry=True, fnExit=True)
    def cmndArgsSpec(self, ):
####+END:
        """
***** Cmnd Args Specification
"""
        cmndArgsSpecDict = cs.CmndArgsSpecDict()
        cmndArgsSpecDict.argsDictAdd(
            argPosition="0&-1",
            argName="cmndArgs",
            argDefault=None,
            argChoices='any',
            argDescription="A sequence of parNames"
        )

        return cmndArgsSpecDict

####+BEGIN: b:py3:cs:func/typing :funcName "curParsGetAsDictValue_wOp" :funcType "WOp" :retType "extTyped" :deco "" :argsList ""
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-T-WOp    [[elisp:(outline-show-subtree+toggle)][||]] /curParsGetAsDictValue_wOp/   [[elisp:(org-cycle)][| ]]
#+end_org """
def curParsGetAsDictValue_wOp(
####+END:
        parNamesList: list,
        outcome: b.op.Outcome = None,
) -> b.op.Outcome:
    """ #+begin_org
** [[elisp:(org-cycle)][| *DocStr | ] A Wrapped Operation with results being a dictionary of values.
    if not ~parNamesList~, get all the values.
*** TODO --- NOTYET This needs to be moved to
    #+end_org """

    configBaseDir = configUsgCursFpBaseDir_obtain(None)

    return (
        FP_parsGetAsDictValue_wOp(parNamesList, configBaseDir, outcome)
    )

####+BEGIN: b:py3:cs:func/typing :funcName "FP_parsGetAsDictValue_wOp" :funcType "wOp" :retType "OpOutcome" :deco "" :argsList ""
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  F-T-wOp    [[elisp:(outline-show-subtree+toggle)][||]] /FP_parsGetAsDictValue_wOp/   [[elisp:(org-cycle)][| ]]
#+end_org """
def FP_parsGetAsDictValue_wOp(
####+END:
        parNamesList: list,
        configBaseDir,
        outcome: b.op.Outcome = None,
) -> b.op.Outcome:
    """ #+begin_org
** [[elisp:(org-cycle)][| *DocStr | ] A Wrapped Operation with results being a dictionary of values.
    if not ~parNamesList~, get all the values.
*** TODO --- NOTYET This needs to be moved to
    #+end_org """

    return b.fp.parsGetAsDictValue_wOp(parNamesList, configBaseDir, outcome=outcome)

    if not outcome:
        outcome = b.op.Outcome()

    FP_readTreeAtBaseDir_CmndOutput(
        interactive=False,
        fpBaseDir=configBaseDir,
        cmndOutcome=outcome,
    )

    results = outcome.results

    opResults = dict()
    opErrors = ""

    if parNamesList:
        for each in parNamesList:
            # NOTYET, If no results[each], we need to record it in opErrors
            opResults[each] = results[each].parValueGet()
            #print(f"{each} {eachFpValue}")

    else:
        for eachFpName in results:
            opResults[eachFpName] = results[eachFpName].parValueGet()
            #print(f"{eachFpName} {eachFpValue}")

    return outcome.set(
        opError=b.OpError.Success,
        opResults=opResults,
    )

####+BEGIN: b:py3:cs:cmnd/classHead :cmndName "usgCursParsGetK2" :comment "" :parsMand "" :parsOpt "configBaseDir" :argsMin 0 :argsMax 9999 :pyInv ""
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  CmndSvc-   [[elisp:(outline-show-subtree+toggle)][||]] <<usgCursParsGetK2>>  =verify= parsOpt=configBaseDir argsMax=9999 ro=cli   [[elisp:(org-cycle)][| ]]
#+end_org """
class usgCursParsGetK2(cs.Cmnd):
    cmndParamsMandatory = [ ]
    cmndParamsOptional = [ 'configBaseDir', ]
    cmndArgsLen = {'Min': 0, 'Max': 9999,}

    @cs.track(fnLoc=True, fnEntry=True, fnExit=True)
    def cmnd(self,
             rtInv: cs.RtInvoker,
             cmndOutcome: b.op.Outcome,
             configBaseDir: typing.Optional[str]=None,  # Cs Optional Param
             argsList: typing.Optional[list[str]]=None,  # CsArgs
    ) -> b.op.Outcome:

        callParamsDict = {'configBaseDir': configBaseDir, }
        if self.invocationValidate(rtInv, cmndOutcome, callParamsDict, argsList).isProblematic():
            return b_io.eh.badOutcome(cmndOutcome)
        cmndArgsSpecDict = self.cmndArgsSpec()
####+END:
        self.cmndDocStr(f""" #+begin_org
** [[elisp:(org-cycle)][| *CmndDesc:* | ]]  it reads from ../usgCurs/fp.
        #+end_org """)

        if not configBaseDir:
            configBaseDir = configUsgCursFpBaseDir_obtain(None)

        cmndArgs = self.cmndArgsGet("0&-1", cmndArgsSpecDict, argsList)

        # FP_readTreeAtBaseDir_CmndOutput(
        #     interactive=False,
        #     fpBaseDir=configBaseDir,
        #     cmndOutcome=cmndOutcome,
        # )

        b.fp.readTreeAtBaseDir_wOp(configBaseDir, cmndOutcome=cmndOutcome)

        results = cmndOutcome.results

        if len(cmndArgs) == 0:
            for eachFpName in results:
                eachFpValue = results[eachFpName].parValueGet()
                print(f"{eachFpName} {eachFpValue}")
        else:
            for each in cmndArgs:
                eachFpValue = results[each].parValueGet()
                print(f"{each} {eachFpValue}")

        return cmndOutcome

####+BEGIN: b:py3:cs:cmnd/classHead :cmndName "usgCursParsGet" :comment "" :parsMand "" :parsOpt "configBaseDir" :argsMin 0 :argsMax 9999 :pyInv ""
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  CmndSvc-   [[elisp:(outline-show-subtree+toggle)][||]] <<usgCursParsGet>>  =verify= parsOpt=configBaseDir argsMax=9999 ro=cli   [[elisp:(org-cycle)][| ]]
#+end_org """
class usgCursParsGet(cs.Cmnd):
    cmndParamsMandatory = [ ]
    cmndParamsOptional = [ 'configBaseDir', ]
    cmndArgsLen = {'Min': 0, 'Max': 9999,}

    @cs.track(fnLoc=True, fnEntry=True, fnExit=True)
    def cmnd(self,
             rtInv: cs.RtInvoker,
             cmndOutcome: b.op.Outcome,
             configBaseDir: typing.Optional[str]=None,  # Cs Optional Param
             argsList: typing.Optional[list[str]]=None,  # CsArgs
    ) -> b.op.Outcome:

        callParamsDict = {'configBaseDir': configBaseDir, }
        if self.invocationValidate(rtInv, cmndOutcome, callParamsDict, argsList).isProblematic():
            return b_io.eh.badOutcome(cmndOutcome)
        cmndArgsSpecDict = self.cmndArgsSpec()
####+END:
        self.cmndDocStr(f""" #+begin_org
** [[elisp:(org-cycle)][| *CmndDesc:* | ]]  it reads from ../usgCurs/fp.
        #+end_org """)

        if not configBaseDir:
            configBaseDir = configUsgCursFpBaseDir_obtain(None)

        cmndArgs = self.cmndArgsGet("0&-1", cmndArgsSpecDict, argsList)

        curParsGetAsDictValue_wOp(cmndArgs, cmndOutcome)
        results = cmndOutcome.results

        if rtInv.outs:
            for eachKey in results:
                print(f"{eachKey}: {results[eachKey]}")

        return cmndOutcome

####+BEGIN: b:py3:cs:method/args :methodName "cmndArgsSpec" :methodType "anyOrNone" :retType "bool" :deco "default" :argsList "self"
    """ #+begin_org
**  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  Mtd-T-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /cmndArgsSpec/ deco=default  deco=default  [[elisp:(org-cycle)][| ]]
    #+end_org """
    @cs.track(fnLoc=True, fnEntry=True, fnExit=True)
    def cmndArgsSpec(self, ):
####+END:
        """
***** Cmnd Args Specification
"""
        cmndArgsSpecDict = cs.CmndArgsSpecDict()
        cmndArgsSpecDict.argsDictAdd(
            argPosition="0&-1",
            argName="cmndArgs",
            argDefault=None,
            argChoices='any',
            argDescription="A sequence of parNames"
        )

        return cmndArgsSpecDict

####+BEGIN: b:py3:cs:cmnd/classHead :cmndName "usgCursParsSet" :comment "" :parsMand "" :parsOpt "configBaseDir bxoId sr" :argsMin 0 :argsMax 1000 :pyInv ""
""" #+begin_org
*  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  CmndSvc-   [[elisp:(outline-show-subtree+toggle)][||]] <<usgCursParsSet>>  =verify= parsOpt=configBaseDir bxoId sr argsMax=1000 ro=cli   [[elisp:(org-cycle)][| ]]
#+end_org """
class usgCursParsSet(cs.Cmnd):
    cmndParamsMandatory = [ ]
    cmndParamsOptional = [ 'configBaseDir', 'bxoId', 'sr', ]
    cmndArgsLen = {'Min': 0, 'Max': 1000,}

    @cs.track(fnLoc=True, fnEntry=True, fnExit=True)
    def cmnd(self,
             rtInv: cs.RtInvoker,
             cmndOutcome: b.op.Outcome,
             configBaseDir: typing.Optional[str]=None,  # Cs Optional Param
             bxoId: typing.Optional[str]=None,  # Cs Optional Param
             sr: typing.Optional[str]=None,  # Cs Optional Param
             argsList: typing.Optional[list[str]]=None,  # CsArgs
    ) -> b.op.Outcome:

        callParamsDict = {'configBaseDir': configBaseDir, 'bxoId': bxoId, 'sr': sr, }
        if self.invocationValidate(rtInv, cmndOutcome, callParamsDict, argsList).isProblematic():
            return b_io.eh.badOutcome(cmndOutcome)
        cmndArgsSpecDict = self.cmndArgsSpec()
####+END:
        self.cmndDocStr(f""" #+begin_org
** [[elisp:(org-cycle)][| *CmndDesc:* | ]] Args are in the form of a list of varName=varValue. Well known pars can also be set.
=configBaseDir= defaults to ~configBaseDir_obtain()~
        #+end_org """)

        if not configBaseDir:
            configBaseDir = configBaseDir_obtain()

        cmndArgs = self.cmndArgsGet("0&-1", cmndArgsSpecDict, argsList)

        parNameFullPath = ""

        def createPathAndFpWrite(
                fpPath,
                valuePath,
        ):
            valuePath = os.path.abspath(valuePath)
            try:
                os.makedirs(valuePath)
            except OSError:
                if not os.path.isdir(valuePath):
                    raise

            b.fp.b.fp.FileParamWriteToPath(
                parNameFullPath=fpPath,
                parValue=valuePath,
            )
            parNameFullPath = fpPath

        # Any number of Name=Value can be passed as args
        for each in cmndArgs:
            varNameValue = each.split('=')
            parNameFullPath = os.path.join(
                    configUsgCursFpBaseDir_obtain(configBaseDir=configBaseDir),
                    varNameValue[0],
            )
            b.fp.b.fp.FileParamWriteToPath(
                parNameFullPath=parNameFullPath,

                parValue=varNameValue[1],
            )

        if bxoId:
             parNameFullPath = b.fp.b.fp.FileParamWriteToPath(
                parNameFullPath=os.path.join(
                    configUsgCursFpBaseDir_obtain(configBaseDir=configBaseDir),
                    "bxoId",
                ),
                parValue=bxoId,
            )

        if sr:
             parNameFullPath = b.fp.b.fp.FileParamWriteToPath(
                parNameFullPath=os.path.join(configUsgCursFpBaseDir_obtain(configBaseDir=configBaseDir),
                             "sr",
                ),
                parValue=sr,
            )

        if rtInv.outs:
            parValue = b.fp.FileParamValueReadFromPath(parNameFullPath)
            b_io.ann.here("usgCursParsSet: {parValue} at {parNameFullPath}".
                         format(parValue=parValue, parNameFullPath=parNameFullPath))

        return cmndOutcome.set(
            opError=b.OpError.Success,
            opResults=True,
        )

####+BEGIN: b:py3:cs:method/args :methodName "cmndArgsSpec" :methodType "anyOrNone" :retType "bool" :deco "default" :argsList "self"
    """ #+begin_org
**  _[[elisp:(blee:menu-sel:outline:popupMenu)][±]]_ _[[elisp:(blee:menu-sel:navigation:popupMenu)][Ξ]]_ [[elisp:(outline-show-branches+toggle)][|=]] [[elisp:(bx:orgm:indirectBufOther)][|>]] *[[elisp:(blee:ppmm:org-mode-toggle)][|N]]*  Mtd-T-anyOrNone [[elisp:(outline-show-subtree+toggle)][||]] /cmndArgsSpec/ deco=default  deco=default  [[elisp:(org-cycle)][| ]]
    #+end_org """
    @cs.track(fnLoc=True, fnEntry=True, fnExit=True)
    def cmndArgsSpec(self, ):
####+END:
        """
***** Cmnd Args Specification
"""
        cmndArgsSpecDict = cs.CmndArgsSpecDict()
        cmndArgsSpecDict.argsDictAdd(
            argPosition="0&-1",
            argName="cmndArgs",
            argDefault=None,
            argChoices='any',
            argDescription="A sequence of varName=varValue"
        )

        return cmndArgsSpecDict

####+BEGIN: b:prog:file/endOfFile :extraParams nil
""" #+begin_org
* *[[elisp:(org-cycle)][| END-OF-FILE |]]* :: emacs and org variables and control parameters
#+end_org """
### local variables:
### no-byte-compile: t
### end:
####+END:


import json
import numpy as np

def load_json(file_path : str) -> dict:
    """
    Loads .json file types.

    Use json python library to load a .json file.

    Parameters
    ----------
    file_path : string
        Path to file.

    Returns
    -------
    json file : dictionary
        .json dictionary file.

    See Also
    --------
    read_GMR_file
    save_json_dicts

    Notes
    -----
    json files are typically dictionaries, as such the function is intended for
    use with dictionaries stored in .json file types.

    Examples
    --------
    my_dictionary = load_json(file_path="/Path/To/File")

    """
    with open(file_path, 'r') as file:
        return json.load(file)

def read_GMR_file(file_path):
    '''
    Load txt output from GMRX spectrometer. Return wavelength in nm.
    Args:
        file_path: <string> path to file
    Returns:
        wavelength: <array> wavelength array
        intensity: <array> intensity array
    '''
    try:
        wavelength, intensity = np.genfromtxt(
            fname=file_path,
            delimiter=';',
            unpack=True)
    except:
        wavelength, intensity = np.genfromtxt(
            fname=file_path,
            delimiter=',',
            unpack=True)
    return wavelength, intensity

def convert(o):
    """
    Check data type.

    Check type of data string.

    Parameters
    ----------
    o : string
        String to check.

    Returns
    -------
    TypeError : Boolean
        TypeError if string is not suitable.

    See Also
    --------
    None.

    Notes
    -----
    None.

    Examples
    --------
    None.

    """
    if isinstance(o, np.generic):
        return o.item()
    raise TypeError

def save_json_dicts(out_path : str,
                    dictionary : dict) -> None:
    """
    Save .json file types.

    Use json python library to save a dictionary to a .json file.

    Parameters
    ----------
    out_path : string
        Path to file.
    dictionary : dictionary
        Dictionary to save.

    Returns
    -------
    None

    See Also
    --------
    load_json

    Notes
    -----
    json files are typically dictionaries, as such the function is intended for
    use with dictionaries stored in .json file types.

    Examples
    --------
    save_json_dicts(
        out_path="/Path/To/File",
        dictionary=my_dictionary)

    """
    with open(out_path, 'w') as outfile:
        json.dump(
            dictionary,
            outfile,
            indent=2,
            default=convert)
        outfile.write('\n')

def reflectometer_in(file_path : str) -> list:
    """
    Loads text file output from the Filmetrics spectroscopic reflectometer.

    Loads a 3 column, comma delimited, .fitnk file output from a Filmetrics F20
    spectroscopic reflectometer.

    Parameters
    ----------
    file_path: string
        Path to file.

    Returns
    -------
    col0, col1, col2: list
        Typically wavelength (nm), n, k.

    See Also
    --------
    numpy genfromtxt

    Notes
    -----
    The .fitnk file from the Filmetrics F20 contains 5 header rows and 6 footer
    rows that are seemingly not useful information. The function skips over the
    rows.

    Examples
    --------
    None

    """
    col0, col1, col2 = np.genfromtxt(
        fname=file_path,
        delimiter=',',
        skip_header=5,
        skip_footer=6,
        unpack=True)
    return col0, col1, col2

def ellipsometer_in(file_path : str) -> list:
    """
    Load text file output from the J.A. Woollam VASE.

    Loads a 5 column, comma delimited, .csv file output from a J.A. Woollam
    variable angle spectroscopic ellipsometer.

    Parameters
    ----------
    file_path: string
        Path to file.

    Returns
    -------
    col0, col1, col2, col3, col4: list
        Typically wavelength (nm), sample psi, sample delta, model psi, model
        delta.

    See Also
    --------
    numpy genfromtxt

    Notes
    -----
    None

    Example
    -------
    None

    """
    col0, col1, col2, col3, col4 = np.genfromtxt(
        fname=file_path,
        delimiter=',',
        skip_header=2,
        usecols=(0, 1, 2, 3, 4),
        unpack=True)
    return col0, col1, col2, col3, col4


"""
We are given a directed graph. We are given also a set of pairs of vertices.
Find the shortest distance between each pair of vertices or -1 if there is no path connecting them.
On the first line, you will get N, the number of vertices in the graph.
On the second line, you will get P, the number of pairs between which to find the shortest distance.
On the next N lines will be the edges of the graph and on the next P lines, the pairs.

"""
from collections import deque
from typing import Dict, List, Union

def build_graph(nodes: int) -> Dict[int, List[int]]:
    graph = {}

    for _ in range(nodes):
        node, children_str = input().split(':')
        node = int(node)
        children = [int(x) for x in children_str.split(' ')] if children_str else []
        graph[node] = children

    return graph

def bfs(graph: Dict[int, List[int]], source: int, destination: int) -> Dict[int, Union[None, int]]:
    queue = deque([source])

    visited = {source}

    parent = {source: None}

    while queue:
        node = queue.popleft()

        if node == destination:
            break

        for child in graph[node]:
            if child in visited:
                continue

            queue.append(child)
            visited.add(child)

            parent[child] = node

    return parent

def find_size(parent: Dict[int, Union[None, int]], destination: int) -> int:
    node = destination
    size = -1

    while node is not None:
        node = parent[node]
        size += 1

    return size

nodes = int(input())
pairs = int(input())

graph = build_graph(nodes)

for _ in range(pairs):
    source, destination = [int(x) for x in input().split('-')]

    parent = bfs(graph, source, destination)

    if destination not in parent:
        print(f'{{{source}, {destination}}} -> -1')
        continue

    size = find_size(parent, destination)

    print(f'{{{source}, {destination}}} -> {size}')

# Test solution at:
# https://judge.softuni.org/Contests/Practice/Index/3465#0

04. Minimum-spanning-tree-and-Shortest-path-in-Graph/02. Exercise/01. distance_between_vertices.py


import os
today = '02-06-19_'
import numpy as np
import treecorr

def parse_args():
    import argparse
    parser = argparse.ArgumentParser(description='Produce Tau correlations, i.e correlation among galaxies and reserved stars')

    parser.add_argument('--metacal_cat',
                        #default='/home2/dfa/sobreira/alsina/catalogs/y3_master/Y3_mastercat_v2_6_20_18_subsampled.h5',
                        #default='/home2/dfa/sobreira/alsina/catalogs/y3_master/Y3fullmaster/Y3_mastercat_v2_6_20_18.h5',
                        default='/home/dfa/sobreira/alsina/catalogs/Y3_mastercat_7_24/Y3_mastercat_7_24_19.h5',
                        help='Full Path to the Metacalibration catalog')
    parser.add_argument('--piff_cat',
                        default='/home/dfa/sobreira/alsina/catalogs/y3a1-v29',
                        help='Full Path to the Only stars Piff catalog')
    parser.add_argument('--exps_file',
                        default='/home/dfa/sobreira/alsina/Y3_shearcat_tests/alpha-beta-eta-test/code/ally3.grizY',
                        #default='/home/dfa/sobreira/alsina/DESWL/psf/testexp',
                        help='list of exposures (in lieu of separate exps)')
    parser.add_argument('--bands', default='riz', type=str,
                         help='Limit to the given bands')
    parser.add_argument('--use_reserved', default=True,
                        action='store_const', const=True,
                        help='just use the objects with the RESERVED flag')
    parser.add_argument('--frac', default=1.,
                        type=float,
                        help='Choose a random fraction of the input stars')
    parser.add_argument('--mod', default=True,
                        action='store_const', const=True,
                        help='If true it substracts the mean to each field before calculate correlations')
    parser.add_argument('--obs', default=False,
                        action='store_const', const=True,
                        help='If true it uses psf_e stars for tau0')
    parser.add_argument('--weights', default=False,
                        action='store_const', const=True,
                        help='Use weights in the reading of Metacal')
    parser.add_argument('--bin_config', default=None,
                        help='bin_config file for running taus')
    parser.add_argument('--outpath', default='/home/dfa/sobreira/alsina/Y3_shearcat_tests/alpha-beta-eta-test/measured_correlations/',
                        help='location of the output of the files')
    parser.add_argument('--filename', default='TAUS_zbin_n.fits', type=str,
                        help='filename of the tau output file')
    parser.add_argument('--zbin', default=None,type=int,
                        help='Run particular tomobin')
    parser.add_argument('--nz_source',
                        #default='/home/dfa/sobreira/alsina/catalogs/y3_master/nz_source_zbin.h5',
                        default='/home/dfa/sobreira/alsina/catalogs/Y3_mastercat_7_24/nz_source_zbin.h5',
                        help='Indexes catalog to select galaxies in a particular redshift bin in Metacal')
    args = parser.parse_args()
    return args

def main():
    import sys; sys.path.append(".")
    from src.read_cats import read_data_stars, toList, read_metacal
    from src.runcorr import measure_tau
    from astropy.io import fits
    import treecorr

    args = parse_args()

    #Make directory where the ouput data will be
    outpath = os.path.expanduser(args.outpath)
    try:
        if not os.path.exists(outpath):
            os.makedirs(outpath)
    except OSError:
        if not os.path.exists(outpath): raise

    #Reading Mike stars catalog
    keys = ['ra', 'dec','obs_e1', 'obs_e2', 'obs_T',
            'piff_e1', 'piff_e2', 'piff_T', 'mag']
    galkeys = ['ra','dec','e_1','e_2','R11','R22']

    data_stars = read_data_stars(toList(args.exps_file),args.piff_cat, keys,limit_bands=args.bands,use_reserved=args.use_reserved)

    if args.bin_config is not None:
        print("Using external bin config")
        bin_config = treecorr.read_config(args.bin_config)
        print(bin_config)
    else:
        #bin_config = dict( sep_units = 'arcmin', min_sep = 0.1, max_sep = 250, nbins = 20, bin_slop=0.03 )
        bin_config = dict( sep_units = 'arcmin', min_sep = 0.1, max_sep = 250, nbins = 20, )
        #bin_config = dict( sep_units = 'arcmin', min_sep = 1.0, max_sep = 250, nbins = 20,)
        #bin_config = dict(sep_units = 'arcmin' , bin_slop = 0.1, min_sep = 0.1, max_sep = 300, bin_size = 0.2)

    if args.zbin is not None:
        print('STARTING TOMOPRAPHIC TAUS!, measuring tau for zbin=', args.zbin)
        data_galaxies = read_metacal(args.metacal_cat, galkeys, zbin=args.zbin,nz_source_file=args.nz_source, weights=args.weights)
    else:
        print("STARTING NON TOMOGRAPHIC TAUS")
        data_galaxies = read_metacal(args.metacal_cat,  galkeys, weights=args.weights )

    tau0, tau2, tau5= measure_tau( data_stars , data_galaxies, bin_config,
                                   mod=args.mod)
    tau0marr = tau0.xim; tau2marr = tau2.xim;  tau5marr = tau5.xim;
    tau0parr = tau0.xip; tau2parr = tau2.xip;  tau5parr = tau5.xip;
    taus = [tau0parr, tau0marr, tau2parr, tau2marr, tau5parr, tau5marr]
    taus_names = ['TAU0P', 'TAU0M','TAU2P','TAU2M', 'TAU5P', 'TAU5M']

    ##Format of the fit file output
    names=['BIN1', 'BIN2','ANGBIN', 'VALUE', 'ANG']
    forms = ['i4', 'i4', 'i4',  'f8',  'f8']
    dtype = dict(names = names, formats=forms)
    nrows = len(tau0marr)
    outdata = np.recarray((nrows, ), dtype=dtype)

    covmat = np.diag(np.concatenate( (tau0.varxip, tau0.varxim, tau2.varxip, tau2.varxim, tau5.varxip, tau5.varxim ) ))
    hdu = fits.PrimaryHDU()
    hdul = fits.HDUList([hdu])
    covmathdu = fits.ImageHDU(covmat, name='COVMAT')
    hdul.insert(1, covmathdu)

    bin1array = np.array([ -999]*nrows)
    bin2array = np.array([ -999]*nrows)
    angbinarray = np.arange(nrows)
    angarray = np.exp(tau0.meanlogr)

    for j, nam in enumerate(taus_names):
        array_list = [bin1array, bin2array, angbinarray,np.array(taus[j]),  angarray ]
        for array, name in zip(array_list, names): outdata[name] = array
        corrhdu = fits.BinTableHDU(outdata, name=nam)
        hdul.insert(j+2, corrhdu)

    hdul[1].header['COVDATA'] = True
    hdul[1].header['EXTNAME'] =  'COVMAT'
    hdul[1].header['NAME_0'] =  'TAU0P'
    hdul[1].header['STRT_0'] =  0
    hdul[1].header['LEN_0'] = nrows
    hdul[1].header['NAME_1'] =  'TAU0M'
    hdul[1].header['STRT_1'] =  nrows
    hdul[1].header['LEN_1'] = nrows
    hdul[1].header['NAME_2'] =  'TAU2P'
    hdul[1].header['STRT_2'] =  2*nrows
    hdul[1].header['LEN_2'] = nrows
    hdul[1].header['NAME_3'] =  'TAU2M'
    hdul[1].header['STRT_3'] =  3*nrows
    hdul[1].header['LEN_3'] = nrows
    hdul[1].header['NAME_4'] =  'TAU5P'
    hdul[1].header['STRT_4'] =  4*nrows
    hdul[1].header['LEN_4'] = nrows
    hdul[1].header['NAME_5'] =  'TAU5M'
    hdul[1].header['STRT_5'] =  5*nrows
    hdul[1].header['LEN_5'] = nrows

    hdul[2].header['QUANT1'] = 'GeR'; hdul[3].header['QUANT1'] = 'GeR'
    hdul[2].header['QUANT2'] = 'PeR'; hdul[3].header['QUANT2'] = 'PeR'
    hdul[4].header['QUANT1'] = 'GeR'; hdul[5].header['QUANT1'] = 'GeR'
    hdul[4].header['QUANT2'] = 'PqR'; hdul[5].header['QUANT2'] = 'PqR'
    hdul[6].header['QUANT1'] = 'GeR'; hdul[7].header['QUANT1'] = 'GeR'
    hdul[6].header['QUANT2'] = 'PwR'; hdul[7].header['QUANT2'] = 'PwR'

    filename = os.path.join(outpath, args.filename)
    print("Printing file:", filename)
    hdul.writeto(filename, overwrite=True)

if __name__ == "__main__":
    main()


from matplotlib import pyplot as plt
import numpy as np
import collections
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import matplotlib.pyplot as plt

# Get cpu or gpu device for training.
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))

# class_names = ['airplane','automobile','bird','cat','deer',
#                'dog','frog','horse','ship','truck']

# not needed AFTER getting mean and standard deviation
# cifar10_train = datasets.CIFAR10(
#     root='data', train=True, download=True,
#     transform=transforms.ToTensor())
#
# cifar10_val = datasets.CIFAR10(
#     root='data', train=False, download=True,
#     transform=transforms.ToTensor())

# imgs_train = torch.stack([img_t for img_t, _ in cifar10_train], dim=3)
# imgs_val = torch.stack([img_t for img_t, _ in cifar10_val], dim=3)

# train_mean = imgs_train.view(3,-1).mean(dim=1)
# train_std = imgs_train.view(3,-1).std(dim=1)
#
# val_mean = imgs_val.view(3,-1).mean(dim=1)
# val_std = imgs_val.view(3,-1).std(dim=1)

# load data, no think
cifar10_train = datasets.CIFAR10(
    root='data', train=True, download=True,
    transform=transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.4915, 0.4823, 0.4468),
                             (0.2470, 0.2435, 0.2616))]))
train_length = len(cifar10_train)
train_size = int(0.8 *train_length)
val_size = train_length - train_size

# make trai and validation set
cifar10_train, cifar10_val = torch.utils.data.random_split(cifar10_train, [train_size, val_size])

cifar10_test = datasets.CIFAR10(
    root='data', train=False, download=True,
    transform=transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.4915, 0.4823, 0.4468),
                             (0.2470, 0.2435, 0.2616))]))

# comment this and change output neurons (and dataloader far below) if you want only to find difference beetwenn planes and birds
# get only birds and planes
label_map = {0: 0, 2: 1}
class_names = ['airplane', 'bird']
cifar10_train_ = [(img, label_map[label])
          for img, label in cifar10_train
          if label in [0, 2]]

cifar10_val_ = [(img, label_map[label])
          for img, label in cifar10_val
          if label in [0, 2]]

cifar10_test_ = [(img, label_map[label])
              for img, label in cifar10_test
              if label in [0, 2]]

# store train and val loss
train_loss_list = []
val_loss_list = []
epoch_list = []

# make network architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) # convolution layer (in_chl, out_chl,...)
        self.conv1_batchnorm = nn.BatchNorm2d(16)
        self.act1 = nn.Tanh() # activation function
        self.pool1 = nn.MaxPool2d(2) # pooling (kernel size 2x2)
        self.conv2 = nn.Conv2d(16, 8, kernel_size=3, padding=1)
        self.conv2_batchnorm = nn.BatchNorm2d(8)
        self.act2 = nn.Tanh()
        self.pool2 = nn.MaxPool2d(2)
        self.fc1 = nn.Linear(8 * 8 * 8, 32) # first 8 from conv2, next 8's from pooling (32->16->8)
        self.act3 = nn.Tanh()
        self.fc2 = nn.Linear(32, 2)
        # self.act4 = nn.Softmax(dim=1)

    def forward(self, x):
        out = self.conv1_batchnorm(self.conv1(x))
        out = self.pool1(((self.act1(out))))
        out = self.conv2_batchnorm(self.conv2(out))
        out = self.pool2(((self.act2(out))))
        out = out.view(-1, 8 * 8 * 8)  # not sure why reshape
        out = self.act3(self.fc1(out))
        out = self.fc2(out)
        return out

import datetime # to measure time

def training_loop(n_epochs, optimizer, model, loss_fn, train_loader, val_loader, epoch_num_of_no_improve):
    epoch_no_improve = 0
    for epoch in range(1, n_epochs+1):
        loss_train = 0.0
        for imgs, labels in train_loader:

            # move tensors to gpu if available
            imgs = imgs.to(device=device)
            labels = labels.to(device=device)

            outputs = model(imgs)

            loss = loss_fn(outputs, labels)

            l2_lambda = 0.001
            l2_norm = sum(p.pow(2.0).sum()
                          for p in model.parameters())
            loss = loss + l2_lambda * l2_norm

            optimizer.zero_grad()

            loss.backward()

            optimizer.step()

            loss_train += loss.item()

        epoch_list.append(epoch)
        train_loss_list.append(loss_train / len(train_loader)) # to track loss
        # get loss of validation data
        with torch.no_grad():
            loss_val = 0.0
            for imgs, labels in val_loader:
                # move tensors to gpu if available
                imgs = imgs.to(device=device)
                labels = labels.to(device=device)

                outputs = model(imgs)

                loss_v = loss_fn(outputs, labels)

                loss_val += loss_v.item()
        val_loss_list.append(loss_val / len(val_loader))

        # set when to print info about training progress
        if epoch == 1 or epoch % 1 == 0:
            print('Epoch {}, Training loss {}, Validation loss {}'.format(epoch,
                                                                          loss_train / len(train_loader),
                                                                          loss_val / len(val_loader)))

        # early stopping

        if epoch > 1:
            if val_loss_list[-1] >= val_loss_list[-2]:
                epoch_no_improve += 1
            else:
                epoch_no_improve = 0

        if epoch_no_improve == epoch_num_of_no_improve:
            print('Early stopping:')
            print('Epoch {}, Training loss {}, Validation loss {}'.format(epoch,
                                                                          loss_train / len(train_loader),
                                                                          loss_val / len(val_loader)))
            break

def validate_on_test(model, train_loader, val_loader, test_loader):
    for name, loader in [("train", train_loader), ("val", val_loader), ('test', test_loader)]:
        correct = 0
        total = 0

        with torch.no_grad():  # <1>
            for imgs, labels in loader:

                # move to gpu
                imgs = imgs.to(device=device)
                labels = labels.to(device=device)

                outputs = model(imgs)
                _, predicted = torch.max(outputs, dim=1) # Gives us the index of the highest value
                total += labels.shape[0]  # Counts the number of examples, so total is increased by the batch size
                correct += int((predicted == labels).sum())

        print("Accuracy {}: {:.2f} %".format(name , 100 *  (correct / total)))

n_epochs = 100
model = Net().to(device=device)
optimizer = optim.ASGD(model.parameters(), lr=1e-2)
loss_fn = nn.CrossEntropyLoss()
train_loader = torch.utils.data.DataLoader(cifar10_train_, batch_size=64, shuffle=False)
val_loader = torch.utils.data.DataLoader(cifar10_val_, batch_size=64, shuffle=False)
test_loader = torch.utils.data.DataLoader(cifar10_test_,  batch_size=64, shuffle=False)
epoch_num_of_no_improve = 5

training_loop(
    n_epochs = n_epochs,
    optimizer = optimizer,
    model = model,
    loss_fn = loss_fn,
    train_loader = train_loader,
    val_loader = val_loader,
    epoch_num_of_no_improve=epoch_num_of_no_improve)

validate_on_test(model, train_loader, val_loader, test_loader)

plt.plot(epoch_list, train_loss_list, color='blue', label='train_loss')
plt.plot(epoch_list, val_loss_list, color='green', label='validation loss')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend()
plt.show()


from sqlalchemy.orm import Session
from .. import models, schemas
from fastapi.encoders import jsonable_encoder

def get_score(db: Session):
    score = db.query(models.Score).first()
    if not score:
        new_score = create_score()
        db.add(new_score)
        db.commit()
        db.refresh(new_score)
        return new_score
    return score

def post_goal(request: schemas.Goal, db: Session):
    score = db.query(models.Score).first()
    if not score:
        new_score = create_score()
        db.add(new_score)
        db.commit()
        db.refresh(new_score)
    score = db.query(models.Score).first()
    query = jsonable_encoder(request)
    if query["team"] == "home":
        score.home += 1
    else:
        score.away += 1
    db.commit()
    return score

def create_score():
    new_score = models.Score(home=0, away=0)
    return new_score


import torch.nn as nn

from collections import OrderedDict

from graph_ter_seg.tools import utils

class EdgeConvolution(nn.Module):
    def __init__(self, k, in_features, out_features):
        super(EdgeConvolution, self).__init__()
        self.k = k
        self.conv = nn.Conv2d(
            in_features * 2, out_features, kernel_size=1, bias=False
        )
        self.bn = nn.BatchNorm2d(out_features)
        self.relu = nn.LeakyReLU(negative_slope=0.2)

    def forward(self, x):
        x = utils.get_edge_feature(x, k=self.k)
        x = self.relu(self.bn(self.conv(x)))
        x = x.max(dim=-1, keepdim=False)[0]
        return x

class MultiEdgeConvolution(nn.Module):
    def __init__(self, k, in_features, mlp):
        super(MultiEdgeConvolution, self).__init__()
        self.k = k
        self.conv = nn.Sequential()
        for index, feature in enumerate(mlp):
            if index == 0:
                layer = nn.Sequential(OrderedDict([
                    ('conv%d' %index, nn.Conv2d(
                        in_features * 2, feature, kernel_size=1, bias=False
                    )),
                    ('bn%d' % index, nn.BatchNorm2d(feature)),
                    ('relu%d' % index, nn.LeakyReLU(negative_slope=0.2))
                ]))
            else:
                layer = nn.Sequential(OrderedDict([
                    ('conv%d' %index, nn.Conv2d(
                        mlp[index - 1], feature, kernel_size=1, bias=False
                    )),
                    ('bn%d' % index, nn.BatchNorm2d(feature)),
                    ('relu%d' % index, nn.LeakyReLU(negative_slope=0.2))
                ]))
            self.conv.add_module('layer%d' % index, layer)

    def forward(self, x):
        x = utils.get_edge_feature(x, k=self.k)
        x = self.conv(x)
        x = x.max(dim=-1, keepdim=False)[0]
        return x

class Flatten(nn.Module):
    def __init__(self):
        super(Flatten, self).__init__()

    def forward(self, x):
        return x.view(x.size(0), -1)

def main():
    conv = MultiEdgeConvolution(k=20, mlp=(64, 64), in_features=64)
    print(conv)

if __name__ == '__main__':
    main()


import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
import random
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

Datadirectory = "train\\"
Classes = ["0", "1", "2", "3", "4", "5", "6"]

img_size = 224

training_data = []
counter = 0
def createtrainingset():
    for category in Classes:
        path = os.path.join(Datadirectory, category)
        class_num = Classes.index(category)
        for img in os.listdir(path):
            try:
                img_arr = cv2.imread(os.path.join(path, img))
                new_arr = cv2.resize(img_arr, (img_size, img_size))
                training_data.append([new_arr, class_num])
            except Exception as e:
                pass

createtrainingset()

print(len(training_data))

random.shuffle(training_data)

X = []  # Images (features)
y = []  # Labels

for feature, label in training_data:
    X.append(feature)
    y.append(label)

y = np.array(y)
X = np.array(X)
X = X.reshape(-1, img_size, img_size, 3)
X = X / 255.0  # Normalize the image data between 0 and 1

print(X.shape)
print(y.shape)

plt.imshow(X[0])
plt.show()

model = tf.keras.applications.MobileNetV2()

#TRANSFER LEARNING  - TUNING ,weights will start from lasr check point

base_input = model.layers[0].input
base_output = model.layers[-2].output

final_output = layers.Dense(128)(base_output)
final_output = layers.Activation('relu')(final_output)
final_output = layers.Dense(64)(final_output)
final_output = layers.Activation('relu')(final_output)
final_output = layers.Dense(7, activation = 'softmax')(final_output)

new_model = keras.Model(inputs = base_input, outputs = final_output)

new_model.compile(loss = "sparse_categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"])

new_model.fit(X,y, epochs=10, batch_size = 8)

new_model.save('onbes_epoch.h5')


"""Order views module"""
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework import status as st
from rest_framework import generics
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer
from rest_framework.parsers import JSONParser
from rest_framework.response import Response
from rest_framework.exceptions import MethodNotAllowed, NotFound
from rest_framework.decorators import api_view

from orders.models import Order, STATUS_CHOICES
from orders.serializers import OrderSerializer
from orders.pagination import CustomPagination
from order_flow.settings import DEBUG

class OrderAPIListCreate(generics.ListCreateAPIView):
    """
    Returns list of orders in JSON format and gave an option to create orders
    """
    if DEBUG:
        renderer_classes = [JSONRenderer, BrowsableAPIRenderer]
    else:
        renderer_classes = [JSONRenderer]
    queryset = Order.objects.all()
    serializer_class = OrderSerializer
    pagination_class = CustomPagination
    filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
    filterset_fields = ['external_id', 'status']
    ordering_fields = ['id', 'status', 'created_at']

class OrderAPIRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView):
    """
    Returns distinct order JSON info and gave an option to update and delete it
    """
    if DEBUG:
        renderer_classes = [JSONRenderer, BrowsableAPIRenderer]
    else:
        renderer_classes = [JSONRenderer]
    parser_classes = [JSONParser]
    queryset = Order.objects.all()
    serializer_class = OrderSerializer

    def put(self, request, *args, **kwargs):
        """Add a possibility of partial update, using put method"""
        return self.partial_update(request, *args, **kwargs)

    def perform_destroy(self, instance):
        """Protect order from delete if its status is 'accepted'."""
        if instance.status == 'accepted':
            raise MethodNotAllowed(
                'delete',
                detail="You can not delete orders with status 'accepted'.",
            )
        instance.delete()

@api_view(['POST'])
def status_change(request, pk, status):
    """Change order status"""
    try:
        order = Order.objects.get(id=pk)
    except Order.DoesNotExist:
        raise NotFound(f'Order with id {pk} does not exist.')

    if status not in [statuses[0] for statuses in STATUS_CHOICES]:
        raise MethodNotAllowed(
            'post',
            detail="You can change order status"
                   " only to 'accepted' or 'failed'",
        )
    if order.status != 'new':
        raise MethodNotAllowed(
            'post',
            detail="You can not change order status if it is not 'new'",
        )
    order.status = status
    order.save()
    return Response(status=st.HTTP_200_OK)