""" A module containing the necessary constants, enumerations, classes, etc., that have to be used
by numerous other modules.
"""

import os
import math
from functools import partial

from enum import Enum
from perun.utils.structs import OrderedEnum
import perun.utils as utils
import perun.utils.metrics as metrics

class Optimizations(Enum):
    """ Enumeration of the implemented methods and their CLI name.
    """
    BASELINE_STATIC = 'baseline-static'
    BASELINE_DYNAMIC = 'baseline-dynamic'
    CALL_GRAPH_SHAPING = 'cg-shaping'
    DYNAMIC_SAMPLING = 'dynamic-sampling'
    DIFF_TRACING = 'diff-tracing'
    DYNAMIC_PROBING = 'dynamic-probing'
    TIMED_SAMPLING = 'timed-sampling'

    @staticmethod
    def supported():
        """ List the currently supported optimization methods.

        :return list: CLI names of the supported optimizations
        """
        return [optimization.value for optimization in Optimizations]

class Pipeline(Enum):
    """ Enumeration of the implemented pipelines and their CLI name.
    Custom represents a defualt pipeline that has no pre-configured methods or parameters
    """
    CUSTOM = 'custom'
    BASIC = 'basic'
    ADVANCED = 'advanced'
    FULL = 'full'

    @staticmethod
    def supported():
        """ List the currently supported optimization pipelines.

        :return list: CLI names of the supported pipelines
        """
        return [pipeline.value for pipeline in Pipeline]

    @staticmethod
    def default():
        """ Name of the default pipeline.

        :return str: the CLI name of the default pipeline
        """
        return Pipeline.CUSTOM.value

    def map_to_optimizations(self):
        """ Map the selected optimization pipeline to the set of employed optimization methods.

        :return list: list of the Optimizations enumeration objects
        """
        if self == Pipeline.BASIC:
            return [Optimizations.CALL_GRAPH_SHAPING, Optimizations.BASELINE_DYNAMIC]
        if self == Pipeline.ADVANCED:
            return [
                Optimizations.DIFF_TRACING, Optimizations.CALL_GRAPH_SHAPING,
                Optimizations.BASELINE_DYNAMIC, Optimizations.DYNAMIC_SAMPLING
            ]
        if self == Pipeline.FULL:
            return [
                Optimizations.DIFF_TRACING, Optimizations.CALL_GRAPH_SHAPING,
                Optimizations.BASELINE_STATIC, Optimizations.BASELINE_DYNAMIC,
                Optimizations.DYNAMIC_SAMPLING, Optimizations.DYNAMIC_PROBING,
            ]
        return []

class CallGraphTypes(Enum):
    """ Enumeration of the implemented call graph types and their CLI names.
    """
    STATIC = 'static'
    DYNAMIC = 'dynamic'
    MIXED = 'mixed'

    @staticmethod
    def supported():
        """ List the currently supported call graph types.

        :return list: CLI names of the supported cg types
        """
        return [cg.value for cg in CallGraphTypes]

    @staticmethod
    def default():
        """ Name of the default cg type.

        :return str: the CLI name of the default cg type
        """
        return CallGraphTypes.STATIC.value

class Parameters(Enum):
    """ Enumeration of the currently supported CLI options for optimization methods and pipelines.
    """
    DIFF_VERSION = 'diff-version'
    DIFF_KEEP_LEAF = 'diff-keep-leaf'
    DIFF_INSPECT_ALL = 'diff-inspect-all'
    DIFF_CG_MODE = 'diff-cfg-mode'
    SOURCE_FILES = 'source-files'
    SOURCE_DIRS = 'source-dirs'
    STATIC_COMPLEXITY = 'static-complexity'
    STATIC_KEEP_TOP = 'static-keep-top'
    CG_SHAPING_MODE = 'cg-mode'
    CG_PROJ_LEVELS = 'cg-proj-levels'
    CG_PROJ_KEEP_LEAF = 'cg-proj-keep-leaf'
    DYNSAMPLE_STEP = 'dyn-sample-step'
    DYNSAMPLE_THRESHOLD = 'dyn-sample-threshold'
    PROBING_THRESHOLD = 'probing-threshold'
    PROBING_REATTACH = 'probing-reattach'
    TIMEDSAMPLE_FREQ = 'timed-sample-freq'
    DYNBASE_SOFT_THRESHOLD = 'dyn-base-soft-threshold'
    DYNBASE_HARD_THRESHOLD = 'dyn-base-hard-threshold'
    THRESHOLD_MODE = 'threshold-mode'

    @staticmethod
    def supported():
        """ List the currently supported optimization parameters.

        :return list: CLI names of the supported parameters
        """
        return [parameter.value for parameter in Parameters]

class DiffCfgMode(Enum):
    """ Enumeration of the currently supported CFG comparison mode.
    """
    COLORING = 'color'
    SOFT = 'soft'
    SEMISTRICT = 'semistrict'
    STRICT = 'strict'

    @staticmethod
    def supported():
        """ List the currently supported CFG comparison modes.

        :return list: CLI names of the supported modes
        """
        return [mode.value for mode in DiffCfgMode]

class CGShapingMode(Enum):
    """ Enumeration of the currently supported Call Graph Shaping modes.
    """
    MATCH = 'match'
    BOTTOM_UP = 'bottom-up'
    TOP_DOWN = 'top-down'

    @staticmethod
    def supported():
        """ List the currently supported Call Graph Shaping modes.

        :return list: CLI names of the supported modes
        """
        return [mode.value for mode in CGShapingMode]

class ThresholdMode(Enum):
    """ Enumeration of the currently supported threshold modes.
    """
    SOFT = 'soft'
    STRICT = 'strict'

    @staticmethod
    def supported():
        """ List the currently supported Threshold modes.

        :return list: CLI names of the supported modes
        """
        return [mode.value for mode in ThresholdMode]

class Complexity(OrderedEnum):
    """ Enumeration of the complexity degrees that we distinguish in the Bounds collector output.
    """
    # Polynomial-to-complexity map
    _ignore_ = ['map']

    # Complexities
    ZERO = 'zero'
    CONSTANT = 'constant'
    LINEAR = 'linear'
    QUADRATIC = 'quadratic'
    CUBIC = 'cubic'
    QUARTIC = 'quartic'
    GENERIC = 'generic'

    @staticmethod
    def supported():
        """ List the currently supported Complexity degrees.

        :return list: CLI names of the supported complexities
        """
        return [complexity.value for complexity in Complexity]

    @staticmethod
    def max(values):
        """ Compare a collection of Complexity values and select the one with maximum degree.

        :param collection values: the set of Complexity values

        :return Complexity: the Complexity object with the highest degree of polynomial
        """
        return sorted(values, key=lambda complexity: complexity.order, reverse=True)[0]

    @classmethod
    def from_poly(cls, polynomial):
        """ Create a Complexity object from string representing a polynomial.

        :param str polynomial: a string representation of a supported polynomial

        :return Complexity: the corresponding Complexity object
        """
        return Complexity.map.get(polynomial, cls.GENERIC)

Complexity.map = {
    'O(1)': Complexity.CONSTANT,
    'O(n^1)': Complexity.LINEAR,
    'O(n^2)': Complexity.QUADRATIC,
    'O(n^3)': Complexity.CUBIC,
    'O(n^4)': Complexity.QUARTIC
}

class ParametersManager:
    """ Class that parses and stores the user-supplied optimization parameters, as well as predicts
    suitable values for the optimization parameters that were not supplied.

    :ivar list cli_params: contains the list of user-supplied parameters before they are applied
    :ivar dict param_map: stores the default values for all parameters and provides a function
                          to validate the user-supplied values.

    """
    def __init__(self):
        """ Initializes all of the optimization parameters to their default values.
        """
        # Keep leaves when the number of profiled functions is low enough
        self._functions_keep_leaves = 20
        # Keep top 10% of call graph levels, however minimum of 1
        self._keep_top_ratio = 0.1
        self._default_keep_top = 1
        # Call graph default levels to keep: top 50%, minimum 2 levels
        self._levels_ratio = 0.5
        self._default_min_levels = 2
        # Pruning chain length: minimum 1 - i.e. only leaf nodes kept
        self._default_chain_length = 1
        # Default sampling step does not scale
        self._default_sampling_step = 2
        # Soft function call threshold for dynamic baseline / sampling is set 10000
        self._threshold_soft_base = 10000
        # Strict function call threshold is set to 1000
        self._threshold_strict_base = 1000
        # Hard function call threshold used by dynamic baseline is 100x more than the used threshold
        self._hard_threshold_coefficient = 100
        # Dynamic probing threshold is set to 100000, however multiplied by 0.2 in reattach mode
        self._probing_threshold = 100000
        self._probing_reattach_coefficient = 0.2

        self.cli_params = []
        self.param_map = {
            # TODO: add proper check
            Parameters.DIFF_VERSION: {
                'value': None,
                'validate': lambda x: x
            },
            Parameters.DIFF_KEEP_LEAF: {
                'value': False,
                'validate': self._validate_bool
            },
            Parameters.DIFF_INSPECT_ALL: {
                'value': True,
                'validate': self._validate_bool
            },
            Parameters.DIFF_CG_MODE: {
                'value': DiffCfgMode.SEMISTRICT,
                'validate': partial(self._validate_enum, DiffCfgMode)
            },
            Parameters.SOURCE_FILES: {
                'value': [],
                'validate': self._validate_path
            },
            Parameters.SOURCE_DIRS: {
                'value': [],
                'validate': self._validate_path
            },
            Parameters.STATIC_COMPLEXITY: {
                'value': Complexity.CONSTANT,
                'validate': partial(self._validate_enum, Complexity)
            },
            Parameters.STATIC_KEEP_TOP: {
                'value': self._default_keep_top,
                'validate': self._validate_uint
            },
            Parameters.CG_SHAPING_MODE: {
                'value': CGShapingMode.MATCH,
                'validate': partial(self._validate_enum, CGShapingMode)
            },
            Parameters.CG_PROJ_LEVELS: {
                'value': self._default_chain_length,
                'validate': self._validate_uint
            },
            Parameters.CG_PROJ_KEEP_LEAF: {
                'value': False,
                'validate': self._validate_bool
            },
            Parameters.DYNSAMPLE_STEP: {
                'value': self._default_sampling_step,
                'validate': self._validate_ufloat
            },
            Parameters.DYNSAMPLE_THRESHOLD: {
                'value': self._threshold_soft_base,
                'validate': self._validate_uint
            },
            Parameters.PROBING_THRESHOLD: {
                'value': self._probing_threshold,
                'validate': self._validate_uint
            },
            Parameters.PROBING_REATTACH: {
                'value': False,
                'validate': self._validate_bool
            },
            Parameters.TIMEDSAMPLE_FREQ: {
                'value': 1,
                'validate': self._validate_uint
            },
            Parameters.DYNBASE_SOFT_THRESHOLD: {
                'value': self._threshold_soft_base,
                'validate': self._validate_uint
            },
            Parameters.DYNBASE_HARD_THRESHOLD: {
                'value': self._threshold_soft_base * self._hard_threshold_coefficient,
                'validate': self._validate_uint
            },
            Parameters.THRESHOLD_MODE: {
                'value': ThresholdMode.SOFT,
                'validate': partial(self._validate_enum, ThresholdMode)
            }
        }

    def __getitem__(self, item):
        """ Allows quick access to parameter values in the param_map

        :param Parameters item: the parameter we want value for

        :return object: the corresponding value
        """
        return self.param_map[item]['value']

    def __setitem__(self, key, value):
        """ Allows to directly set param_map values

        :param Parameters key: the parameter to change
        :param object value: the new value
        """
        self.param_map[key]['value'] = value

    def add_cli_parameter(self, name, value):
        """ Add new CLI parameter to the list of user-supplied arguments.

        :param str name: the string representation of a Parameter
        :param object value: the Parameter value

        :return object or None: the parameter value if the validation is successful, else None
        """
        param = Parameters(name)
        validated = self.param_map[param]['validate'](value)
        if validated is not None:
            self.cli_params.append((param, validated))
            return validated
        return None

    def infer_params(self, call_graph, pipeline, binary):
        """ Attempts to infer sensible default values for the parameters that were not supplied
        by the user. The prediction is done safely in several steps since various modes and
        parameters can affect other parameters as well.

        :param CallGraphResource call_graph: the CGR instance
        :param Pipeline pipeline: the currently selected pipeline
        :param str binary: path to the executable binary
        """
        metrics.start_timer('optimization_parameters')
        func_count, level_count = 0, 0
        if call_graph is not None:
            func_count, level_count = len(call_graph.cg_map.keys()), len(call_graph.levels)
            # Update the default keep top according to the first call graph branching
            self._default_keep_top = call_graph.coverage_max_cut()[1] + 1
        # Extract the user-supplied modes and parameters
        modes = [Parameters.DIFF_CG_MODE, Parameters.CG_SHAPING_MODE, Parameters.THRESHOLD_MODE]
        cli_modes, cli_params = utils.partition_list(
            self.cli_params, lambda param: param[0] in modes
        )

        # Infer general parameters (used in multiple methods) based on the call graph
        self._infer_general_parameters(func_count, level_count)
        # Infer modes used by some methods based on the pipeline
        self._infer_modes(pipeline, cli_modes)
        # Infer the call graph shaping parameters
        self._infer_cg_shaping_parameters(func_count, level_count)
        # Infer the thresholds used by various methods
        self._infer_thresholds()

        # Set user-supplied parameters to override the inferred ones
        for param_name, param_value in cli_params:
            if isinstance(self[param_name], list):
                self[param_name].append(param_value)
            else:
                self[param_name] = param_value

        # Infer the dynamic probing parameters
        self._infer_dynamic_probing(cli_params)
        # Extract source files based on the supplied parameters
        self._extract_sources(binary)
        metrics.end_timer('optimization_parameters')

    def _infer_general_parameters(self, func_count, level_count):
        """ Predicts parameters that are applied across multiple optimization methods.

        :param int func_count: the number of extracted functions
        :param int level_count: the amount of call graph levels
        """
        if func_count == 0 and level_count == 0:
            return
        # Keep the leaf functions if the total number of profiled functions is low
        if func_count <= self._functions_keep_leaves:
            self[Parameters.DIFF_KEEP_LEAF] = True
            self[Parameters.CG_PROJ_KEEP_LEAF] = True
        # Keep-top: 10% of levels, minimum is default
        keep_top = max(math.ceil(level_count * self._keep_top_ratio), self._default_keep_top)
        self[Parameters.STATIC_KEEP_TOP] = keep_top

    def _infer_modes(self, selected_pipeline, user_modes):
        """ Predicts the mode parameters based on the used pipeline.

        :param Pipeline selected_pipeline: the currently selected pipeline
        :param list user_modes: list of pairs with user-specified modes
        """
        self[Parameters.DIFF_CG_MODE] = DiffCfgMode.COLORING
        self[Parameters.CG_SHAPING_MODE] = CGShapingMode.TOP_DOWN
        # The selected pipeline determines the used modes
        if selected_pipeline == Pipeline.BASIC:
            self[Parameters.THRESHOLD_MODE] = ThresholdMode.STRICT
        else:
            self[Parameters.THRESHOLD_MODE] = ThresholdMode.SOFT
        # Apply the user-supplied modes
        for mode_type, mode_value in user_modes:
            self[mode_type] = mode_value

    def _infer_cg_shaping_parameters(self, func_count, level_count):
        """ Predicts the Call Graph Shaping parameters based on the number of functions and levels.

        :param int func_count: the number of extracted functions
        :param int level_count: the amount of call graph levels
        """
        if func_count == 0 and level_count == 0:
            return
        # Determine the number of trimmed levels
        trim_levels = round(level_count * self._levels_ratio)
        # Set the trim levels
        self[Parameters.CG_PROJ_LEVELS] = max(trim_levels, self._default_min_levels)

    def _infer_thresholds(self):
        """ Infer the threshold values based on the selected modes.
        """
        # Determine the thresholds based on the mode
        base = self._threshold_soft_base
        if self[Parameters.THRESHOLD_MODE] == ThresholdMode.STRICT:
            base = self._threshold_strict_base
        # Set the threshold
        self[Parameters.DYNSAMPLE_THRESHOLD] = base
        self[Parameters.DYNBASE_SOFT_THRESHOLD] = base
        self[Parameters.DYNBASE_HARD_THRESHOLD] = base * self._hard_threshold_coefficient

    def _infer_dynamic_probing(self, cli_params):
        """ Predict parameters and threshold values for Dynamic Probing .

        :param list cli_params: a collection of user-supplied parameters
        """
        # Update the probing threshold if reattach is enabled and probing threshold is not set
        probing_threshold_set = Parameters.PROBING_THRESHOLD in [param for param, _ in cli_params]
        if self[Parameters.PROBING_REATTACH] and not probing_threshold_set:
            probing_threshold = self._probing_threshold * self._probing_reattach_coefficient
            self[Parameters.PROBING_THRESHOLD] = probing_threshold

    def _extract_sources(self, binary):
        """ Search for source files of the project in the binary directory, if none are given.

        :param str binary: path to the binary executable
        """
        files, dirs = self[Parameters.SOURCE_FILES], self[Parameters.SOURCE_DIRS]
        # No need to extract if only source files are supplied
        if files and not dirs:
            return
        # If no files or directories are supplied, assume the binary directory contains sources
        if not files and not dirs:
            dirs.append(os.path.dirname(binary))

        # Save the sources
        self[Parameters.SOURCE_FILES] = list(set(_get_source_files(dirs, files)))

    @staticmethod
    def _validate_bool(value):
        """ Bool validation function that accepts boolean values as 1 or 0.

        :param str value: the boolean value to validate
        :return bool or None: the boolean value if the validation is successful
        """
        if value in ['0', '1']:
            return bool(int(value))
        return None

    @staticmethod
    def _validate_uint(value):
        """ Uint validation function.

        :param str value: the uint value to validate
        :return int or None: the uint value if the validation is successful
        """
        try:
            value = int(value)
            if value >= 0:
                return value
            return None
        except ValueError:
            return None

    @staticmethod
    def _validate_ufloat(value):
        """ unsigned float validation function.

        :param str value: the ufloat value to validate
        :return float or None: the ufloat value if the validation is successful
        """
        try:
            value = float(value)
            if value > 0:
                return value
            return None
        except ValueError:
            return None

    @staticmethod
    def _validate_path(path):
        """ Path validation function that takes string and resolves the path.

        :param str path: the path to validate
        :return str or None: fully resolved path if the validation is successful
        """
        if not os.path.exists(os.path.realpath(path)):
            return None
        return os.path.realpath(path)

    @staticmethod
    def _validate_enum(enumclass, value):
        """ Validate if value is supported in the given enum class

        :param Enum enumclass: Enum class
        :param str value: value to check

        :return object or None: Enum item corresponding to the given string value
        """
        return enumclass(value) if value in enumclass.supported() else None

def _get_source_files(dirs, files):
    """ Get all source files in the supplied dirs and files

    :param list dirs: list of directories
    :param list files: list of files

    :return list: list of file paths
    """
    candidate_files = []
    for src in dirs + files:
        if os.path.isdir(src):
            for root, _, files in os.walk(src):
                candidate_files.extend(os.path.join(root, file) for file in files)
        elif os.path.isfile(src):
            candidate_files.append(src)

    return [file for file in candidate_files if os.path.splitext(file)[1] == '.c']


from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("https://python.org")
time.sleep(2)
driver.maximize_window()

time.sleep(2)

# 拖动窗口
#执行javaScript代码
JS = "window.scrollTo(10000,document.body.scrollHeight)"
driver.execute_script(JS)

time.sleep(2)
driver.quit()


from openprocurement.api.validation import validate_cpv_group
from schematics.exceptions import ValidationError
from openprocurement.api.utils import get_now
from openprocurement.tender.core.constants import CPV_ITEMS_CLASS_FROM

class TenderCPVItemsValidate(object):
    def __init__(self, tender):
        self.context = tender

    def __call__(self, cls, data, items):
        cpv_336_group = items[0].classification.id[:3] == "336" if items else False
        if (
            not cpv_336_group
            and (data.get("revisions")[0].date if data.get("revisions") else get_now()) > CPV_ITEMS_CLASS_FROM
            and items
            and len(set([i.classification.id[:4] for i in items])) != 1
        ):
            raise ValidationError(u"CPV class of items should be identical")
        else:
            validate_cpv_group(items)


import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
import datetime
from dash.dependencies import Input, Output
import time

import pandas as pd
import plotly.express as px

from app import app
from components import range_slider as RangeSlider, default_card, country_dropdown
from helpers import time_helper

df_clean = None

def layout(data):
    global df_clean
    df_clean = data.sort_values(by='date')

    return html.Div(children=[
        dbc.Alert("Overzicht totale omzet per land", color="info",
                  style={'text-align': 'center'}),
        html.Div(
            country_dropdown.show(
                dropdownId="country-rev-select",
                placeholder="Selecteer een of meerdere landen",
                df_clean=data,
                labelsColumn='country_name',
                valuesColumn='country_name'
            )
        ),
        html.Br(),
        dcc.Tabs(id='country-rev-tabs', value='tab-1', children=[
            dcc.Tab(label='Cirkel diagram',
                    value='tab-1'),
            dcc.Tab(label='Lijn diagram', value='tab-2')]),
        html.Div(id='country-rev-tabs-content'),
        html.Br(),
        default_card.showCard(
            RangeSlider.show(df_clean, 'country-rev-date-slider')
        )
    ])

@app.callback(Output('country-rev-tabs-content', 'children'),
              [Input('country-rev-tabs', 'value')])
def render_country_rev_content(tab):
    global df_clean

    if tab == 'tab-1':
        countryPieChart = px.pie(
            df_clean['omzet']
            .groupby(df_clean['country_name'])
            .sum()
            .reset_index(),
            values='omzet',
            names='country_name'
        )
        countryPieChart.update_traces(
            textposition='inside',
            textinfo='percent+label'
        )

        return html.Div([
            dcc.Graph(id='country-rev-pie',
                      figure=countryPieChart)
        ])

    elif tab == 'tab-2':
        return html.Div([
            dcc.Graph(id="country-rev-line",
                      figure=px.line(
                        df_clean.groupby([df_clean.date.dt.strftime(
                            '%Y-%m-%d'), df_clean.country_name])['omzet']
                        .sum().reset_index(), x='date', y='omzet', color='country_name'))
        ])

@app.callback(
    Output('country-rev-pie', 'figure'),
    [Input('country-rev-select', 'value'),
     Input('country-rev-date-slider', 'value')])
def update_country_rev_fig_pie(countries_selected, between_date):
    country_rev_df = df_clean

    if between_date is not None:
        country_rev_df = country_rev_df[
            df_clean['date'].between(time_helper.unix_to_datetime(between_date[0]),
                                     time_helper.unix_to_datetime(between_date[1]))]

    if countries_selected is not None and len(countries_selected) > 0:
        country_rev_df = country_rev_df.loc[df_clean['country_name'].isin(
            countries_selected)]

    country_rev_df = country_rev_df['omzet'].groupby(
        df_clean['country_name']).sum().reset_index()

    countryPieChart = px.pie(
        country_rev_df, values='omzet', names='country_name')
    countryPieChart.update_traces(
        textposition='inside',
        textinfo='percent+label'
    )

    return countryPieChart

@app.callback(
    Output('country-rev-line', 'figure'),
    [Input('country-rev-select', 'value'),
     Input('country-rev-date-slider', 'value')])
def update_country_rev_fig_line(countries_selected, between_date):
    global df_clean
    country_rev_df = df_clean

    if between_date is not None:
        country_rev_df = country_rev_df[df_clean['date'].between(
            time_helper.unix_to_datetime(between_date[0]), time_helper.unix_to_datetime(between_date[1]))]

    if countries_selected is not None and len(countries_selected) > 0:
        country_rev_df = country_rev_df.loc[df_clean['country_name'].isin(
            countries_selected)]

    country_rev_df = country_rev_df.groupby([df_clean.date.dt.strftime(
        '%Y-%m-%d'), df_clean.country_name])['omzet'].sum().reset_index()

    return px.line(country_rev_df, x='date', y='omzet', color='country_name')


import operator
import pytesseract
import timeit
import difflib
from appenv import app_multiprocessing
from difflib import get_close_matches
from pytesseract import Output
from difflib import SequenceMatcher
from mongo.Ingredient import Ingredient
from mongo.IngredientRead import IngredientRead
from rust.PreProcess import *

class Tesseract:
    def __init__(self, image):
        self.image = image

    def pre_process(self):
        image = self.image
        # image = resize_compress(image)
        image = grayscale(image)
        # image = opening(image)
        image = thresholding(image)
        # image = invert_color(image)
        image = deskew(image)

        return image

    def get_text(self):
        image = self.pre_process()
        user_words_path = __file__.replace('rust/Tesseract.py', 'assets/user-words.txt')
        text = pytesseract.image_to_string(image, lang='por+eng', config='--user-words ' + user_words_path)
        return text.replace('\n', '')

        # load_freq_dawg
        # load_punc_dawg
        # load_number_dawg
        # load_unambig_dawg
        # load_bigram_dawg
        # load_fixed_length_dawgs')

    def get_matches(self):
        async def ingredients_p():
            return Ingredient.objects.values_list('name')
        async def image_text_p():
            image_text = self.get_text().replace(';', ',').replace(':', ',')
            return [text.strip() for text in image_text.split(',')]

        [ingredients, texts] = app_multiprocessing.process_all([ingredients_p(), image_text_p()])
        async def find_best_match(text):
            matches = difflib.get_close_matches(text, ingredients)
            best_match = matches[0] if matches else None
            accuracy = SequenceMatcher(None, best_match, text).ratio() if best_match else 0
            return IngredientRead(name=text, accuracy=accuracy, best_match=Ingredient(name=best_match))

        matches_tasks = [find_best_match(text) for text in texts]
        matches = app_multiprocessing.process_all(matches_tasks)
        matches_names = [match['best_match']['name'] for match in matches if match['best_match']['name']]
        matches_search = Ingredient.objects(name__in=matches_names)

        for match in matches:
            match_search = [search for search in matches_search if search['name'] == match['best_match']['name']]
            if match_search:
                match['best_match'] = match_search[0]

        return matches

    def get_matches_deadline(self, deadline):
        start = timeit.default_timer()
        matches = list(self.get_matches())
        end = timeit.default_timer()
        diff = end - start
        return [match for match in matches if match['accuracy'] > deadline]


import matplotlib.pyplot as plt
import pickle
import click
import os.path
import math

@click.command()
@click.argument('in-file')
@click.argument('out-dir')
def main(in_file, out_dir):
    with open(in_file, 'rb') as f:
        images = pickle.load(f)

    print("Writing {} images to disk.".format(len(images)))

    num_digits = int(math.log(len(images), 10)) + 1
    for n, image in enumerate(images[::100]):
        fig = plt.figure()

        for i in range(16):
            fig.add_subplot(4, 4, i + 1)
            plt.imshow(image[i], cmap='gray')

        plt.savefig(os.path.join(out_dir, "fig_{:0{}}.png".format(n, num_digits)))

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File Name:    偏微分方程模拟扩散.py
Author:       Liheng-Chan
Email:        202111081084@mail.bnu.edu.cn
Time:         2023/5/21
"""

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

def my_laplacian(In):
    Out = -1.0 * In + 0.20*(np.roll(In,1,axis=1)+np.roll(In,-1,axis=1)+np.roll(In,1,axis=0)+np.roll(In,-1,axis=0) ) + \
          0.05*(np.roll(np.roll(In,1,axis=0),1,axis=1)+np.roll(np.roll(In,-1,axis=0),1,axis=1)+np.roll(np.roll(In,1,axis=0),-1,axis=1)+np.roll(np.roll(In,-1,axis=0),-1,axis=1))
    return Out

def load_image(file_path, width):
    image = Image.open(file_path).convert("L")  # Convert to grayscale
    image = image.resize((width, width), Image.ANTIALIAS)
    image_array = np.array(image, dtype=float) / 255.0
    return image_array

if __name__=="__main__":
    f = 0.055       #进料率
    k = 0.062       #去除率
    da = 1.0          #U的扩散率
    db = 0.5        #V的扩散率
    width = 128     #网格大小
    dt = 0.25       #每进行一需要0.25秒
    stoptime = 100.0 #自定义模拟时间

    image_file = "C:/image/image6.png"  # Replace with your image file path

    B = load_image(image_file, width)
    A = np.ones((width, width), dtype=float)
    t = 0

    nframes = 1

    while t < stoptime:
        anew = A + (da*my_laplacian(A) - A*(B*B) + f*(1-A))*dt
        bnew = B + (db*my_laplacian(B) + A*(B*B) - (k+f)*B)*dt

        A = anew
        B = bnew
        t += dt
        nframes += 1

    fig, ax0 = plt.subplots()
    f = ax0.pcolor(B)
    fig.colorbar(f, ax=ax0)

    plt.show()

偏微分方程模拟扩散final.py


import sys
import json
import pickle

# Load the pickle model
with open("C:/Users/Yared/Downloads/SAGE prediction Models/rf_model", "rb") as f:
    model = pickle.load(f)

input_data_array = json.loads(sys.stdin.read())
outputs = []

for input_data in input_data_array:
    feature_names = list(input_data.keys())
    values = [input_data[feature] for feature in feature_names]
    result = model.predict([values])
    outputs.append(result.tolist())

# Print the output predictions to stdout
print(json.dumps(outputs))


from matplotlib.figure import Figure
import numpy as np
import pickle
import matplotlib
import matplotlib.pyplot as plt
import string
import random

def compare_plot(x1: np.ndarray, y1: np.ndarray, x2: np.ndarray, y2: np.ndarray,
                 xlabel: str, ylabel: str, title: str, label1: str, label2: str) -> Figure:
    """Funkcja służąca do porównywania dwóch wykresów typu plot.
    Szczegółowy opis w zadaniu 3.

    Parameters:
    x1(np.ndarray): wektor wartości osi x dla pierwszego wykresu,
    y1(np.ndarray): wektor wartości osi y dla pierwszego wykresu,
    x2(np.ndarray): wektor wartości osi x dla drugiego wykresu,
    y2(np.ndarray): wektor wartości osi x dla drugiego wykresu,
    xlabel(str): opis osi x,
    ylabel(str): opis osi y,
    title(str): tytuł wykresu ,
    label1(str): nazwa serii z pierwszego wykresu,
    label2(str): nazwa serii z drugiego wykresu.

    Returns:
    matplotlib.pyplot.figure: wykres zbiorów (x1,y1), (x2,y2) zgody z opisem z zadania 3
    """
    try:
        if (x1.shape != y1.shape or min(x1.shape) == 0 or x2.shape != y2.shape or min(x2.shape) == 0):
                return None
        fig , ax = plt.subplots()
        ax.plot(x1,y1,'b',label = label1, linewidth = 4)
        ax.plot(x2,y2,'r',label = label2, linewidth = 2)
        ax.legend()
        ax.set( xlabel = xlabel , ylabel = ylabel, title = title ,)
        # fig.show()
        return fig
    except:
        return None

def parallel_plot(x1:np.ndarray,y1:np.ndarray,x2:np.ndarray,y2:np.ndarray,
                  x1label:str,y1label:str,x2label:str,y2label:str,title:str,orientation:str):
    """Funkcja służąca do stworzenia dwóch wykresów typu plot w konwencji subplot wertykalnie lub chorycontalnie.
    Szczegółowy opis w zadaniu 5.

    Parameters:
    x1(np.ndarray): wektor wartości osi x dla pierwszego wykresu,
    y1(np.ndarray): wektor wartości osi y dla pierwszego wykresu,
    x2(np.ndarray): wektor wartości osi x dla drugiego wykresu,
    y2(np.ndarray): wektor wartości osi x dla drugiego wykresu,
    x1label(str): opis osi x dla pierwszego wykresu,
    y1label(str): opis osi y dla pierwszego wykresu,
    x2label(str): opis osi x dla drugiego wykresu,
    y2label(str): opis osi y dla drugiego wykresu,
    title(str): tytuł wykresu,
    orientation(str): parametr przyjmujący wartość '-' jeżeli subplot ma posiadać dwa wiersze albo '|' jeżeli ma posiadać dwie kolumny.

    Returns:
    matplotlib.pyplot.figure: wykres zbiorów (x1,y1), (x2,y2) zgody z opisem z zadania 5
    """
    try:
        if (x1.shape != y1.shape or  min(x1.shape)==0 or x2.shape != y2.shape or  min(x2.shape)==0 or (orientation not in ['|','-'])):
            return None
        subplot_arg : str = "21" if orientation == '|' else "12"
        fig : Figure
        if orientation == '|' :
            fig = plt.figure(figsize=(8,24))
        else:
            fig = plt.figure(figsize=(24,8))

        ax1 = plt.subplot(int(subplot_arg + "1"))
        ax1.plot(x1,y1)
        ax1.set(xlabel = x1label, ylabel=y1label)
        ax2 = plt.subplot(int(subplot_arg + "2"))
        ax2.plot(x2 , y2)
        ax2.set(xlabel = x2label , ylabel = y2label)
        fig.suptitle(title , fontsize = 32)
        # fig.show()

    except:
        return None

def log_plot(x:np.ndarray,y:np.ndarray,xlabel:np.ndarray,ylabel:str,title:str,log_axis:str):
    """Funkcja służąca do tworzenia wykresów ze skalami logarytmicznymi.
    Szczegółowy opis w zadaniu 7.

    Parameters:
    x(np.ndarray): wektor wartości osi x,
    y(np.ndarray): wektor wartości osi y,
    xlabel(str): opis osi x,
    ylabel(str): opis osi y,
    title(str): tytuł wykresu ,
    log_axis(str): wartość oznacza:
        - 'x' oznacza skale logarytmiczną na osi x,
        - 'y' oznacza skale logarytmiczną na osi y,
        - 'xy' oznacza skale logarytmiczną na obu osiach.

    Returns:
    matplotlib.pyplot.figure: wykres zbiorów (x,y) zgody z opisem z zadania 7
    """
    try:
        if (x.shape != y.shape or  min(x.shape)==0 or (log_axis not in ['x','y','xy'])):
            return None
        fig , ax = plt.subplots()
        if log_axis == 'x' :
            ax.set(xscale = "log")
        elif log_axis == 'y':
            ax.set(yscale = "log")
        else:
            ax.set(xscale = "log" , yscale = "log")

        ax.plot(x,y)
        ax.set(xlabel = xlabel, ylabel=ylabel)
        ax.set_title(title)
        # fig.show()
    except:
        return None


import time
import pygame
import logging
from constants import *
from client.utils import *

log = logging.getLogger(__name__)

DEBUG_SERVER_POSITION = 1 << 0
DEBUG_NO_PLAYER = 1 << 1
DEBUG_PREDICTED_POSITION = 1 << 2

DEBUG = DEBUG_SERVER_POSITION | DEBUG_PREDICTED_POSITION
DEBUG = 0

# by how much we predict the position and move to it
PREDICTION = 0

class Player:

    def __init__(self, username, pos, color, fonts):

        self.server_pos = pos
        self.pos = list(self.server_pos)

        self.color = color
        self.username = username

        self.rect = pygame.Rect((0, 0), PLAYER_SIZE)

        # the predicted position
        self.predicted_pos = None

        self.last_server_update = time.time()
        self.fonts = fonts

    def update_state(self, new_server_pos):
        """ Update state from the server

        Rule #1:

        Every time I receive a server update, I make a prediction about where the
        player will be at the next server update. We assume that the direction
        of the player between the 2 server updates will be the same in the next
        update (this will obviously be wrong sometimes, but it will be
        corrected by rule #2)

        Rule #2:

        Whatever happens, I have to get to the predicted position  exactly on
        the next server update.
        """

        self.last_server_update = time.time()

        self.predicted_pos = [
            new_server_pos[0] + (new_server_pos[0] - self.server_pos[0]) * PREDICTION,
            new_server_pos[1] + (new_server_pos[1] - self.server_pos[1]) * PREDICTION
        ]

        self.server_pos = new_server_pos

        # direction = [
        #     arg(new_server_pos[0] - self.server_pos[0]),
        #     arg(new_server_pos[1] - self.server_pos[1])
        # ]
        # self.last_pos = self.pos
        # self.server_pos = new_server_pos

        # predicted_change = [
        #     direction[0] * PLAYER_SPEED * SERVER_REFRESH_RATE,
        #     direction[1] * PLAYER_SPEED * SERVER_REFRESH_RATE,
        # ]

        # self.prediction =

    def update(self):
        """ Update state in between server updates """
        if self.predicted_pos:
            percentage_done = (time.time() - self.last_server_update) / SERVER_REFRESH_RATE
            dx = percentage_done * (self.predicted_pos[0] - self.pos[0])
            dy = percentage_done * (self.predicted_pos[1] - self.pos[1])
            self.pos = [self.pos[0] + dx, self.pos[1] + dy]

        self.rect.left = int(round(self.pos[0]))
        self.rect.top = int(round(self.pos[1]))

    def render(self, surf, srect):
        if self.pos is None:
            log.warning(f"{self} position is None")
            return

        with fontedit(self.fonts.mono, fgcolor=self.color) as font:
            rect = font.get_rect(self.username)
            rect.midbottom = self.rect.midtop
            rect.top -= 10
            font.render_to(surf, rect, self.username)

        if not DEBUG & DEBUG_NO_PLAYER:
            pygame.draw.rect(surf, self.color, self.rect)

        if DEBUG & DEBUG_SERVER_POSITION:
            pygame.draw.rect(surf, self.color,
                             pygame.Rect(self.server_pos, PLAYER_SIZE), 1)

        if DEBUG & DEBUG_PREDICTED_POSITION and self.predicted_pos:
            r = pygame.Rect(self.predicted_pos, PLAYER_SIZE)
            pygame.draw.rect(surf, self.color, r, 1)
            pygame.draw.rect(surf, self.color, r.inflate(-4, -4), 1)

    def __str__(self):
        return f"<c.Player {self.username!r} {self.pos}>"

    def __repr__(self):
        return str(self)


#!/usr/bin/python3

"""
Script that queries the Reddit API and returns the number of
subscribers (not active users, total subscribers) for a given subreddit
"""

import requests

def number_of_subscribers(subreddit):
    """Fetches a subreddits number of subscribers"""

    url = "https://www.reddit.com/r/{}/about.json".format(subreddit)
    headers = {"User-agent": "Mozilla/5.0"}
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        json_data = response.json()
        return json_data["data"]["subscribers"]
    else:
        return 0


# -*- coding:utf-8 -*-
# ！/usr/bin/env python
# @Time : 2023/7/28 14:25
# @Author : waxberry
# @File : 2. Python xlwt 写入 操作Excel（仅限xls格式！）.py
# @Software : PyCharm

import xlwt

# 2.2 使用xlwt创建新表格并写入

# 编写xlwt新表格写入程序:
# 3.2.2 使用xlwt创建新表格并写入
def fun3_2_2():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')
    # 保存
    worksheet.save('新创建的表格.xls')

# 2.3 xlwt 设置字体格式
def fun3_2_3():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 初始化样式
    style = xlwt.XFStyle

    # 为样式创建字体
    font = xlwt.Font()
    font.name = 'Times New Roman'#字体
    font.bold = True # 加粗
    font.underline = True #下划线
    font.italic = True #斜体
    # 设置样式
    style.font = font

    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')
    # 保存
    worksheet.save('新创建的表格.xls')

# 2.4 xlwt 设置列宽
def fun3_2_4():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')

    # 设置列宽
    worksheet.col(0).width = 256*20

    # 保存
    worksheet.save('新创建的表格.xls')

# 2.5 xlwt 设置行高
def fun3_2_5():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')

    # 设置行高
    style = xlwt.easyxf('font:height 360;')# 18pt,类型小初的字号
    row = worksheet.row(0)
    row.set_style(style)

    # 保存
    worksheet.save('新创建的表格.xls')

# 2.6 xlwt 合并列和行
def fun3_2_6():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')

    # 合并第1行到第2行的第0列到第3列
    worksheet.write_merge(1, 2, 0, 3, 'Merge Test')

    # 保存
    worksheet.save('新创建的表格.xls')

# 2.7 xlwt 添加边框
def fun3_2_7():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')

    # 设置边框样式
    borders = xlwt.Borders()# Create Borders

    # May be:   NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR,
    #           MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED,
    #           MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
    # DASHED虚线
    # NO_LINE没有
    # THIN实线
    borders.left = xlwt.Borders.DASHED
    borders.right = xlwt.Borders.DASHED
    borders.top = xlwt.Borders.DASHED
    borders.bottom = xlwt.Borders.DASHED
    borders.left_colour = 0x40
    borders.right_colour = 0x40
    borders.top_colour = 0x40
    borders.bottom_colour = 0x40

    style = xlwt.XFStyle() # Create Style
    style.borders = borders# Add Borders to Style
    worksheet.write(0, 0, '内容1', style)
    worksheet.write(2, 1, '内容2')
    # 保存
    worksheet.save('新创建的表格.xls')

# 2.8 xlwt为单元格设置背景色
def fun3_2_8():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')

    # 创建样式
    pattern = xlwt.Pattern()

    # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN

    # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow,
    # 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow ,
    # almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
    pattern.pattern_fore_colour = 5
    style = xlwt.XFStyle()
    style.pattern = pattern

    # 使用样式
    worksheet.write(2, 1, '内容2', style)

# 2.9 xlwt设置单元格对齐
def fun3_2_9():
    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')
    # 创建新的sheet表
    worksheet = workbook.add_sheet('My New Sheet')
    # 往表格写入内容
    worksheet.write(0, 0, '内容1')
    worksheet.write(2, 1, '内容2')

    # 设置样式
    style = xlwt.XFStyle()
    al = xlwt.Alignment()
    # VERT_TOP = 0x00       上端对齐
    # VERT_CENTER = 0x01    居中对齐（垂直方向上）
    # VERT_BOTTOM = 0x02    低端对齐
    # HORZ_LEFT = 0x01      左端对齐
    # HORZ_CENTER = 0x02    居中对齐（水平方向上）
    # HORZ_RIGHT = 0x03     右端对齐
    al.horz = 0x02# 设置水平居中
    al.vert = 0x01# 设置垂直居中
    style.alignment = al

    # 对齐写入
    worksheet.write(2, 1, '内容2', style)
    # 保存
    worksheet.save('新创建的表格.xls')

tools/Python办公自动化excel/2. Python xlwt 写入 操作Excel（仅限xls格式！）.py


from collections import defaultdict

class Polynomials:
    def __init__(self, equation):
        self.coefs = defaultdict(float)
        parsed_equation = self.parse_equation(equation)
        for exp, coef in parsed_equation.items():
            self.coefs[exp] += coef

    def parse_equation(self, equation):
        equation = equation.replace(' ', '')
        terms = equation.split('+')
        coefs = defaultdict(float)
        for term in terms:
            if '-' in term:
                subterms = term.split('-')
                for subterm in subterms:
                    if subterm:
                        coef, exp = subterm.split('x^')
                        coefs[int(exp)] += float(coef)
            else:
                if term:
                    coef, exp = term.split('x^')
                    coefs[int(exp)] += float(coef)
        return coefs

    def add_term(self, term, sign):
        if not term:
            coef = 0
            exp = 0
        elif "x" not in term:
            coef = float(term) * sign
            exp = 0
        else:
            if 'x^' in term:
                term = term.replace("x^", "")
                exp = int(term)
                coef = sign
            elif 'x' in term:
                if term == 'x':
                    coef = 1
                    exp = 1
                elif term == '-x':
                    coef = -1
                    exp = 1
                else:
                    if 'x' in term:
                        coef = float(term.split('x')[0])
                        exp = 1
                    else:
                        coef = float(term)
                        exp = 0
        if exp in self.coefs:
            self.coefs[exp] += coef
        else:
            if coef==1:
                self.coefs[exp] = 1
            elif coef==-1:
                self.coefs[exp] = -1
            else:
                self.coefs[exp] = coef

    # a function to add two polynomials

    def __add__(self, other):
        new_coefs = defaultdict(float)
        for exp, coef in self.coefs.items():
            new_coefs[exp] += coef
        for exp, coef in other.coefs.items():
            new_coefs[exp] += coef
        return Polynomials(new_coefs)

    def __sub__(self, other):
        result = Polynomials("")
        result.coefs = self.coefs.copy()
        for exp, coef in other.coefs.items():
            if exp in result.coefs:
                result.coefs[exp] -= coef
            else:
                result.coefs[exp] = -coef
        return result

    def __mul__(self, other):
        result = Polynomials("")
        for exp1, coef1 in self.coefs.items():
            for exp2, coef2 in other.coefs.items():
                new_exp = exp1 + exp2
                new_coef = coef1 * coef2
                if new_exp in result.coefs:
                    result.coefs[new_exp] += new_coef
                else:
                    result.coefs[new_exp] = new_coef
        return result

    def __str__(self):
        polynomial = ''
        for exp, coef in sorted(self.coefs.items(), key=lambda x: x[0], reverse=True):
            if coef==0:
                continue
            if exp == 0:
                if coef == 1:
                    polynomial += '+1'
                elif coef == -1:
                    polynomial += '-1'
                else:
                    polynomial += f'{coef}'
            elif exp == 1:
                if coef == 1:
                    polynomial += '+x'
                elif coef == -1:
                    polynomial += '-x'
                elif coef > 0:
                    polynomial += f'+{coef}x'
                else:
                    polynomial += f'{coef}x'
            else:
                if coef == 1:
                    polynomial += f'+x^{exp}'
                elif coef == -1:
                    polynomial += f'-x^{exp}'
                elif coef > 0:
                    polynomial += f'+{coef}x^{exp}'
                else:
                    polynomial += f'{coef}x^{exp}'
        return polynomial

    def derivative(self):
        result = Polynomials("")
        for exp, coef in self.coefs.items():
            if exp != 0:
                new_exp = exp - 1
                new_coef = coef * exp
                result.coefs[new_exp] = new_coef
        return result

    def integral(self):
        result = Polynomials("")
        for exp, coef in self.coefs.items():
            new_exp = exp + 1
            new_coef = coef / (new_exp)
            result.coefs[new_exp] = new_coef
        return result


from flask import Flask,request, render_template

app = Flask(__name__)

@app.route('/')

def index():
    user = {'username': 'Miguel'}
    return render_template('index.html', title='Home', user=user,)

if __name__ == "__main__":
    app.run(debug=True)


from dataloader import Dataloader, RE_Dataset
import argparse
from model import Model
import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.callbacks import ModelCheckpoint, LearningRateMonitor
import os
from sklearn.model_selection import StratifiedKFold
import pandas as pd
from omegaconf import OmegaConf
from transformers import set_seed
import math
from utils import validate_conf

if __name__ == "__main__":
    conf = OmegaConf.load("./config.yaml")

    # conf validation
    validate_conf(conf)

    # seed 고정
    if conf.params.seed > 0:
        set_seed(conf.params.seed)

    # k-fold 여부에 따라 step 수가 달라짐.
    if conf.params.use_stratified_kfold:
        train_df = pd.read_csv(conf.path.origin_train_path)  # 학습 데이터

        # ((num_folds-1) x num_folds) 를 곱해줘야 함.
        # 총 스텝 수 = 학습 데이터 개수 / 배치사이즈 x max_epoch / ((num_folds-1) x num_folds)
        total_steps = math.ceil(
            (len(train_df) // conf.params.batch_size)
            * conf.params.max_epoch
            * ((conf.params.num_folds - 1) / conf.params.num_folds)
        )

    else:
        train_df = pd.read_csv(conf.path.train_path)  # 학습 데이터
        # 총 스텝 수 = 학습 데이터 개수 / 배치사이즈 x max_epoch
        total_steps = (len(train_df) // conf.params.batch_size) * conf.params.max_epoch

    # warmup_ratio 가 음수가 아닌 경우에만 warmup_staps 를 overwrite 합니다.
    if conf.params.warmup_ratio > 0.0:  # float라서 등호(=) 주의
        conf.params.warmup_steps = int(total_steps * conf.params.warmup_ratio)

    model = Model(
        model_name=conf.model_name,
        lr=conf.params.learning_rate,
        num_labels=conf.params.num_labels,
        warmup_steps=conf.params.warmup_steps,
        max_training_step=total_steps,
        loss_type=conf.params.loss_type,
        classifier=conf.params.classifier,
        lr_decay=conf.params.lr_decay,
    )

    wandb_logger = WandbLogger(
        project=conf.params.project_name, name=conf.params.test_name
    )

    checkpoint_callback = ModelCheckpoint(
        monitor="val_micro_f1",
        dirpath="./ckpt",
        filename="roberta-large-emb-lr_sched-{epoch:02d}-{val_micro_f1:.2f}",
        save_top_k=1,
        mode="max",
    )

    lr_monitor = LearningRateMonitor(logging_interval="step")

    os.makedirs("./ckpt", exist_ok=True)

    # StratifiedKFold 사용할 경우
    if conf.params.use_stratified_kfold:
        num_folds = conf.params.num_folds
        skf = StratifiedKFold(
            n_splits=num_folds, shuffle=True, random_state=42
        )  # StratifiedKFold 객체 선언

        train_dataset = pd.read_csv(conf.path.origin_train_path)
        indices = list(range(len(train_dataset)))
        labels = train_dataset["label"].values
        folds = list(
            skf.split(indices, labels)
        )  # labels 를 보고 분포에 맞게 skf 객체가 indices를 나눠준다 (train_index, val_index) 형태로 반환

        # k-fold 만큼 반복
        for fold, (train_index, val_index) in enumerate(folds):
            print(f"Training Fold {fold + 1}/{num_folds}")

            # fold 별로 trainer, dataloader 객체를 새로 만들어준다
            trainer = pl.Trainer(
                accelerator="gpu",
                max_epochs=conf.params.max_epoch,
                log_every_n_steps=1,
                logger=wandb_logger,
                callbacks=[checkpoint_callback, lr_monitor],
            )

            fold_dataloader = Dataloader(
                model_name=conf.model_name,
                batch_size=conf.params.batch_size,
                shuffle=conf.params.shuffle,
                origin_train_path=conf.path.origin_train_path,
                train_path=None,  # StratifiedKFold 시엔 None
                dev_path=None,  # StratifiedKFold 시엔 None
                test_path=None,  # StratifiedKFold 시엔 None
                predict_path=None,  # StratifiedKFold 시엔 None
                emb=conf.params.emb,
                use_stratified_kfold=conf.params.use_stratified_kfold,
                train_indices=train_index,
                val_indices=val_index,
                only_entity=False,  # CoRE predict 시에만 True
            )

            trainer.fit(model=model, datamodule=fold_dataloader)

    else:
        trainer = pl.Trainer(
            accelerator="gpu",
            max_epochs=conf.params.max_epoch,
            log_every_n_steps=1,
            logger=wandb_logger,
            callbacks=[checkpoint_callback, lr_monitor],
        )

        dataloader = Dataloader(
            model_name=conf.model_name,
            batch_size=conf.params.batch_size,
            shuffle=conf.params.shuffle,
            origin_train_path=None,  # StratifiedKFold 시엔 None
            train_path=conf.path.train_path,
            dev_path=conf.path.dev_path,
            test_path=conf.path.test_path,
            predict_path=conf.path.predict_path,
            emb=conf.params.emb,
            use_stratified_kfold=None,  # StratifiedKFold 시엔 None
            train_indices=None,  # StratifiedKFold 시엔 None
            val_indices=None,  # StratifiedKFold 시엔 None
            only_entity=False,  # CoRE predict 시에만 True
        )

        trainer.fit(model=model, datamodule=dataloader)


import numpy
import matplotlib.pyplot
import multiprocessing
import random
import warnings
import time
import centroid_dictionary_builder as cdb
import KTester as kt

alpha = 0.001

def calculate_z(data_array_i, neuron_weights):
    dot_product = -numpy.dot(data_array_i, neuron_weights)
    z_value = 1 / (1 + numpy.exp(dot_product))
    return z_value

def calculate_neuron_weights(data_array_i, neuron_weights, error):
    neuron_weights = neuron_weights + alpha * error * data_array_i
    return neuron_weights

warnings.filterwarnings('ignore')
#numpy.set_printoptions(suppress=True)

if __name__ == "__main__":

    with open('../100centroids/data_array_100centroids.npy', 'rb') as opened_file:
        letter_array = numpy.load(opened_file)

    with open('../100centroids/nonletter_labeled_data_100centroids.npy', 'rb') as opened_file:
        non_letter_array = numpy.load(opened_file)

    letter_centroid_data = letter_array[:,-1] #get centroid labels for datasets
    non_letter_centroid_data = non_letter_array[:,-1]

    #half the letter data by truncating half of each letter's data set
    #letter_centroid_data = numpy.reshape(letter_centroid_data, (62, int(len(letter_centroid_data) / 62)))
    #letter_centroid_data = letter_centroid_data[:,:-int((len(letter_centroid_data[0])/2))]
    #letter_centroid_data = numpy.ndarray.flatten(letter_centroid_data)

    #put 8x8 samples back into 32x32 images
    letter_centroid_data = numpy.reshape(letter_centroid_data,(int(len(letter_centroid_data)/16), 16))
    non_letter_centroid_data = numpy.reshape(non_letter_centroid_data, (int(len(non_letter_centroid_data) / 16), 16))

    # put letter/nonletter centroid data through translator
    letter_data = cdb.build_translated_letter_centroid_labels(letter_centroid_data)
    non_letter_data = cdb.build_translated_letter_centroid_labels(non_letter_centroid_data)

    data_array = numpy.concatenate((letter_data, non_letter_data), axis=0)

    Bias = numpy.full((len(data_array),1), 1)

    Class_one = numpy.full((len(letter_data), 1), 1)
    Class_two = numpy.full((len(non_letter_data), 1), 0)
    Classes = numpy.concatenate((Class_one, Class_two), axis=0)

    #concatenate Bias and Classes to a super letter_array
    data_array = numpy.concatenate((Bias, data_array), axis=1)
    data_array = numpy.concatenate((data_array, Classes), axis=1)

    #shuffle data array for training
    indexArray = list(range(numpy.size(data_array, 0)))
    random.shuffle(indexArray)
    data_array = data_array[indexArray]

    #letter_array = letter_array[:25000]

    #Code to count many letters/non-letters were randomly selected
    count_non = 0
    count_letter = 0

    for i in data_array:
        if i[-1] == 0:
            count_non += 1
        else:
            count_letter += 1

    print("Non-letters:", count_non)
    print("Letters:", count_letter)

    # Splitting up image and classifier data into their own arrays
    data_classifiers = data_array.T[-1]
    data_array = data_array[:, :-1]

    # define number of hidden layers
    NEURONS = 50 + 1  # Extra 1 weight for bias
    ATTRIBUTE_COUNT = len(data_array[0])  # bias already added

    # weights pre-initialized
    neuron_weights = numpy.random.rand(NEURONS * (ATTRIBUTE_COUNT))
    neuron_weights = neuron_weights * 2 - 1
    neuron_weights = neuron_weights.reshape((NEURONS, ATTRIBUTE_COUNT))
    output_weights = numpy.random.rand(NEURONS)
    output_weights = output_weights * 2 - 1

    pool = multiprocessing.Pool(multiprocessing.cpu_count() - 1)
    print("\nLogical Threads being used:", multiprocessing.cpu_count() - 1)

    # MLP Training
    EPOCHS = 100

    previous_error = 0
    same_error_count = 0
    total_error_list = []

    start = time.time()  # Start Timer

    for epoch in range(EPOCHS):
        total_error = 0
        print("Progress:", str(epoch * 100 / EPOCHS) + "%")
        for i in range(len(data_array)):

            # forward calculation
            hidden_layer_z = numpy.ones(NEURONS)

            # zip and send data to function for z values
            data_array_i = numpy.full((NEURONS, len(data_array[i, :])), data_array[i])
            values = list(zip(data_array_i, neuron_weights))
            return_value = pool.starmap(calculate_z, values)

            if return_value:
                hidden_layer_z = numpy.array(return_value)
            return_value.clear()
            values.clear()

            dot_product_output = -numpy.dot(hidden_layer_z, output_weights)
            output_z = 1 / (1 + numpy.exp(dot_product_output))

            # prediction
            prediction = round(output_z, 0)

            total_error = total_error + abs(data_classifiers[i] - prediction)

            # error propagation
            output_error = output_z * (1 - output_z) * (data_classifiers[i] - output_z)
            error = hidden_layer_z * (1 - hidden_layer_z) * (output_error * output_weights)

            # update weights
            values = list(zip(data_array_i, neuron_weights, error))
            return_value = pool.starmap(calculate_neuron_weights, values)

            if return_value:
                neuron_weights = numpy.array(return_value)
            return_value.clear()
            values.clear()

            output_weights = output_weights + alpha * output_error * hidden_layer_z

        total_error_list.append(total_error)

        if previous_error != total_error_list[-1]:
            previous_error = total_error_list[-1]
            same_error_count = 0
        else:
            same_error_count += 1

        if same_error_count > 3:
            print("Training Completed.")
            break

    end = time.time()

    numpy.save("neuron_weights", neuron_weights)
    numpy.save("output_weights", output_weights)

    print("Number of errors during training:", total_error_list[-1])
    print("Percent of errors during training:", total_error_list[-1] / len(data_array) * 100, "%")

    matplotlib.pyplot.plot(total_error_list)
    matplotlib.pyplot.title(label=f"Alpha: {alpha}")
    matplotlib.pyplot.show()

    #with open('neuron_weights.npy', 'rb') as opened_file:
    #    neuron_weights = numpy.load(opened_file)

    #with open('output_weights.npy', 'rb') as opened_file:
    #    output_weights = numpy.load(opened_file)

    #print("neuron_weights:", neuron_weights)
    #print("output_weights:", output_weights)
    print("Training Time:", end - start)

    time_string = "Training Time: " + str(end - start)

    # open text file
    text_file = open("../TrainingTime.txt", "w")
    text_file.write(time_string)
    text_file.close()

Deprecated Files/MLP Training Binary Output.py


from time import time
from typing import Any
from core.db_manager import DBManager
from bson import ObjectId
from dataclasses import dataclass, is_dataclass, field, asdict

def from_dict_utils(cls, ndict):
    d2 = {}
    for key in cls.__dataclass_fields__.keys():
        if key not in ndict:
            continue
        value = cls.__dataclass_fields__[key].type
        if ndict.get(key) is not None and value in [str,float,int]:
            d2[key] = value(ndict.get(key))
        elif value == bool:
            if ndict.get(key) in ["true", "True", True]:
                d2[key] = True
            elif ndict.get(key) in ["false", "False", False]:
                d2[key] = False
        elif ndict.get(key) is not None and is_dataclass(value):
            d2[key] = from_dict_utils(value, ndict.get(key))
        else:
            d2[key] = ndict.get(key)
    if "_id" in ndict and "_id" not in d2:
        d2["_id"] = ndict.get("_id")

    return d2

@dataclass
class Entity():
    created_at: float = field(default_factory=time)
    updated_at: float = field(default_factory=time)
    deleted: bool = False
    _id: ObjectId = field(default_factory=ObjectId)

    @classmethod
    def from_dict(cls, ndict):
        if ndict is None:
            return None
        d2 = from_dict_utils(cls, ndict)
        return cls(**d2)

    @classmethod
    def _db(cls):
        db_manager = DBManager.get_instance()
        return db_manager.get_collection(cls)

    @classmethod
    def find_one(cls, params, keys=[], deleted=False):
        db = cls._db()
        is_object = isinstance(params, dict)
        if not is_object and not ObjectId.is_valid(params):
            params = {"_id": params}
        elif not is_object:
            params = {"_id": ObjectId(params)}
        if not deleted:
            params.update({"deleted":{"$ne":True}})
        if len(keys) == 0:
            obj = db.find_one(params)
        else:
            obj = db.find_one(params, keys)
        return cls.from_dict(obj)

    @classmethod
    def find_many(cls, params, keys=[], deleted=False, limit=0, sort=None):
        db = cls._db()

        if not isinstance(params, dict):
            olist = [ObjectId(_id) if ObjectId.is_valid(_id) else _id for _id in params ]
            params = {"_id":{"$in": olist}}
        if not deleted:
            params.update({"deleted":{"$ne":True}})

        cur = db.find(filter=params, projection=keys, limit=limit, sort=sort)

        return [cls.from_dict(obj) for obj in cur]

    def update(self, keys=[]):
        db = self._db()
        self.updated_at = time()
        ndict = asdict(self)
        if (len(keys) == 0):
            db.update_one({"_id": self._id}, {"$set": ndict},  upsert=True)
        else:
            d2 = {x:ndict.get(x) for x  in keys if x in ndict}
            if (len(d2) >0):
                db.update_one({"_id": self._id}, {"$set": d2},  upsert=True)

    def delete(self, delete_from_db = False):
        db = self._db()
        if (delete_from_db):
            db.delete_one({"_id": self._id})
        else:
            db.update_one({"_id": self._id},{"$set":{"deleted":True}}, upsert = False)

    def update_from_dict(self, params):
        d2 = {x:params.get(x) for x  in self.__dataclass_fields__.keys() if x in params}
        for x,y in d2.items():
            if x in self.__dict__:
                value = self.__dataclass_fields__[x].type

                if  y is not None and value in [str,float,int]:
                    self.__dict__[x] = value(y)
                elif value == bool:
                    if y in ["true", "True", True]:
                        self.__dict__[x] = True
                    elif y in ["false", "False", False]:
                        self.__dict__[x] = False
                elif is_dataclass(y):
                    self.__dict__[x] = from_dict_utils(y, params.get(x))
                else:
                    self.__dict__[x] = y

    def update_document_from_dict(self, params):
        self.update_from_dict(params)
        self.update()

    @classmethod
    def count_documents(cls, params, ):
        return cls._db().count_documents(params)

    def get_dict(self):
        return asdict(self)


from gpiozero import LED, Button
from time import time, sleep
import os

class Rullgardin:
    def __init__(self, a1, a2, en, knapp, tid, index):
        self.a1 = LED(a1)
        self.a2 = LED(a2)
        self.en = LED(en)
        self.knapp = Button(knapp)
        self.tid = tid
        self.index = str(index)
        self.state = "Unknown"

    def off(self):
        self.en.off()
        self.a1.off()
        self.a2.off()

    def upp(self):
        if self.state != "Uppe":
            self.state = "Pvg upp"
            self.off()
            self.a2.on()
            self.en.on()
            self.knapp.wait_for_press()
            self.off()
            sleep(0.1)
            self.off()
            self.a1.on()
            self.en.on()
            sleep(0.2)
            self.off()
            if self.state != "Avbruten":
                self.state = "Uppe"

    def ner(self):
        if self.state != "Nere":
            self.state = "Pvg ner"
            self.off()
            self.a1.on()
            self.en.on()
            for i in range(int(self.tid/0.1)):
                sleep(0.1)
                if self.state == "Avbruten":
                    break
            if self.state != "Avbruten":
                self.off()
                self.state = "Nere"

def gardiner():
    file = open("/home/pi/Rullgardin/gardiner.txt", encoding="utf8")
    file_rader = file.readlines()
    rullgardiner = []
    for rad in file_rader:
        rullgardiner.append(rad.rstrip("\n"))
    rullgardiner.pop(0)
    gardiner = []
    for gardin in rullgardiner:
        pinnar = gardin.split()
        index = 1 + rullgardiner.index(gardin)
        a1 = pinnar[0]
        a2 = pinnar[1]
        en = pinnar[2]
        knapp = pinnar[3]
        tid = float(pinnar[4])
        gardiner.append(Rullgardin(a1, a2, en, knapp, tid, index))
    return gardiner


import datetime, sys, dynamic, bruteforce, backtracking, firstBranchBound

def run():
	path = input("Which method do you want to use? \nBrute force (1), Dynamic (2), Best First Branch Bound (3), or Backtracking (4)?\nOr press q to quit  : ")

	if path == '1':
		time1 = datetime.datetime.now()
		print( bruteforce.bruteforce(W, items[2], items[1], len(items[0])) )
		time2 = datetime.datetime.now()
		tdelta = time2 - time1
		print("Brute force: Time taken: %s ms" % int(tdelta.total_seconds()*1000) )
		print("\n")
		run()

	elif path == '2':
		time1 = datetime.datetime.now()
		print( dynamic.dynamic(W, items[2], items[1], len(items[0])) )
		time2 = datetime.datetime.now()
		tdelta = time2 - time1
		print("Dynamic: Time taken: %s ms" % int(tdelta.total_seconds()*1000) )
		print("\n")
		run()

	elif path == '3':
		time1 = datetime.datetime.now()
		print( firstBranchBound.firstBranchBound(W, items[2], items[1], len(items[0])) )
		time2 = datetime.datetime.now()
		tdelta = time2 - time1
		print("Best First Branch Bound: Time taken: %s ms" % int(tdelta.total_seconds()*1000) )
		print("\n")
		run()

	elif path == '4':
		time1 = datetime.datetime.now()
		print( backtracking.backtracking(W, items[2], items[1], len(items[0])) )
		time2 = datetime.datetime.now()
		tdelta = time2 - time1
		print("Backtracking: Time taken: %s ms" % int(tdelta.total_seconds()*1000) )
		print("\n")
		run()

	elif path == 'q':
		sys.exit("See ya!")

	else:
		print("Error: no valid option chosen. Please try again.")
		print("\n")
		run()

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

# Main program, load items

n = int(input("Enter the number of items : "))
W = int(input("Enter total weight : "))

#init 2-dimensional array for items
items = [[0]*4 for i in range(n)]    # create 2-dimensional array

for i in range(n):
	items[i][0] = i+1
	items[i][1] = int(input("Enter profit for %s : " % (i+1) ) )
	items[i][2] = int(input("Enter weight for %s : " % (i+1) ) )
	items[i][3] = int(items[i][1] / items[i][2])

print()
print("Items:  %s" % items)

run()


import gradio as gr
import joblib
import pandas as pd
import random

# Load the pipeline
pipeline = joblib.load('pipeline.joblib')
X_test_smaller = joblib.load('X_test_smaller.joblib')

# Define input components
last_pymnt_amnt = gr.inputs.Number(label='Last Payment Amount')
dti = gr.inputs.Number(label='DTI (Debt-to-Income Ratio)')
mo_sin_old_rev_tl_op = gr.inputs.Number(
    label='Months since Oldest Revolving Account')
int_rate = gr.inputs.Number(label='Interest Rate')
revol_util = gr.inputs.Number(label='Revolving Utilization Rate')
bc_open_to_buy = gr.inputs.Number(label='Credit Line Balance')
revol_bal = gr.inputs.Number(label='Revolving Balance')
avg_cur_bal = gr.inputs.Number(label='Average Current Balance')
total_bal_ex_mort = gr.inputs.Number(label='Total Balance Excluding Mortgage')
annual_inc = gr.inputs.Number(label='Annual Income')
loan_amnt = gr.inputs.Number(label='Loan Amount')

def greet():
    return "Hello!"

generate_button = gr.Button(value="Generate Random Row")

# Combine input components
input_components = [last_pymnt_amnt, dti, mo_sin_old_rev_tl_op, int_rate, revol_util,
                    bc_open_to_buy, revol_bal, avg_cur_bal, total_bal_ex_mort,
                    annual_inc, loan_amnt, generate_button]

# Define prediction function
def predict_loan_approval(last_pymnt_amnt, dti, mo_sin_old_rev_tl_op, int_rate, revol_util,
                          bc_open_to_buy, revol_bal, avg_cur_bal, total_bal_ex_mort,
                          annual_inc, loan_amnt, generate_random_row):
    if generate_random_row:
        random_index = random.randint(0, len(X_test_smaller) - 1)
        # Get the row at the random index
        random_row = X_test_smaller.iloc[random_index]
        # Fill input values with random data
        last_pymnt_amnt = random_row['last_pymnt_amnt']
        dti = random_row['dti']
        mo_sin_old_rev_tl_op = random_row['mo_sin_old_rev_tl_op']
        int_rate = random_row['int_rate']
        revol_util = random_row['revol_util']
        bc_open_to_buy = random_row['bc_open_to_buy']
        revol_bal = random_row['revol_bal']
        avg_cur_bal = random_row['avg_cur_bal']
        total_bal_ex_mort = random_row['total_bal_ex_mort']
        annual_inc = random_row['annual_inc']
        loan_amnt = random_row['loan_amnt']

        input_data = {
        'last_pymnt_amnt': last_pymnt_amnt,
        'dti': dti,
        'mo_sin_old_rev_tl_op': mo_sin_old_rev_tl_op,
        'int_rate': int_rate,
        'revol_util': revol_util,
        'bc_open_to_buy': bc_open_to_buy,
        'revol_bal': revol_bal,
        'avg_cur_bal': avg_cur_bal,
        'total_bal_ex_mort': total_bal_ex_mort,
        'annual_inc': annual_inc,
        'loan_amnt': loan_amnt
        }

        input_df = pd.DataFrame([input_data])
        prediction = pipeline.predict(input_df)
        predict_proba = pipeline.predict_proba(input_df)

        if prediction[0] == 1:
            return f'Fully paid with: {round(predict_proba[0][1] * 100, 2)}% probability \n\nRandomly Generated Row:\n {random_row}'
        else:
            return f'Charged Off with: {round(predict_proba[0][0] * 100, 2)}% probability \nRandomly Generated Row:\n {random_row}'

    else:
        input_data = {
            'last_pymnt_amnt': last_pymnt_amnt,
            'dti': dti,
            'mo_sin_old_rev_tl_op': mo_sin_old_rev_tl_op,
            'int_rate': int_rate,
            'revol_util': revol_util,
            'bc_open_to_buy': bc_open_to_buy,
            'revol_bal': revol_bal,
            'avg_cur_bal': avg_cur_bal,
            'total_bal_ex_mort': total_bal_ex_mort,
            'annual_inc': annual_inc,
            'loan_amnt': loan_amnt
        }

        input_df = pd.DataFrame([input_data])
        prediction = pipeline.predict(input_df)
        predict_proba = pipeline.predict_proba(input_df)

        if prediction[0] == 1:
            return f'Fully paid with {round(predict_proba[0][1] * 100, 2)}% probability'
        else:
            return f'Charged Off with {round(predict_proba[0][0] * 100, 2)}% probability'

# Create the Gradio interface
iface = gr.Interface(
    fn=predict_loan_approval,
    inputs=input_components,
    outputs='text',
    title='Loan Approval Predictor',
    description='If you are using the random row generator, click the button to generate a random row. Then click the Submit button to make a prediction.'
)
iface.launch(server_port=7000, server_name='0.0.0.0')


"""

A* grid planning

author: Atsushi Sakai(@Atsushi_twi)
        Nikos Kanargias (nkana@tee.gr)

See Wikipedia article (https://en.wikipedia.org/wiki/A*_search_algorithm)

"""

import math

import matplotlib.pyplot as plt

show_animation = True

class AStarPlanner:

    # def __init__(self, ox, oy, resolution, rr):
    #     """
    #     Initialize grid map for a star planning

    #     ox: x position list of Obstacles [m]
    #     oy: y position list of Obstacles [m]
    #     resolution: grid resolution [m]
    #     rr: robot radius[m]
    #     """

    #     self.resolution = resolution
    #     self.rr = rr
    #     self.min_x, self.min_y = 0, 0
    #     self.max_x, self.max_y = 0, 0
    #     self.obstacle_map = None
    #     self.x_width, self.y_width = 0, 0
    #     self.motion = self.get_motion_model()
    #     self.calc_obstacle_map(ox, oy)

    def __init__(self, ox, oy, resolution, rr = None, min_x = None, min_y = None, max_x = None, max_y = None, list_x = None, list_y = None, list_z = None):
        """
        Initialize grid map for a star planning

        Args:
            ox: x position list of Obstacles [m]
            oy: y position list of Obstacles [m]
            resolution: grid resolution [m]
            rr (optional): robot radius[m]. If an obstacle is within robot radius of a cell, the robot cannot go through this cell. If None, only the cell containing the obstacle is set as obstacle cell. Defaults to None.
            min_x : Minimum x value in the map [m]
            min_y : Minimum y value in the map [m]
            max_x : Maximum x value in the map [m]
            max_y : Maximum y value in the map [m]
            list_x: x position list for height values [m]
            list_y: y position list for height values [m]
            list_z: z height list for height values [m]
        """
        # if rr is specified, use calc_obstacle_map to build the map. Time consuming.
        if rr is not None:
            self.resolution = resolution
            self.rr = rr
            self.min_x, self.min_y = 0, 0
            self.max_x, self.max_y = 0, 0
            self.obstacle_map = None
            self.x_width, self.y_width = 0, 0
            self.motion = self.get_motion_model()
            self.calc_obstacle_map(ox, oy)
        else:
            self.resolution = resolution
            self.rr = rr
            self.min_x, self.min_y = min_x, min_y
            self.max_x, self.max_y = max_x, max_y

            print("min_x:", self.min_x)
            print("min_y:", self.min_y)
            print("max_x:", self.max_x)
            print("max_y:", self.max_y)
            self.obstacle_map = None
            # self.x_width, self.y_width = 0, 0
            self.motion = self.get_motion_model()
            self.x_width = round((self.max_x - self.min_x) / self.resolution) + 1
            self.y_width = round((self.max_y - self.min_y) / self.resolution) + 1
            print("resolution:", self.resolution)
            print("x_width:", self.x_width)
            print("y_width:", self.y_width)

            # obstacle map generation
            self.obstacle_map = [[False for _ in range(self.y_width)]
                                    for _ in range(self.x_width)]
            # print(len(self.obstacle_map))
            # print(len(self.obstacle_map[0]))

            for iox, ioy in zip(ox, oy):
                # print(self.calc_xy_index(iox, self.min_x))
                # print(self.calc_xy_index(ioy, self.min_y))
                self.obstacle_map[self.calc_xy_index(iox, self.min_x)][self.calc_xy_index(ioy, self.min_y)] = True

        # height
        self.height = [[0 for _ in range(self.y_width)]
                                for _ in range(self.x_width)]
        if list_z is not None:
            for ix, iy, iz in zip(list_x, list_y, list_z):
            # print(self.calc_xy_index(iox, self.min_x))
            # print(self.calc_xy_index(ioy, self.min_y))
                self.height[self.calc_xy_index(ix, self.min_x)][self.calc_xy_index(iy, self.min_y)] = iz

    class Node:
        def __init__(self, x, y, cost, parent_index):
            self.x = x  # index of grid
            self.y = y  # index of grid
            self.cost = cost
            self.parent_index = parent_index

        def __str__(self):
            return str(self.x) + "," + str(self.y) + "," + str(
                self.cost) + "," + str(self.parent_index)

    def is_in_obstacle_cell(self, x, y):
        # ind_x = self.calc_xy_index(x, self.min_x)
        # ind_x = self.calc_xy_index(x, self.min_y)
        return self.obstacle_map[self.calc_xy_index(x, self.min_x)][self.calc_xy_index(y, self.min_y)]

    def reset(self, ox_reset, oy_reset):
        """
        Reset some cells as non-obstacle

        Args:
            ox_reset (list of float): the list of x position to be reset
            oy_reset (list of float): the list of x position to be reset
        """
        for x,y in zip(ox_reset, oy_reset):
            self.obstacle_map[self.calc_xy_index(x, self.min_x)][self.calc_xy_index(y, self.min_y)] = False

    def planning(self, sx, sy, gx, gy):
        """
        A star path search

        input:
            s_x: start x position [m]
            s_y: start y position [m]
            gx: goal x position [m]
            gy: goal y position [m]

        output:
            rx: x position list of the final path
            ry: y position list of the final path
            path_length: the length of the final path
            3D_path_length: the 3D length of the final path
        """

        start_node = self.Node(self.calc_xy_index(sx, self.min_x),
                               self.calc_xy_index(sy, self.min_y), 0.0, -1)
        goal_node = self.Node(self.calc_xy_index(gx, self.min_x),
                              self.calc_xy_index(gy, self.min_y), 0.0, -1)

        open_set, closed_set = dict(), dict()
        open_set[self.calc_grid_index(start_node)] = start_node

        while 1:
            if len(open_set) == 0:
                # print("Open set is empty..")
                break

            c_id = min(
                open_set,
                key=lambda o: open_set[o].cost + self.calc_heuristic(goal_node,
                                                                     open_set[
                                                                         o]))
            current = open_set[c_id]

            # show graph
            if show_animation:  # pragma: no cover
                plt.plot(self.calc_grid_position(current.x, self.min_x),
                         self.calc_grid_position(current.y, self.min_y), "xc")
                # for stopping simulation with the esc key.
                plt.gcf().canvas.mpl_connect('key_release_event',
                                             lambda event: [exit(
                                                 0) if event.key == 'escape' else None])
                if len(closed_set.keys()) % 10 == 0:
                    plt.pause(0.001)

            if current.x == goal_node.x and current.y == goal_node.y:
                # print("Find goal")
                goal_node.parent_index = current.parent_index
                goal_node.cost = current.cost
                break

            # Remove the item from the open set
            del open_set[c_id]

            # Add it to the closed set
            closed_set[c_id] = current

            # expand_grid search grid based on motion model
            for i, _ in enumerate(self.motion):
                node = self.Node(current.x + self.motion[i][0],
                                 current.y + self.motion[i][1],
                                 current.cost + self.motion[i][2], c_id)
                n_id = self.calc_grid_index(node)

                # If the node is not safe, do nothing
                if not self.verify_node(node):
                    continue

                if n_id in closed_set:
                    continue

                if n_id not in open_set:
                    open_set[n_id] = node  # discovered a new node
                else:
                    if open_set[n_id].cost > node.cost:
                        # This path is the best until now. record it
                        open_set[n_id] = node

        rx, ry = self.calc_final_path(goal_node, closed_set)
        cost_2D = goal_node.cost * self.resolution
        cost_3D = self.calc_final_path_3D_cost(goal_node, closed_set)
        return rx, ry, cost_2D, cost_3D

    def calc_final_path(self, goal_node, closed_set):
        # generate final course
        rx, ry = [self.calc_grid_position(goal_node.x, self.min_x)], [
            self.calc_grid_position(goal_node.y, self.min_y)]
        parent_index = goal_node.parent_index
        while parent_index != -1:
            n = closed_set[parent_index]
            rx.append(self.calc_grid_position(n.x, self.min_x))
            ry.append(self.calc_grid_position(n.y, self.min_y))
            parent_index = n.parent_index

        return rx, ry

    def get_height_grid_index(self, ind_x, ind_y):
        return self.height[ind_x][ind_y]

    # def get_3D_distance_two_nodes():

    def calc_final_path_3D_cost(self, goal_node, closed_set):
        cost_3D = 0.0
        pre_n = goal_node

        parent_index = goal_node.parent_index
        while parent_index != -1:
            n = closed_set[parent_index]
            horizontal_cost = math.sqrt(math.pow(pre_n.x - n.x,2) + math.pow(pre_n.y - n.y,2)) * self.resolution
            vertical_cost = self.get_height_grid_index(pre_n.x, pre_n.y) - self.get_height_grid_index(n.x, n.y)
            cost_3D += math.sqrt(math.pow(horizontal_cost, 2) + math.pow(vertical_cost, 2))
            parent_index = n.parent_index
            pre_n = n
        return cost_3D

    @staticmethod
    def calc_heuristic(n1, n2):
        w = 1.0  # weight of heuristic
        d = w * math.hypot(n1.x - n2.x, n1.y - n2.y)
        return d

    def calc_grid_position(self, index, min_position):
        """
        calc grid position

        :param index:
        :param min_position:
        :return:
        """
        pos = index * self.resolution + min_position
        return pos

    def calc_xy_index(self, position, min_pos):
        return round((position - min_pos) / self.resolution)

    def calc_grid_index(self, node):
        # return (node.y - self.min_y) * self.x_width + (node.x - self.min_x)
        return node.y * self.x_width + node.x

    def verify_node(self, node):
        px = self.calc_grid_position(node.x, self.min_x)
        py = self.calc_grid_position(node.y, self.min_y)
        # print("px, py:{}, {}".format(px, py))

        if px < self.min_x:
            return False
        elif py < self.min_y:
            return False
        elif px >= self.max_x:
            return False
        elif py >= self.max_y:
            return False

        # collision check
        if self.obstacle_map[node.x][node.y]:
            return False
        # print("Verify node success")
        return True

    def verify_xy(self, px, py):
        if px < self.min_x:
            return False
        elif py < self.min_y:
            return False
        elif px >= self.max_x:
            return False
        elif py >= self.max_y:
            return False
        return True

    def calc_obstacle_map(self, ox, oy):

        self.min_x = round(min(ox))
        self.min_y = round(min(oy))
        self.max_x = round(max(ox))
        self.max_y = round(max(oy))
        print("min_x:", self.min_x)
        print("min_y:", self.min_y)
        print("max_x:", self.max_x)
        print("max_y:", self.max_y)

        self.x_width = round((self.max_x - self.min_x) / self.resolution)
        self.y_width = round((self.max_y - self.min_y) / self.resolution)
        print("x_width:", self.x_width)
        print("y_width:", self.y_width)

        # obstacle map generation
        self.obstacle_map = [[False for _ in range(self.y_width)]
                             for _ in range(self.x_width)]
        for ix in range(self.x_width):
            x = self.calc_grid_position(ix, self.min_x)
            for iy in range(self.y_width):
                y = self.calc_grid_position(iy, self.min_y)
                for iox, ioy in zip(ox, oy):
                    d = math.hypot(iox - x, ioy - y)
                    if d <= self.rr:
                        self.obstacle_map[ix][iy] = True
                        break

    @staticmethod
    def get_motion_model():
        # dx, dy, cost
        motion = [[1, 0, 1],
                  [0, 1, 1],
                  [-1, 0, 1],
                  [0, -1, 1],
                  [-1, -1, math.sqrt(2)],
                  [-1, 1, math.sqrt(2)],
                  [1, -1, math.sqrt(2)],
                  [1, 1, math.sqrt(2)]]

        return motion

def main():
    print(__file__ + " start!!")

    # start and goal position
    sx = 10.0  # [m]
    sy = 10.0  # [m]
    gx = 50.0  # [m]
    gy = 50.0  # [m]
    grid_size = 2.0  # [m]
    robot_radius = 1.0  # [m]

    # set obstacle positions
    ox, oy = [], []
    for i in range(-10, 60):
        ox.append(i)
        oy.append(-10.0)
    for i in range(-10, 60):
        ox.append(60.0)
        oy.append(i)
    for i in range(-10, 61):
        ox.append(i)
        oy.append(60.0)
    for i in range(-10, 61):
        ox.append(-10.0)
        oy.append(i)
    for i in range(-10, 40):
        ox.append(20.0)
        oy.append(i)
    for i in range(0, 40):
        ox.append(40.0)
        oy.append(60.0 - i)

    if show_animation:  # pragma: no cover
        plt.plot(ox, oy, ".k")
        plt.plot(sx, sy, "og")
        plt.plot(gx, gy, "xb")
        plt.grid(True)
        plt.axis("equal")

    a_star = AStarPlanner(ox, oy, grid_size, robot_radius)
    rx, ry, route_len, route_len_3D = a_star.planning(sx, sy, gx, gy)
    print("len of route:", route_len)
    print("len of route in 3D:", route_len_3D)

    if show_animation:  # pragma: no cover
        plt.plot(rx, ry, "-r")
        plt.pause(0.001)
        plt.show()

if __name__ == '__main__':
    main()


import pytorch_lightning
from pytorch_lightning.trainer.trainer import Trainer
from pytorch_lightning.tuner.tuning import Tuner
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
from dataset import PetsDataModule

# from model import ResnetTest
from model_mixup import ResnetTest
import config as Config

data_module = PetsDataModule(
    Config.DATA_DIR_BASE,
    Config.BATCH_SIZE,
    Config.NUM_WORKERS)

model = ResnetTest(
    num_classes=data_module.get_num_classes())

checkpoint_callback = ModelCheckpoint(dirpath="./checkpoints", save_top_k=5, monitor="val_accuracy", mode="max")
early_stop_callback = EarlyStopping(monitor="val_accuracy", min_delta=0.001, patience=25, verbose=True, mode="max")

trainer = Trainer(
    accelerator="gpu",
    devices=1,
    enable_checkpointing=True,
    # precision=16,
    callbacks=[checkpoint_callback, early_stop_callback])

# Pick point based on suggestion
# tuner = Tuner(trainer)
# lr_finder = tuner.lr_find(model, datamodule=data_module, min_lr=1e-08, max_lr=1e-3, num_training=250)
# new_lr = lr_finder.suggestion()
# print("new lr: ", new_lr)
# model.learning_rate = new_lr

trainer.fit(
    model,
    datamodule=data_module,
    ckpt_path="./checkpoints/epoch=126-step=93853-v1.ckpt")

trainer.test(
    model,
    datamodule=data_module)


#!/usr/bin/env python
from setuptools import setup, find_packages

required_modules = [
    "django == 1.5.1",
    "selenium == 2.33.0",
    "mock == 1.0.1",
    "south == 0.8.1",
    "MySQL-python",
    "PIL == 1.1.7"
    ]

setup(
    name="Citi Digits",
    version="0.1",
    description="",
    author="Vikash Dat",
    author_email="dat.vikash@gmail.com",
    packages=find_packages(exclude=['test']),
    install_requires=required_modules
)


# coding: utf-8

__author__ = ["Samuel Joshua"]

from PIL import Image

catIm = Image.open('zophie.png')

# left, top, right -1, bottom -1`
croppedIm = catIm.crop((335, 345, 565, 560))
croppedIm.save('cropped.png')


from django.shortcuts import render
from django.http import JsonResponse

from .models import Car
import pymongo

# Create your views here.
def homePage(request):
    object_list = Car.objects.all()
    return render(request,'home.html',{'Cars':object_list})

def carDetailView(request,pk):
    cluster = pymongo.MongoClient("<YOUR MONGODB CONNECTİON STRİNG>")
    db = cluster["db_proje"]
    mycol = db["marker"]
    all_records = mycol.find().sort('_id',-1)
    all_records_list = []

    for i in all_records:
        all_records_list+=[i]

    result = str(pk)

    return render(request,'car_detail.html',{'markers':all_records_list,'pk':result})

def ajax_deneme(request):
    cars = Car.objects.filter(user_id = request.user.id)
    context = {'Cars':cars}
    return render(request,'car_api_deneme.html',context)

def testView(request):
    cluster = pymongo.MongoClient("<YOUR MONGODB CONNECTİON STRİNG>")
    db = cluster["db_proje"]
    mycol = db["marker"]
    all_records1 = mycol.find({'Araba':'47'},{'_id': False}).sort('_id',-1).limit(30)
    all_records2 = mycol.find({'Araba':'52'},{'_id': False}).sort('_id',-1).limit(30)
    all_records3 = mycol.find({'Araba':'101'},{'_id': False}).sort('_id',-1).limit(30)
    all_records4 = mycol.find({'Araba':'104'},{'_id': False}).sort('_id',-1).limit(30)
    all_records_list = []

    for i in all_records1:
        all_records_list+=[i]
    for i in all_records2:
        all_records_list+=[i]
    for i in all_records3:
        all_records_list+=[i]
    for i in all_records4:
        all_records_list+=[i]

    return JsonResponse({'markers':all_records_list})


import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
import numpy as np

df = pd.read_excel('500 comp.xlsx')

df = df.drop(['Company','Sector',"Country","neto",'oper', 'Ventas'],  axis=1)
df.dropna(inplace=True)
print(df.head())

X = df.values[:,1:]
X = np.nan_to_num(X)
Clus_dataSet = StandardScaler().fit_transform(X)
# print(Clus_dataSet)

    #Функция оптимального количества кластеров

def optimise_k_means(data, max_k):

    means = []
    inertias = []

    for k in range(1, max_k):
        kmeans = KMeans(n_clusters=k)
        kmeans.fit(data)

        means.append(k)
        inertias.append(kmeans.inertia_)

    #График плеча (не обращаем внимание на предупреждения в ходе запуска)
    fig = plt.subplots(figsize=(10, 5))
    plt.plot(means, inertias, 'o-')
    plt.xlabel('Number of Clusters')
    plt.ylabel('Inertia')
    plt.grid(True)
    plt.show()
optimise_k_means(df[['Activo', 'Roe']],10)

    #Создание кластеров - Проведение кластеризации

kmeans = KMeans(n_clusters=3)
kmeans.fit(df[['Activo', 'Roe']])
df['kmeans_3'] = kmeans.labels_

    #Готовый к визуализации дб с средними значениями

print(df)

    #Среднее значение (можно заигнорить)

print(df.groupby('kmeans_3').mean())

    #Кластерный график

plt.scatter(x=df['Activo'], y=df['Roe'], c=df['kmeans_3'])
plt.xlim(-0.1, 30)
plt.xlabel('Activo', fontsize=16)
plt.ylim(-100,350)
plt.ylabel('Roe', fontsize=16)
plt.show()

# area = np.pi * (X[:, 1])**2
# plt.scatter(X[:,0], X[:,1], s=area, c=labels, alpha=0.5)
# plt.xlabel('Empl', fontsize=18)
# plt.ylabel('Activo', fontsize=16)
# #
# plt.show()

from mpl_toolkits.mplot3d import Axes3D
# fig = plt.figure(1, figsize=(8, 6))
# plt.clf()
#
# ax = fig.add_subplot(projection='3d')
# ax = Axes3D(fig, rect=[0,0,.95,1], elev=48, azim=134)
#
# ax.set_xlabel('Да')
# ax.set_ylabel('Два')
# ax.set_zlabel('ТРИ')
#
# ax.scatter(X[:, 0],X[:, 1],X[:, 2], c= labels)

# fig = plt.figure(figsize=(7, 4))
# ax_3d = fig.add_subplot(projection='3d')
#
# x = np.arange
# y = np.arange
#
# x = np.arange(-2*np.pi, 2*np.pi, 0.2)
# y = np.arange(-2*np.pi, 2*np.pi, 0.2)
# xgrid, ygrid = np.meshgrid(x,y)
# zgrid = np.sin(xgrid) * np.sin(ygrid) / (xgrid * ygrid)
#
# ax_3d.set_xlabel('x')
# ax_3d.set_ylabel('y')
# ax_3d.set_zlabel('z')
#
# ax_3d.scatter(xgrid, ygrid, zgrid, s=1, color='g')
#
# plt.show()

# X = df[['Activo','Empl']].copy()
# wcss = []
# #the elbow method plots whithin-cluster-sum-of-squares (WCSS) vs the number of clusters
# for i in range(1, 11):
#     kmeans = KMeans(n_clusters=i, random_state=0)
#     kmeans.fit(X)
#     wcss.append(kmeans.inertia_)
#
# sns.set()
# plt.plot(range(1,11,1),wcss)
# plt.title('Selecting the number of clusters using the elbow method')
# plt.xlabel('clusters')
# plt.ylabel('WCSS')
# print(plt.show())
#
# plt.scatter(df["Activo"],df['Empl'])
# plt.title('Activo and Empl')
# plt.xlabel('Activo')
# plt.ylabel('Employers')
# print(plt.show())


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import os
from os import path
import time

class Bot():
    def __init__(self):
        """set ressources urls"""
        self.path = path.abspath(os.curdir) #folder path
        self.bg_remover = "https://removal.ai/"
        self.upscaler = "https://www.upscale.media/upload"
        self.img_suffix = (".jpeg",".png",".jpg")

    def remove_bg(self):
        options = webdriver.ChromeOptions()
        for img in os.listdir():
            if img.lower().endswith(self.img_suffix):
                print(f"removing bg for: {img}")
                with webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) as driver:
                    driver.maximize_window()
                    wait = WebDriverWait(driver, timeout=30, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException,ElementNotSelectableException,NoSuchElementException])
                    driver.get(self.bg_remover)
                    form = driver.find_element(By.CLASS_NAME, "form-control")
                    form.send_keys(f"{self.path}/{img}")
                    btn = wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT,"Download")))
                    btn.click()
                    time.sleep(5)
                os.remove(img)

    def upscale(self):
        options = webdriver.ChromeOptions()
        for img in os.listdir():
            if img.lower().endswith(self.img_suffix):
                print(f"processing: {img}")
                with webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) as driver:
                    driver.maximize_window()
                    wait = WebDriverWait(driver, timeout=30, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException,ElementNotSelectableException,NoSuchElementException])
                    driver.get(self.upscaler)
                    form = wait.until(EC.presence_of_element_located((By.ID, "uploadImage")))
                    form.send_keys(f"{self.path}/{img}")
                    select_element = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/div[1]/div[3]/div[2]/div/div[2]/div/div/div[3]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/select")))
                    select = Select(select_element)
                    select.select_by_value("4x")
                    #working on toggling to enheance pic quality
                    #toggle_div = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "hIlDCJ")))
                    #toggle = toggle_div.find_element(By.TAG_NAME, "input")
                    #driver.execute_script("arguments[0].setAttribute('checked','checked')",toggle)
                    btn = wait.until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[1]/div/div[1]/div[3]/div[2]/div/div[2]/div/div/div[3]/div[1]/div[2]/div[2]/div[2]/button")))
                    btn.click()
                    time.sleep(10)
                os.remove(img)

if __name__=="__main__":
    "debugging code note that the bot removes the file after processing"
    bot = Bot()
    #bot.remove_bg() #uncomment and run to test
    bot.upscale() #uncomment and run to test