import re
import sys
import urllib.error
import urllib.request
from curses import wrapper
from functools import partial
from os import getenv
from pathlib import Path
from shutil import which
from sqlite3 import connect

import click
import toml

from .__version__ import __title__, __version__
from .db import init_db, update_db
from .player import Player

DEFAULT_CONFIG_HOME = Path.home() / '.musicview'
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
CONFIG_ENVAR = 'MUSICVIEW_CONFIG_HOME'
CONF_FILE = 'musicview.toml'

print = click.echo
fprint = partial(click.echo, file=sys.stderr)

class Ctx:
    """
    Context for the cli
    """
    default_config = {
        'general': {
            'check for updates': True,
        },
        'player control': {
            'play/pause': 'p',
            'skip': '>',
            'toggle favourite': 'f',
            'quit': 'q',
        },
        'library paths': {}
    }

    def __init__(self):
        ffplay = which('ffplay')
        ffmpeg = which('ffmpeg')
        if not ffplay or not ffmpeg:
            exit('ffmpeg/ffplay not found!')
        self.ffplay = ffplay
        self.ffmpeg = ffmpeg
        self.config_home = Path(getenv(CONFIG_ENVAR, DEFAULT_CONFIG_HOME)).expanduser()
        if not (self.config_home / CONF_FILE).is_file():
            self.setup()
        self.config = toml.loads((self.config_home / CONF_FILE).read_text())
        if self.config['general']['check for updates']:
            check_update()

    def setup(self):
        """Prompt the user to do some initial setup"""
        click.confirm(
            f"It seems like it's your first time using {__title__}!\n"
            f'Would you like to store configuration and metadata under'
            f' {self.config_home}?\n'
            f'If not, you can set the {CONFIG_ENVAR} environment variable'
            f' to change the location.',
            abort=True
        )
        try:
            self.config_home.mkdir(parents=True, exist_ok=True)
        except OSError as e:
            exit(e)

        check_update = click.confirm(
            'Would you like to automatically check for updates?'
        )
        self.default_config['general']['check for updates'] = check_update
        self.dump_config(self.default_config)
        print(f'Configuration file has been generated at '
              f'{self.config_home / CONF_FILE}')

    def dump_config(self, cfg):
        """
        Dump a config dict
        Args:
            cfg: The config to dump
        """
        with (self.config_home / CONF_FILE).open('w+') as f:
            try:
                toml.dump(cfg, f)
            except OSError as e:
                exit(e)

    def get_libpath(self, name):
        """
        Get library path by name
        Args:
            name: name of the library

        Returns:
            path to the library
        """
        return Path(self.config['library paths'][name]).expanduser()

    def library_exists(self, name):
        """
        Check if a library exists
        Args:
            name: The name for the library

        Returns:
            if the library exists
        """
        return name in map(
            lambda p: p.name.replace('.db', ''), self.config_home.iterdir()
        ) and name in self.config['library paths']

    def delete_lib(self, name):
        """
        Delete a music library
        Args:
            name: Name of the music library
        """
        del self.config['library paths'][name]
        self.dump_config(self.config)

    def new_lib(self, name, path):
        """
        Add a new music library
        Args:
            name: name of the library
            path: path of the library
        """
        assert name not in self.config['library paths']
        self.config['library paths'][name] = path
        self.dump_config(self.config)

    def get_conn(self, name):
        """
        Get a sqlite3 connection
        Args:
            name: name of the library

        Returns:
            the connection
        """
        return connect(str(self.config_home / f'{name}.db'))

pass_context = click.make_pass_decorator(Ctx, ensure=True)

def check_update():
    """Check for updates"""
    url = ('https://raw.githubusercontent.com'
           '/MaT1g3R/musicview/master/musicview/__version__.py')
    try:
        with urllib.request.urlopen(url) as resp:
            text = resp.read().decode()
    except urllib.error.URLError:
        print('Could not check for updates, '
              'are you connected to the internet?')
    else:
        regex = re.compile(r"__version__[\s]*=[\s]*\'(.*)\'")
        head_version = regex.findall(text)[0]
        t = lambda s: tuple(map(int, s.split('.')))
        if t(head_version) > t(__version__):
            print(f'New version ({head_version}) available!')

def play_music(data, name, conn, ffplay, controls, stdscr):
    """
    Play some music!
    Args:
        data: path to the dirctory containg database files
        name: name of the music library
        conn: database connection
        ffplay: ffplay binary
        controls: curses controls keymap
        stdscr: curses screen
    """
    stdscr.clear()
    player = Player(data, name, ffplay, conn, controls, stdscr)
    player.start()

@click.group(context_settings=CONTEXT_SETTINGS)
@pass_context
def cli(ctx):
    """musicview, (re)discover your music library"""
    pass

@click.command(name='list')
@pass_context
def list_(ctx):
    """List existing music libraries"""
    names = ctx.config['library paths']
    if names:
        for name, path in names.items():
            print(f'{name} (at {path})')
    else:
        print('There are currently no music libraries!')

@click.command()
@click.argument('name')
@pass_context
def play(ctx, name):
    """Start playing music"""
    if not ctx.library_exists(name):
        exit(f'Library "{name}" does not exist!')
    controls = {val: key for key, val in ctx.config['player control'].items()}
    with ctx.get_conn(name) as conn:
        wrapper(partial(play_music, ctx.config_home, name, conn, ctx.ffplay, controls))

@click.command()
@click.argument('name')
@pass_context
def delete(ctx, name: str):
    """Delete a music library"""
    if not ctx.library_exists(name):
        exit(f'Library "{name}" does not exist!')
    to_del = ctx.config_home / f'{name}.db'
    click.confirm(f'Delete {name}?', abort=True)
    ctx.delete_lib(name)
    print(f'{name} deleted!')
    try:
        to_del.unlink()
    except OSError as e:
        fprint(e)

@click.command()
@click.argument('name')
@pass_context
def update(ctx, name: str):
    """Update an existing music library"""
    if not ctx.library_exists(name):
        exit(f'Library "{name}" does not exist!')
    with ctx.get_conn(name) as conn:
        update_db(ctx.get_libpath(name), conn, ctx.ffmpeg, ctx.ffplay)

@click.command(short_help='Create a new music library')
@click.argument('name')
@click.argument('path')
@pass_context
def new(ctx, name, path):
    """
    Create a new music library

    Arguments:

        NAME: Name of the new music library

        PATH: Path of the music library
    """
    if ctx.library_exists(name):
        exit(f'Library with name {name} already exists!')
    path = Path(path).expanduser().resolve()
    if not path.is_dir():
        exit(f'Path {path} is not a valid direcory.')
    init_db(
        path, ctx.config_home / f'{name}.db', ctx.ffmpeg, ctx.ffplay
    )
    ctx.new_lib(name, str(path))

cli.add_command(list_)
cli.add_command(new)
cli.add_command(update)
cli.add_command(delete)
cli.add_command(play)


from library.argparse import argparse
import importlib

def argparse_cli(request_uri=["--uri", "-u"]) -> list:
    parser = argparse.ArgumentParser()

    try:
        # 获取uri
        parser.add_argument("--uri", "-u", required=True, help="request uri", type=str)
        args = parser.parse_known_args()[0]
        uri = args.uri
        uri_list = uri.strip("/").split("/")

        if len(uri_list) != 3:
            raise Exception("request uri error")

        module, controller, method = uri_list
    except Exception as e:
        print("analysis request uri error: uri={}".format(uri))
        exit()

    try:
        # 验证参数
        params_path = "config.params.{}.{}".format(module, controller)
        params_file = importlib.import_module(params_path)
        params_info = getattr(params_file, "param_check")[method]
        for field, param in params_info.items():
            field = "--" + field
            parser.add_argument(field, **param)

        args = parser.parse_args()
    except Exception as e:
        print("check params error:")
        exit()

    args = vars(args)
    del args["uri"]

    return uri, args


import pandas as pd
import joblib
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split

# Função para determinar a estação do ano
def get_season(date):
    month = date.month
    if month in [9, 10, 11]:
        return 'Primavera'
    elif month in [12, 1, 2]:
        return 'Verão'
    elif month in [3, 4, 5]:
        return 'Outono'
    else:
        return 'Inverno'

# Importação e Preparação dos Dados
data_path = "path_to_your_data/Itajai_2013_2021_a.xlsx"
df = pd.read_excel(data_path)
df['Estacao'] = df['Data'].apply(get_season)
df_seasonal = df.groupby(['Ano', 'Estacao']).mean().reset_index()
df_seasonal.drop(columns=['Dia', 'Mes'], inplace=True)

# Divisão dos dados em treino e teste
X = df_seasonal.drop(columns=['Aedes', 'Data'])
y = df_seasonal['Aedes']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Treinamento do Modelo
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Salvar o modelo treinado
model_filename = "random_forest_model.pkl"
joblib.dump(model, model_filename)

# Carregar o modelo e fazer previsões
loaded_model = joblib.load(model_filename)
predictions = loaded_model.predict(X_test)

# Aqui, você pode continuar a processar as previsões conforme necessário


#! /usr/bin/env python
from __future__ import print_function

import copy
import pddl

import sys # Warning

class ConditionProxy(object):
    def clone_owner(self):
        clone = copy.copy(self)
        clone.owner = copy.copy(clone.owner)
        return clone

class PreconditionProxy(ConditionProxy):
    def __init__(self, action):
        self.owner = action
        self.condition = action.precondition
    def set(self, new_condition):
        self.owner.precondition = self.condition = new_condition
    def register_owner(self, task):
        task.actions.append(self.owner)
    def delete_owner(self, task):
        task.actions.remove(self.owner)
    def build_rules(self, rules, fluent_preds): #, modules):
        action = self.owner
        rule_head = get_action_predicate(action)
        rule_body = condition_to_rule_body(action.parameters, self.condition)
        rules.append((rule_body, rule_head))
    def get_type_map(self):
        return self.owner.type_map

class EffectConditionProxy(ConditionProxy):
    def __init__(self, action, effect):
        self.action = action
        self.owner = effect
        self.condition = effect.condition
    def set(self, new_condition):
        self.owner.condition = self.condition = new_condition
    def register_owner(self, task):
        self.action.effects.append(self.owner)
    def delete_owner(self, task):
        self.action.effects.remove(self.owner)
    def build_rules(self, rules, fluent_preds): #, modules):
        # NFD: numeric variables are not completely ignored during translation
        # however, it is assumed that if a numeric variable is assigned *any*
        # value, it can also have the value desired for the precondition.
        # Of course, this is an over approximation, but so is delete free reachability
        # that is used for grounding "reachable" facts

        # FIXME support for modules requires to add some code here, too

        effect = self.owner
        rule_head = effect.peffect
        fluent_head = None
        if not isinstance(rule_head,pddl.NegatedAtom):
            # TODO: add treatment of modules in translation here
            if isinstance(rule_head,pddl.FunctionAssignment):
                fluent = rule_head.fluent
                rule_head = get_function_predicate(fluent)
                fluent_head = pddl.Atom(fluent,fluent.args)
            rule_body = [get_action_predicate(self.action)]
            rule_body += condition_to_rule_body([], self.condition)
            if rule_head:
                rules.append((rule_body, rule_head))
            if fluent_head:
                rules.append((rule_body, fluent_head))

    def get_type_map(self):
        return self.action.type_map

class AxiomConditionProxy(ConditionProxy):
    def __init__(self, axiom):
        self.owner = axiom
        self.condition = axiom.condition
    def set(self, new_condition):
        self.owner.condition = self.condition = new_condition
    def register_owner(self, task):
        task.axioms.append(self.owner)
    def delete_owner(self, task):
        task.axioms.remove(self.owner)
    def build_rules(self, rules, fluent_preds):
        axiom = self.owner
        app_rule_head = get_axiom_predicate(axiom)
        app_rule_body = condition_to_rule_body(axiom.parameters, self.condition)
        rules.append((app_rule_body, app_rule_head))
        params = axiom.parameters[:axiom.num_external_parameters]
        eff_rule_head = pddl.Atom(axiom.name, [par.name for par in params])
        eff_rule_body = [app_rule_head]
        rules.append((eff_rule_body, eff_rule_head))
    def get_type_map(self):
        return self.owner.type_map

class GoalConditionProxy(ConditionProxy):
    def __init__(self, task):
        self.owner = task
        self.condition = task.goal
    def set(self, new_condition):
        self.owner.goal = self.condition = new_condition
    def register_owner(self, task):
        # this assertion should never trigger, because disjunctive
        # goals are now implemented with axioms
        # (see substitute_complicated_goal)
        assert False, "Disjunctive goals not (yet) implemented."
    def delete_owner(self, task):
        # this assertion should never trigger, because disjunctive
        # goals are now implemented with axioms
        # (see substitute_complicated_goal)
        assert False, "Disjunctive goals not (yet) implemented."
    def build_rules(self, rules, fluent_preds):
        rule_head = pddl.Atom("@goal-reachable", [])
        rule_body = condition_to_rule_body([], self.condition)
        rules.append((rule_body, rule_head))
    def get_type_map(self):
        # HACK!
        # Method uniquify_variables HAS already been called (which is good).
        # We call it here again for its SIDE EFFECT of collecting the type_map
        # (which is bad). Having "top-level conditions" (currently, only goal
        # conditions, but might also include safety conditions and similar)
        # contained in a separate wrapper class that stores a type map might
        # be a better design.
        type_map = {}
        self.condition.uniquify_variables(type_map)
        return type_map

def get_action_predicate(action):
    name = action
    variables = [par.name for par in action.parameters]
    if isinstance(action.precondition, pddl.ExistentialCondition):
        variables += [par.name for par in action.precondition.parameters]
    return pddl.Atom(name, variables)

def get_axiom_predicate(axiom):
    name = axiom
    variables = [par.name for par in axiom.parameters]
    if isinstance(axiom.condition, pddl.ExistentialCondition):
        variables += [par.name for par in axiom.condition.parameters]
    return pddl.Atom(name, variables)

def get_fluent_predicates(task):
    fluent_predicates = set()
    for action in task.actions:
        for effect in action.effects:
            if isinstance(effect.peffect,pddl.Literal):
                fluent_predicates.add(effect.peffect.predicate)
            else:
                predicate = get_function_predicate(effect.peffect.fluent).predicate
                fluent_predicates.add(predicate)
    for axiom in task.axioms:
        fluent_predicates.add(axiom.name)
    return fluent_predicates

def all_conditions(task):
    for action in task.actions:
#        print(" all_conditions yields conditions of action %s" % action)
#        print("  yielding precondition %s" % PreconditionProxy(action))
        yield PreconditionProxy(action)
        for effect in action.effects: # NFD: also includes numeric cost effect as regular increas effect
            yield EffectConditionProxy(action, effect)
    for axiom in task.axioms:
        yield AxiomConditionProxy(axiom)
    yield GoalConditionProxy(task)

# [1] Remove universal quantifications from conditions.
#
# Replace, in a top-down fashion, <forall(vars, phi)> by <not(not-all-phi)>,
# where <not-all-phi> is a new axiom.
#
# <not-all-phi> is defined as <not(forall(vars,phi))>, which is of course
# translated to NNF. The parameters of the new axioms are exactly the free
# variables of <forall(vars, phi)>.

def remove_universal_quantifiers(task):
    def recurse(condition):
        # Uses new_axioms_by_condition and type_map from surrounding scope.
        if isinstance(condition, pddl.UniversalCondition):
            axiom_condition = condition.negate()
            parameters = sorted(axiom_condition.free_variables())
            axiom = new_axioms_by_condition.get(axiom_condition)
            if not axiom:
                typed_parameters = [pddl.TypedObject(v, type_map[v]) for v in parameters]
                condition = recurse(axiom_condition)
                axiom = task.add_axiom(typed_parameters, condition)
                new_axioms_by_condition[condition] = axiom
            return pddl.NegatedAtom(axiom.name, parameters)
        else:
            new_parts = [recurse(part) for part in condition.parts]
            return condition.change_parts(new_parts)

    new_axioms_by_condition = {}
    for proxy in tuple(all_conditions(task)):
        # Cannot use generator because we add new axioms on the fly.
        if proxy.condition.has_universal_part():
            type_map = proxy.get_type_map()
            proxy.set(recurse(proxy.condition))

# [2] Pull disjunctions to the root of the condition.
#
# After removing universal quantifiers, the (k-ary generalization of the)
# following rules suffice for doing that:
# (1) or(phi, or(psi, psi'))      ==  or(phi, psi, psi')
# (2) exists(vars, or(phi, psi))  ==  or(exists(vars, phi), exists(vars, psi))
# (3) and(phi, or(psi, psi'))     ==  or(and(phi, psi), and(phi, psi'))
def build_DNF(task):
    def recurse(condition):
        disjunctive_parts = []
        other_parts = []
        for part in condition.parts:
            part = recurse(part)
            if isinstance(part, pddl.Disjunction):
                disjunctive_parts.append(part)
            else:
                other_parts.append(part)
        if not disjunctive_parts:
            return condition

        # Rule (1): Associativity of disjunction.
        if isinstance(condition, pddl.Disjunction):
            result_parts = other_parts
            for part in disjunctive_parts:
                result_parts.extend(part.parts)
            return pddl.Disjunction(result_parts)

        # Rule (2): Distributivity disjunction/existential quantification.
        if isinstance(condition, pddl.ExistentialCondition):
            parameters = condition.parameters
            result_parts = [pddl.ExistentialCondition(parameters, (part,))
                            for part in disjunctive_parts[0].parts]
            return pddl.Disjunction(result_parts)

        # Rule (3): Distributivity disjunction/conjunction.
        assert isinstance(condition, pddl.Conjunction)
        result_parts = [pddl.Conjunction(other_parts)]
        while disjunctive_parts:
            previous_result_parts = result_parts
            result_parts = []
            parts_to_distribute = disjunctive_parts.pop().parts
            for part1 in previous_result_parts:
                for part2 in parts_to_distribute:
                    result_parts.append(pddl.Conjunction((part1, part2)))
        return pddl.Disjunction(result_parts)

    for proxy in all_conditions(task):
        if proxy.condition.has_disjunction():
            proxy.set(recurse(proxy.condition).simplified())

# [3] Split conditions at the outermost disjunction.
def split_disjunctions(task):
    for proxy in tuple(all_conditions(task)):
        # Cannot use generator directly because we add/delete entries.
        if isinstance(proxy.condition, pddl.Disjunction):
            for part in proxy.condition.parts:
                new_proxy = proxy.clone_owner()
                new_proxy.set(part)
                new_proxy.register_owner(task)
            proxy.delete_owner(task)

# [4] Pull existential quantifiers out of conjunctions and group them.
#
# After removing universal quantifiers and creating the disjunctive form,
# only the following (representatives of) rules are needed:
# (1) exists(vars, exists(vars', phi))  ==  exists(vars + vars', phi)
# (2) and(phi, exists(vars, psi))       ==  exists(vars, and(phi, psi)),
#       if var does not occur in phi as a free variable.
def move_existential_quantifiers(task):
    def recurse(condition):
        existential_parts = []
        other_parts = []
        for part in condition.parts:
            part = recurse(part)
            if isinstance(part, pddl.ExistentialCondition):
                existential_parts.append(part)
            else:
                other_parts.append(part)
        if not existential_parts:
            return condition

        # Rule (1): Combine nested quantifiers.
        if isinstance(condition, pddl.ExistentialCondition):
            new_parameters = condition.parameters + existential_parts[0].parameters
            new_parts = existential_parts[0].parts
            return pddl.ExistentialCondition(new_parameters, new_parts)

        # Rule (2): Pull quantifiers out of conjunctions.
        assert isinstance(condition, pddl.Conjunction)
        new_parameters = []
        new_conjunction_parts = other_parts
        for part in existential_parts:
            new_parameters += part.parameters
            new_conjunction_parts += part.parts
        new_conjunction = pddl.Conjunction(new_conjunction_parts)
        return pddl.ExistentialCondition(new_parameters, (new_conjunction,))

    for proxy in all_conditions(task):
        if proxy.condition.has_existential_part():
            proxy.set(recurse(proxy.condition).simplified())

# [5a] Drop existential quantifiers from axioms, turning them
#      into parameters.

def eliminate_existential_quantifiers_from_axioms(task):
    # Note: This is very redundant with the corresponding method for
    # actions and could easily be merged if axioms and actions were
    # unified.
    for axiom in task.axioms:
        precond = axiom.condition
        if isinstance(precond, pddl.ExistentialCondition):
            # Copy parameter list, since it can be shared with
            # parameter lists of other versions of this axiom (e.g.
            # created when splitting up disjunctive preconditions).
            axiom.parameters = list(axiom.parameters)
            axiom.parameters.extend(precond.parameters)
            axiom.condition = precond.parts[0]

# [5b] Drop existential quantifiers from action preconditions,
#      turning them into action parameters (that don't form part of the
#      name of the action).

def eliminate_existential_quantifiers_from_preconditions(task):
    for action in task.actions:
        precond = action.precondition
        if isinstance(precond, pddl.ExistentialCondition):
            # Copy parameter list, since it can be shared with
            # parameter lists of other versions of this action (e.g.
            # created when splitting up disjunctive preconditions).
            action.parameters = list(action.parameters)
            action.parameters.extend(precond.parameters)
            action.precondition = precond.parts[0]

# [5c] Eliminate existential quantifiers from effect conditions
#
# For effect conditions, we replace "when exists(x, phi) then e" with
# "forall(x): when phi then e.
def eliminate_existential_quantifiers_from_conditional_effects(task):
    for action in task.actions:
        for effect in action.effects:
            condition = effect.condition
            if isinstance(condition, pddl.ExistentialCondition):
                effect.parameters = list(effect.parameters)
                effect.parameters.extend(condition.parameters)
                effect.condition = condition.parts[0]

def remove_arithmetic_expressions(task):
    def recurse(condition):
        if isinstance(condition, pddl.FunctionComparison):
            parts = [task.function_administrator.get_derived_function(exp)
                        for exp in condition.parts]
            if condition.negated:
                return pddl.NegatedFunctionComparison(condition.comparator,parts)
            else:
                return pddl.FunctionComparison(condition.comparator,parts)
        else:
            new_parts = [recurse(part) for part in condition.parts]
            return condition.change_parts(new_parts)

    # remove from conditions
    for proxy in tuple(all_conditions(task)):
        if isinstance(proxy.condition,list):
            condition = []
            for cond in proxy.condition:
                condition.append(recurse(cond))
            proxy.set(condition)
        else:
            proxy.set(recurse(proxy.condition))

    # remove from actions
    admin = task.function_administrator
    for act in task.actions:
        for eff in act.effects:
            if isinstance(eff.peffect,pddl.FunctionAssignment):
                assign = eff.peffect
                assign.expression = admin.get_derived_function(assign.expression)

    # remove from metric expression
    opt_dir, metric_expression = task.metric
    if metric_expression is None: # unit cost -> no metric specified in task
        assert opt_dir == '<', "No metric expression specified in task, but 'maximize' found"
        metric_expression = -1
    else:
        metric_expression = admin.get_derived_function(metric_expression)
    task.metric = (opt_dir, metric_expression)

def substitute_complicated_goal(task):
    goal = task.goal
    if isinstance(goal, pddl.Literal):
        return
    elif isinstance(goal, pddl.Conjunction):
        for item in goal.parts:
            if not isinstance(item, pddl.Literal):
                break
        else:
            return
    new_axiom = task.add_axiom([], goal)
    task.goal = pddl.Atom(new_axiom.name, new_axiom.parameters)

# Combine Steps [1], [2], [3], [4], [5] and do some additional verification
# that the task makes sense.

def normalize(task):
    remove_universal_quantifiers(task)
    substitute_complicated_goal(task)
    build_DNF(task)
    split_disjunctions(task)
    move_existential_quantifiers(task)
    eliminate_existential_quantifiers_from_axioms(task)
    eliminate_existential_quantifiers_from_preconditions(task)
    eliminate_existential_quantifiers_from_conditional_effects(task)
    verify_and_fix_arithmetic_expressions(task) # fix errors from invalid PDDL specifications
    remove_arithmetic_expressions(task) # replace complicated expressions with their derived variable
    verify_axiom_predicates(task)

def verify_and_fix_arithmetic_expressions(task):
    function_names = [fun.name for fun in task.functions]
#     print ("task functions are:")
#     for fun in task.functions:
#         print (fun)
#     print ("task num init is:")
#     for nin in task.num_init:
#         print ("%s of class %s hc %s, tc %s" %(nin, nin.__class__, nin.fluent.__class__, nin.expression.__class__))

    def verify_expression(aexp):
#         print("Verifying ",str(aexp))
        if isinstance(aexp,pddl.PrimitiveNumericExpression):
#             print("- it is a PNE with fluent",aexp.symbol)
            if not aexp.symbol in function_names:
                msg = ("WARNING: Function symbol '%s' appears in a numeric expression but is not defined in domain file.\n"
                       "Adding new symbol '%s' with arity %d.") % (aexp.symbol, aexp.symbol, len(aexp.args))
                print(msg, file=sys.stderr)
                if len(aexp.args) == 0:
                    newfunction = pddl.Function(aexp.symbol, [], "number")
                    task.functions.append(newfunction)
                    initassignment = pddl.Assign(aexp, pddl.NumericConstant(0.0))
                    task.num_init.append(initassignment)
                else: # if this occurs in an actual domain, maybe we could also determine the type of the parameters of higher-arity functions
                    raise Error("Don't know the parameters of function %s with arity %d" % (aexp.symbol, len(aexp.args)))
                    assert False
#         elif isinstance(aexp, pddl.NumericConstant):
#             print("- it is a constant: ",aexp.value)
        elif isinstance(aexp,pddl.AdditiveInverse):
#             print("- it is an additive inverse: ",aexp)
            assert len(parts)==1
            verify_expression(aexp.parts[0])
        elif isinstance(aexp,pddl.ArithmeticExpression):
#             print("- it is an arithmetic expression: ",aexp)
            for part in aexp.parts:
                verify_expression(part)

    admin = task.function_administrator
    #conditions
    # TODO: check conditions
#     for cond_proxy in all_conditions(task):
#         # do something
#         print("DEBUG: missing check for condition",cond_proxy)
    #actions
    for act in task.actions:
        for eff in act.effects:
            if isinstance(eff.peffect,pddl.FunctionAssignment):
                verify_expression(eff.peffect.expression)

    #metric
    if (task.metric[1]):
        verify_expression(task.metric[1])

def verify_axiom_predicates(task):
    # Verify that derived predicates are not used in :init or
    # action effects.
    axiom_names = set()
    for axiom in task.axioms:
        axiom_names.add(axiom.name)

    for fact in task.init:
        # Note that task.init can contain the assignment to (total-cost)
        # in addition to regular atoms.
        if getattr(fact, "predicate", None) in axiom_names:
            raise SystemExit(
                "error: derived predicate %r appears in :init fact '%s'" %
                (fact.predicate, fact))

    for action in task.actions:
        for effect in action.effects:
            if isinstance(effect.peffect, pddl.Literal) and effect.peffect.predicate in axiom_names:
                raise SystemExit(
                    "error: derived predicate %r appears in effect of action %r" %
                    (effect.peffect.predicate, action.name))

# [6] Build rules for exploration component.
def build_exploration_rules(task):
    result = []
    fluent_preds = get_fluent_predicates(task)
    for proxy in all_conditions(task):
        proxy.build_rules(result, fluent_preds) #, task.modules)

    for axiom in task.function_administrator.get_all_axioms():
    # add rules to determine defined functions
        rule_head = get_function_axiom_predicate(axiom)
        rule_body = []
        for part in axiom.parts:
            if isinstance(part,pddl.PrimitiveNumericExpression):
                rule_body.append(get_function_predicate(part))

        result.append((rule_body, rule_head))
        rule_body = [rule_head]
        rule_head = get_function_predicate(axiom.get_head())
        result.append((rule_body, rule_head))
        # add rule to determine fluent functions
        rule_head = get_fluent_function_predicate(axiom.get_head())
        for part in axiom.parts:
            if isinstance(part,pddl.PrimitiveNumericExpression):
                new_rule_body = rule_body + [get_fluent_function_predicate(part)]
                result.append((new_rule_body, rule_head))
    return result

def condition_to_rule_body(parameters, condition):
    result = []
    for par in parameters:
        result.append(par.get_atom())
    if not isinstance(condition, pddl.Truth):
        if isinstance(condition, pddl.ExistentialCondition):
            for par in condition.parameters:
                result.append(par.get_atom())
            condition = condition.parts[0]
        if isinstance(condition, pddl.Conjunction):
            parts = condition.parts
        else:
            parts = (condition,)
        for part in parts:
            assert isinstance(part, pddl.Literal) or isinstance(part, pddl.FunctionComparison), "Condition not normalized (part of class %s)" % part.__class__
            if isinstance(part, pddl.Literal):

#            if isinstance(part, pddl.Falsity):
#                # Use an atom in the body that is always false because
#                # it is not initially true and doesn't occur in the
#                # head of any rule.
#                return [pddl.Atom("@always-false", [])]
#            assert isinstance(part, pddl.Literal), "Condition not normalized: %r" % part

                if not part.negated:
                    result.append(part)
            else:
                primitives = part.primitive_numeric_expressions()
                for pne in primitives:
                    result.append(get_function_predicate(pne))
    return result

def get_function_predicate(pne):
    name = "defined!%s" % pne.symbol
    return pddl.Atom(name, pne.args)

def get_fluent_function_predicate(pne):
    return pddl.Atom(pne,pne.args)

def get_function_axiom_predicate(axiom):
    name = axiom
    args = axiom.parameters
    for part in axiom.parts:
        if isinstance(part, pddl.PrimitiveNumericExpression):
            args += part.args
        elif isinstance(part, pddl.NumericAxiom):
            args += part.parameters
    return pddl.Atom(name, args)

if __name__ == "__main__":
    import pddl_parser
    task = pddl_parser.open()
    normalize(task)
    task.dump()


class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        answers = {}
        def solve(start, end):
            if start > end: return 0
            if start == end: return nums[start]
            if answers.get((start, end)) is None:
                answers[(start, end)] = max(nums[start] + solve(start + 2, end), solve(start + 1, end))
            return answers[(start, end)]
        return max(solve(0, len(nums) - 2), solve(1, len(nums) - 1)) if len(nums) != 1 else nums[0]


#!/bin/env python3

import sys
import os
import argparse
import numpy
import math

# Parse arguments
parser = argparse.ArgumentParser(description='Visualise a grid')
parser.add_argument('input', type=str, help='input file')
parser.add_argument('--ref', type=str, help='reference file')
parser.add_argument('--dtype', default='float64', type=str, help='numpy element dtype (e.g. float, complex)')
parser.add_argument('--theta', dest='theta', type=float, required=True,
                    help='Field of view size')
parser.add_argument('--norm', type=float,
                    help='Use colour scale centered at zero')
parser.add_argument('--backend', type=str, default='TkAgg',
                    help='matplotlib backend to use')

parser.add_argument('--threshold', type=float, default=0,
                    help='Source detection threshold')
parser.add_argument('--fwhm', type=float, default=1.0,
                    help='Source detection kernel full width half maximum')
parser.add_argument('--npixels', type=int, default=5,
                    help='Source detection number of pixels required for detection')

args = parser.parse_args()

# Read file
data = numpy.fromfile(args.input, dtype=args.dtype, count=-1, sep='')
if args.ref is not None:
    data -= numpy.fromfile(args.ref, dtype=args.dtype, count=-1, sep='')
size = data.size
width = int(math.sqrt(size))
print("Size %dx%d, lambda %g" % (width, width, width / args.theta))
print("Min %g, max %g, mean %g, mean square %g" % (numpy.min(data), numpy.max(data),
                                                   numpy.mean(data), numpy.mean(data**2)))
image = data.reshape((width, width))

# Detect sources, if requested
if args.threshold > 0:

    from astropy.convolution import Gaussian2DKernel, Box2DKernel
    from astropy.stats import gaussian_fwhm_to_sigma
    import astropy.units as u
    from photutils import segmentation
    kernel = Gaussian2DKernel(args.fwhm * gaussian_fwhm_to_sigma,
                              x_size=int(1.5*args.fwhm), y_size=int(1.5*args.fwhm))
    kernel.normalize()
    segments = segmentation.detect_sources(image, args.threshold, npixels=args.npixels, filter_kernel=kernel)
    print("Have %d segments:" % (segments.nlabels))
    props = segmentation.source_properties(image, segments, filter_kernel=kernel)
    for segment in props:
        print("l=%+.6f m=%+.6f intensity=%.f" %
              (segment.xcentroid.value / width * args.theta - args.theta/2,
               segment.ycentroid.value / width * args.theta - args.theta/2,
               segment.max_value))

# Visualise
import matplotlib
matplotlib.use(args.backend)

project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(project_root)
from util.visualize import show_image
show_image(image, args.input, args.theta, norm=args.norm)


from tensorflow.python.data.benchmarks import benchmark_base
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.data.ops import options as options_lib

class RangeBenchmark(benchmark_base.DatasetBenchmarkBase):
  """Benchmarks for `tf.data.Dataset.range()`."""

  def _benchmark_range(self, num_elements, autotune, benchmark_id):
    options = options_lib.Options()
    options.autotune.enabled = autotune
    dataset = dataset_ops.Dataset.range(num_elements)
    dataset = dataset.with_options(options)

    self.run_and_report_benchmark(
        dataset,
        num_elements=num_elements,
        extras={
            "model_name": "range.benchmark.%d" % benchmark_id,
            "parameters": "%d.%s" % (num_elements, autotune),
        },
        name="modeling_%s" % ("on" if autotune else "off"))

  def benchmark_range_with_modeling(self):
    self._benchmark_range(num_elements=10000000, autotune=True, benchmark_id=1)

  def benchmark_range_without_modeling(self):
    self._benchmark_range(num_elements=50000000, autotune=False, benchmark_id=2)

if __name__ == "__main__":
  benchmark_base.test.main()


import cv2
import numpy as np
# masks are the binary images that indicate the pixel in which an operation is to be
#performed
img=cv2.imread("blackwhite.jpg")
print(img.shape)
img2=np.zeros((339,509,3),np.uint8)
cv2.rectangle(img2,(200,0),(300,100),(255,255,255),-1)
bitand=cv2.bitwise_and(img,img2)
bitor=cv2.bitwise_or(img,img2)
bitnot=cv2.bitwise_not(img)
bitxor=cv2.bitwise_xor(img,img2)
cv2.imshow("image",img)
cv2.imshow("image2",img2)
cv2.imshow("image3",bitand)
cv2.imshow("image4",bitor)
cv2.imshow("image5",bitnot)
cv2.imshow("image6",bitxor)

cv2.waitKey(0)
cv2.destroyAllWindows()


d={}
d['name1']="Zia UR Rahman"
d['name2']="Sandeep Sandy"
d['nmae3']="Sravanthi "
d['name4']="Prabanjan"
d['name1']="Sunder Pichai"
print(d)
print(type(d))
for x1 in d:
	print(x1)


import time
import logging

import pytest

LOGGER = logging.getLogger(__name__)

def test_cli_args(container, http_client):
    """Container should respect notebook server command line args
    (e.g., disabling token security)"""
    c = container.run(command=["start-notebook.sh", "--NotebookApp.token=''"])
    resp = http_client.get("http://localhost:8888")
    resp.raise_for_status()
    logs = c.logs(stdout=True).decode("utf-8")
    LOGGER.debug(logs)
    assert "ERROR" not in logs
    warnings = [
        warning for warning in logs.split("\n") if warning.startswith("WARNING")
    ]
    assert len(warnings) == 1
    assert warnings[0].startswith("WARNING: Jupyter Notebook deprecation notice")
    assert "login_submit" not in resp.text

@pytest.mark.filterwarnings("ignore:Unverified HTTPS request")
def test_unsigned_ssl(container, http_client):
    """Container should generate a self-signed SSL certificate
    and notebook server should use it to enable HTTPS.
    """
    c = container.run(environment=["GEN_CERT=yes"])
    # NOTE: The requests.Session backing the http_client fixture does not retry
    # properly while the server is booting up. An SSL handshake error seems to
    # abort the retry logic. Forcing a long sleep for the moment until I have
    # time to dig more.
    time.sleep(5)
    resp = http_client.get("https://localhost:8888", verify=False)
    resp.raise_for_status()
    assert "login_submit" in resp.text
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    warnings = [
        warning for warning in logs.split("\n") if warning.startswith("WARNING")
    ]
    assert len(warnings) == 1
    assert warnings[0].startswith("WARNING: Jupyter Notebook deprecation notice")

def test_uid_change(container):
    """Container should change the UID of the default user."""
    c = container.run(
        tty=True,
        user="root",
        environment=["NB_UID=1010"],
        command=["start.sh", "bash", "-c", "id && touch /opt/conda/test-file"],
    )
    # usermod is slow so give it some time
    rv = c.wait(timeout=120)
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert rv == 0 or rv["StatusCode"] == 0
    assert "uid=1010(jovyan)" in c.logs(stdout=True).decode("utf-8")

def test_gid_change(container):
    """Container should change the GID of the default user."""
    c = container.run(
        tty=True,
        user="root",
        environment=["NB_GID=110"],
        command=["start.sh", "id"],
    )
    rv = c.wait(timeout=10)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert "gid=110(jovyan)" in logs
    assert "groups=110(jovyan),100(users)" in logs

def test_nb_user_change(container):
    """Container should change the user name (`NB_USER`) of the default user."""
    nb_user = "nayvoj"
    running_container = container.run(
        tty=True,
        user="root",
        environment=[f"NB_USER={nb_user}", "CHOWN_HOME=yes"],
        command=["start.sh", "bash", "-c", "sleep infinity"],
    )

    # Give the chown time to complete. Use sleep, not wait, because the
    # container sleeps forever.
    time.sleep(10)
    LOGGER.info(f"Checking if the user is changed to {nb_user} by the start script ...")
    output = running_container.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in output
    assert "WARNING" not in output
    assert (
        f"username: jovyan       -> {nb_user}" in output
    ), f"User is not changed to {nb_user}"

    LOGGER.info(f"Checking {nb_user} id ...")
    command = "id"
    expected_output = f"uid=1000({nb_user}) gid=100(users) groups=100(users)"
    cmd = running_container.exec_run(command, user=nb_user, workdir=f"/home/{nb_user}")
    output = cmd.output.decode("utf-8").strip("\n")
    assert output == expected_output, f"Bad user {output}, expected {expected_output}"

    LOGGER.info(f"Checking if {nb_user} owns his home folder ...")
    command = f'stat -c "%U %G" /home/{nb_user}/'
    expected_output = f"{nb_user} users"
    cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}")
    output = cmd.output.decode("utf-8").strip("\n")
    assert (
        output == expected_output
    ), f"Bad owner for the {nb_user} home folder {output}, expected {expected_output}"

    LOGGER.info(
        f"Checking if home folder of {nb_user} contains the hidden '.jupyter' folder with appropriate permissions ..."
    )
    command = f'stat -c "%F %U %G" /home/{nb_user}/.jupyter'
    expected_output = f"directory {nb_user} users"
    cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}")
    output = cmd.output.decode("utf-8").strip("\n")
    assert (
        output == expected_output
    ), f"Hidden folder .jupyter was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}"

def test_chown_extra(container):
    """Container should change the UID/GID of a comma separated
    CHOWN_EXTRA list of folders."""
    c = container.run(
        tty=True,
        user="root",
        environment=[
            "NB_UID=1010",
            "NB_GID=101",
            "CHOWN_EXTRA=/home/jovyan,/opt/conda/bin",
            "CHOWN_EXTRA_OPTS=-R",
        ],
        command=[
            "start.sh",
            "bash",
            "-c",
            "stat -c '%n:%u:%g' /home/jovyan/.bashrc /opt/conda/bin/jupyter",
        ],
    )
    # chown is slow so give it some time
    rv = c.wait(timeout=120)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert "/home/jovyan/.bashrc:1010:101" in logs
    assert "/opt/conda/bin/jupyter:1010:101" in logs

def test_chown_home(container):
    """Container should change the NB_USER home directory owner and
    group to the current value of NB_UID and NB_GID."""
    c = container.run(
        tty=True,
        user="root",
        environment=[
            "CHOWN_HOME=yes",
            "CHOWN_HOME_OPTS=-R",
            "NB_USER=kitten",
            "NB_UID=1010",
            "NB_GID=101",
        ],
        command=["start.sh", "bash", "-c", "stat -c '%n:%u:%g' /home/kitten/.bashrc"],
    )
    rv = c.wait(timeout=120)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert "/home/kitten/.bashrc:1010:101" in logs

def test_sudo(container):
    """Container should grant passwordless sudo to the default user."""
    c = container.run(
        tty=True,
        user="root",
        environment=["GRANT_SUDO=yes"],
        command=["start.sh", "sudo", "id"],
    )
    rv = c.wait(timeout=10)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert "uid=0(root)" in logs

def test_sudo_path(container):
    """Container should include /opt/conda/bin in the sudo secure_path."""
    c = container.run(
        tty=True,
        user="root",
        environment=["GRANT_SUDO=yes"],
        command=["start.sh", "sudo", "which", "jupyter"],
    )
    rv = c.wait(timeout=10)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert logs.rstrip().endswith("/opt/conda/bin/jupyter")

def test_sudo_path_without_grant(container):
    """Container should include /opt/conda/bin in the sudo secure_path."""
    c = container.run(
        tty=True,
        user="root",
        command=["start.sh", "which", "jupyter"],
    )
    rv = c.wait(timeout=10)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert logs.rstrip().endswith("/opt/conda/bin/jupyter")

def test_group_add(container):
    """Container should run with the specified uid, gid, and secondary
    group. It won't be possible to modify /etc/passwd since gid is nonzero, so
    additionally verify that setting gid=0 is suggested in a warning.
    """
    c = container.run(
        user="1010:1010",
        group_add=["users"],  # Ensures write access to /home/jovyan
        command=["start.sh", "id"],
    )
    rv = c.wait(timeout=5)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    warnings = [
        warning for warning in logs.split("\n") if warning.startswith("WARNING")
    ]
    assert len(warnings) == 1
    assert "Try setting gid=0" in warnings[0]
    assert "uid=1010 gid=1010 groups=1010,100(users)" in logs

def test_set_uid(container):
    """Container should run with the specified uid and NB_USER.
    The /home/jovyan directory will not be writable since it's owned by 1000:users.
    Additionally verify that "--group-add=users" is suggested in a warning to restore
    write access.
    """
    c = container.run(
        user="1010",
        command=["start.sh", "id"],
    )
    rv = c.wait(timeout=5)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "uid=1010(jovyan) gid=0(root)" in logs
    warnings = [
        warning for warning in logs.split("\n") if warning.startswith("WARNING")
    ]
    assert len(warnings) == 1
    assert "--group-add=users" in warnings[0]

def test_set_uid_and_nb_user(container):
    """Container should run with the specified uid and NB_USER."""
    c = container.run(
        user="1010",
        environment=["NB_USER=kitten"],
        group_add=["users"],  # Ensures write access to /home/jovyan
        command=["start.sh", "id"],
    )
    rv = c.wait(timeout=5)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "uid=1010(kitten) gid=0(root)" in logs
    warnings = [
        warning for warning in logs.split("\n") if warning.startswith("WARNING")
    ]
    assert len(warnings) == 1
    assert "user is kitten but home is /home/jovyan" in warnings[0]

def test_container_not_delete_bind_mount(container, tmp_path):
    """Container should not delete host system files when using the (docker)
    -v bind mount flag and mapping to /home/jovyan.
    """
    d = tmp_path / "data"
    d.mkdir()
    p = d / "foo.txt"
    p.write_text("some-content")

    c = container.run(
        tty=True,
        user="root",
        working_dir="/home/",
        environment=[
            "NB_USER=user",
            "CHOWN_HOME=yes",
        ],
        volumes={d: {"bind": "/home/jovyan/data", "mode": "rw"}},
        command=["start.sh", "ls"],
    )
    rv = c.wait(timeout=5)
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert rv == 0 or rv["StatusCode"] == 0
    assert p.read_text() == "some-content"
    assert len(list(tmp_path.iterdir())) == 1

@pytest.mark.parametrize("enable_root", [False, True])
def test_jupyter_env_vars_to_unset_as_root(container, enable_root):
    """Environment variables names listed in JUPYTER_ENV_VARS_TO_UNSET
    should be unset in the final environment."""
    root_args = {"user": "root"} if enable_root else {}
    c = container.run(
        tty=True,
        environment=[
            "JUPYTER_ENV_VARS_TO_UNSET=SECRET_ANIMAL,UNUSED_ENV,SECRET_FRUIT",
            "FRUIT=bananas",
            "SECRET_ANIMAL=cats",
            "SECRET_FRUIT=mango",
        ],
        command=[
            "start.sh",
            "bash",
            "-c",
            "echo I like $FRUIT and ${SECRET_FRUIT:-stuff}, and love ${SECRET_ANIMAL:-to keep secrets}!",
        ],
        **root_args,
    )
    rv = c.wait(timeout=10)
    assert rv == 0 or rv["StatusCode"] == 0
    logs = c.logs(stdout=True).decode("utf-8")
    assert "ERROR" not in logs
    assert "WARNING" not in logs
    assert "I like bananas and stuff, and love to keep secrets!" in logs


"""
This script fetch an AAD token for an Azure Application using its client ID, client secret, and tenant ID. This AAD
token is then used to authenticate the Databricks service principal so it can then provision itself a personal access
token (PAT). The script was written in Python to be as platform-agnostic as possible (as compared to something like
curl) but that means Python3 is a necessary dependency to use this module.
"""
import urllib.request
import json
import sys

input = json.loads(sys.stdin.read())
url = f'https://login.microsoftonline.com/{input.get("tenant_id")}/oauth2/v2.0/token'
data = (f'client_id={input.get("client_id")}&'
        'grant_type=client_credentials&'
        'scope=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default&'  # programmatic ID for Azure Databricks
        f'client_secret={input.get("client_secret")}').encode('utf-8')

request = urllib.request.Request(url, data=data)
response = json.load(urllib.request.urlopen(request))
token = {
    "token": response.get("access_token")
}
print(json.dumps(token))


""" Special configs for the Logger """
import logging
import time
from functools import wraps
import os

supress_error = False

def basiclogger(loggerobj, loggername, printline = False):
    """ This is a very basic logger, which logs every Level and writes the time
    and a Message.

    arguments:

        -- loggerobj (str): Name for the loggerobject to later call on
        -- loggername (str): Name of the loggingfile
        -- printline (bool): Condition if the logger should log a restarting line into the log to see all restarts/crashes
    """
    logger = logging.getLogger(loggerobj)
    logger.setLevel(logging.DEBUG)
    dirpath = os.path.dirname(__file__)
    subfoldername = "logs"
    savepath = os.path.join(dirpath, '..', subfoldername, "{}.log".format(loggername))
    # print(savepath)
    fh = logging.FileHandler(savepath)
    fh.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - %(message)s', "%Y-%m-%d %H:%M")
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    # Logging the Start of the Programm. If it runs a whole evening, each restart means a previous crash.
    if printline:
        template = "{:-^80}"
        logger.debug(template.format("Restarting the Programm",))

def logerror(func):
    """ Logs every time an error occours """
    @wraps(func)
    def wrapper(*args, **kwargs):
        if supress_error:
            logger = logging.getLogger('debuglog')
            try:
                func(*args, **kwargs)
                # return func(*args, **kwargs)
            except Exception:
                logger.exception
                logger.exception("The function {} could not be fully excecuted!".format(func.__name__))
                print("The function {} could not be fully excecuted!".format(func.__name__))
        else:
            func(*args, **kwargs)
    return wrapper


import subprocess
import tempfile
import time
import os
from threading import Thread

import configparser
from rhsmlib import file_monitor

import unittest
from unittest.mock import Mock, patch

from test import fixture

# we could probably try to import 'pyinotify' ourselves here to check
# that it is present; shamelessly rely on the import logic in file_monitor
HAS_PYINOTIFY = file_monitor.pyinotify is not None

class TestFilesystemWatcher(fixture.SubManFixture):
    def setUp(self):
        super(TestFilesystemWatcher, self).setUp()
        self.mock_cb1 = Mock(return_value=None)
        self.mock_cb2 = Mock(return_value=None)
        self.testpath1 = self.write_tempfile("").name
        self.testpath2 = self.write_tempfile("").name
        self.testpath3 = self.write_tempfile("").name
        self.dw1 = file_monitor.DirectoryWatch(self.testpath1, [], is_glob=True)
        self.dw2 = file_monitor.DirectoryWatch(self.testpath2, [], is_glob=False)
        self.dw3 = file_monitor.DirectoryWatch(self.testpath2, [self.mock_cb1, self.mock_cb2], is_glob=False)
        self.dir_list = {"DW1": self.dw1, "DW2": self.dw2, "DW3": self.dw3}
        self.fsw1 = file_monitor.FilesystemWatcher(self.dir_list)
        if HAS_PYINOTIFY:
            self.fsw2 = file_monitor.InotifyFilesystemWatcher(self.dir_list)

        now: float = time.time()
        self.future = now + 120.0

    def tearDown(self):
        super(TestFilesystemWatcher, self).tearDown()

    @patch("rhsmlib.file_monitor.is_inotify_available")
    @patch("rhsmlib.file_monitor.is_inotify_config")
    def test_create_fsw(self, mock_config, mock_avail):
        """
        Tests that create_filesystem_watcher returns the correct object

        create_filesystem_watcher should return an inotify filesystem watcher if
        pyinotify is available and configured, and a polling filesystem watcher otherwise
        :param mock_config: mock of whether pyinotify is configured or not according to is_inotify_config()
        :param mock_avail: mock of whether pyinotify is available or not according to is_inotify_avail()
        :return:
        """
        mock_config.return_value = True
        mock_avail.return_value = True
        fsw = file_monitor.create_filesystem_watcher(self.dir_list)
        self.assertIsInstance(fsw, file_monitor.InotifyFilesystemWatcher)
        mock_config.return_value = False
        mock_avail.return_value = True
        fsw = file_monitor.create_filesystem_watcher(self.dir_list)
        self.assertIsInstance(fsw, file_monitor.FilesystemWatcher)
        mock_config.return_value = True
        mock_avail.return_value = False
        fsw = file_monitor.create_filesystem_watcher(self.dir_list)
        self.assertIsInstance(fsw, file_monitor.FilesystemWatcher)
        mock_config.return_value = False
        mock_avail.return_value = False
        fsw = file_monitor.create_filesystem_watcher(self.dir_list)
        self.assertIsInstance(fsw, file_monitor.FilesystemWatcher)

    @patch("rhsmlib.file_monitor.is_inotify_config")
    def test_create_fsw_real(self, mock_config):
        mock_config.return_value = True
        fsw = file_monitor.create_filesystem_watcher(self.dir_list)
        self.assertIsInstance(
            fsw, file_monitor.InotifyFilesystemWatcher if HAS_PYINOTIFY else file_monitor.FilesystemWatcher
        )

    @patch("rhsmlib.file_monitor.pyinotify", new=None)
    def test_inotify_None(self):
        self.assertFalse(file_monitor.is_inotify_available())

    def test_inotify_avail(self):
        self.assertEqual(file_monitor.is_inotify_available(), HAS_PYINOTIFY, "expected: inotify is available")

    @patch("rhsmlib.file_monitor.conf")
    def test_inotify_config(self, mock_config):
        """
        test each case of is_inotify_config()

        :param mock_config: mock_config.__getitem__.return_value.get_int.return_value mocks out
        the value of use_notify in is_inotify_config()
        :return:
        """
        mock_config.__getitem__.return_value.get_int.return_value = True
        self.assertTrue(file_monitor.is_inotify_config())
        mock_config.__getitem__.return_value.get_int.side_effect = ValueError("bees?")
        self.assertTrue(file_monitor.is_inotify_config())
        mock_config.__getitem__.return_value.get_int.side_effect = configparser.Error
        self.assertTrue(file_monitor.is_inotify_config())
        mock_config.__getitem__.return_value.get_int.return_value = None
        self.assertTrue(file_monitor.is_inotify_config())

    def test_polling_stop_value_change(self):
        self.assertFalse(self.fsw1.should_stop)
        self.fsw1.stop()
        self.assertTrue(self.fsw1.should_stop)

    def test_polling_changed_dw_set_2_modified(self):
        self.fsw1.changed_dw_set()
        os.utime(self.testpath1, (self.future, self.future))
        os.utime(self.testpath2, (self.future, self.future))
        self.assertEqual(self.fsw1.changed_dw_set(), {self.dw1, self.dw2, self.dw3})

    def test_polling_changed_dw_set_first_modified(self):
        self.fsw1.changed_dw_set()
        os.utime(self.testpath1, (self.future, self.future))
        self.assertEqual(self.fsw1.changed_dw_set(), {self.dw1})

    def test_polling_changed_dw_set_second_modified(self):
        self.fsw1.changed_dw_set()
        os.utime(self.testpath2, (self.future, self.future))
        self.assertEqual(self.fsw1.changed_dw_set(), {self.dw2, self.dw3})

    def test_polling_changed_dw_set_0_modified(self):
        self.fsw1.changed_dw_set()
        self.assertEqual(self.fsw1.changed_dw_set(), set())

    def test_polling_update_2_modified(self):
        self.fsw1.changed_dw_set()
        os.utime(self.testpath1, (self.future, self.future))
        os.utime(self.testpath2, (self.future, self.future))
        self.assertEqual(self.fsw1.update(), {self.dw1, self.dw2, self.dw3})

    def test_polling_update_first_modified(self):
        self.fsw1.changed_dw_set()
        os.utime(self.testpath1, (self.future, self.future))
        self.assertEqual(self.fsw1.update(), {self.dw1})

    def test_polling_update_second_modified(self):
        self.fsw1.changed_dw_set()
        os.utime(self.testpath2, (self.future, self.future))
        self.assertEqual(self.fsw1.update(), {self.dw2, self.dw3})

    def test_polling_update_0_modified(self):
        self.fsw1.changed_dw_set()
        self.assertEqual(self.fsw1.update(), set())

    def test_polling_no_loop_when_stopped(self):
        self.fsw1.should_stop = True
        self.fsw1.loop()

    def test_polling_loop_stops(self):
        test_loop = TestLoop(self.fsw1)
        loop_thread = Thread(target=test_loop.run)
        loop_thread.start()
        self.assertFalse(test_loop.stopped)
        test_loop.fsw.stop()
        loop_thread.join(5.0)
        self.assertTrue(test_loop.stopped)

    @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available")
    def test_inotify_stop_value_change(self):
        self.assertFalse(self.fsw2.should_stop)
        self.fsw2.stop()
        self.assertTrue(self.fsw2.should_stop)

    @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available")
    def test_inotify_no_loop_when_stopped(self):
        self.fsw2.should_stop = True
        self.fsw2.loop()

    @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available")
    @patch("rhsmlib.file_monitor.DirectoryWatch.notify")
    @patch("pyinotify.Event")
    def test_handle_event(self, mock_event, mock_notify):
        mock_event.path = self.testpath1
        mock_event.pathname = self.testpath1
        mock_event.mask = self.dw3.IN_MODIFY
        self.fsw2.handle_event(mock_event)
        self.assertEqual(mock_notify.call_count, 1)
        mock_notify.call_count = 0
        mock_event.mask = 0
        self.fsw2.handle_event(mock_event)
        self.assertEqual(mock_notify.call_count, 0)
        mock_notify.call_count = 0

        mock_event.path = self.testpath2
        mock_event.pathname = self.testpath2
        mock_event.mask = self.dw3.IN_MODIFY
        self.fsw2.handle_event(mock_event)
        self.assertEqual(mock_notify.call_count, 2)
        mock_notify.call_count = 0
        mock_event.mask = 0
        self.fsw2.handle_event(mock_event)
        self.assertEqual(mock_notify.call_count, 0)
        mock_notify.call_count = 0

        mock_event.path = self.testpath3
        mock_event.pathname = self.testpath3
        mock_event.mask = self.dw3.IN_MODIFY
        self.fsw2.handle_event(mock_event)
        self.assertEqual(mock_notify.call_count, 0)
        mock_notify.call_count = 0
        mock_event.mask = 0
        self.fsw2.handle_event(mock_event)
        self.assertEqual(mock_notify.call_count, 0)

class TestDirectoryWatch(fixture.SubManFixture):
    def setUp(self):
        super(TestDirectoryWatch, self).setUp()
        self.mock_cb1 = Mock(return_value=None)
        self.mock_cb2 = Mock(return_value=None)
        self.tmp_file1 = tempfile.NamedTemporaryFile(prefix="directory_watcher_test_")
        self.tmp_file2 = tempfile.NamedTemporaryFile(prefix="directory_watcher_test_")
        self.testpath1 = self.tmp_file1.name
        self.testpath2 = self.tmp_file2.name
        subprocess.call("touch %s" % self.testpath1, shell=True)
        subprocess.call("touch %s" % self.testpath2, shell=True)
        self.dw1 = file_monitor.DirectoryWatch(self.testpath1, [], is_glob=True)
        self.dw2 = file_monitor.DirectoryWatch(self.testpath2, [], is_glob=False)
        self.dw3 = file_monitor.DirectoryWatch(self.testpath2, [self.mock_cb1, self.mock_cb2], is_glob=False)

    def tearDown(self):
        self.tmp_file1.close()
        self.tmp_file2.close()

    def test_notify(self):
        self.dw3.notify()
        self.mock_cb1.assert_called_once()
        self.mock_cb2.assert_called_once()

    @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available")
    @patch("pyinotify.Event")
    def test_paths_match(self, mock_event):
        mock_event.path = self.testpath2
        mock_event.pathname = self.testpath2
        self.assertTrue(self.dw3.paths_match(mock_event.path, mock_event.pathname))
        mock_event.pathname = "%s.swp" % self.testpath2
        self.assertFalse(self.dw3.paths_match(mock_event.path, mock_event.pathname))
        mock_event.pathname = self.testpath2
        mock_event.path = self.testpath1
        self.assertTrue(self.dw3.paths_match(mock_event.path, mock_event.pathname))
        mock_event.pathname = self.testpath1
        self.assertFalse(self.dw3.paths_match(mock_event.path, mock_event.pathname))

    @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available")
    @patch("pyinotify.Event")
    def test_file_modified(self, mock_event):
        mock_event.mask = 1
        self.assertFalse(self.dw3.is_file_modified(mock_event.mask))
        mock_event.mask = self.dw3.IN_MODIFY
        self.assertTrue(self.dw3.is_file_modified(mock_event.mask))
        mock_event.mask = self.dw3.IN_DELETE
        self.assertTrue(self.dw3.is_file_modified(mock_event.mask))
        mock_event.mask = self.dw3.IN_MOVED_TO
        self.assertTrue(self.dw3.is_file_modified(mock_event.mask))

class TestLoop:
    def __init__(self, fsw):
        self.stopped = False
        self.fsw = fsw

    def run(self):
        self.fsw.loop()
        self.stopped = True


from setuptools import setup, find_packages  # Always prefer setuptools over distutils
from codecs import open  # To use a consistent encoding
from os import path

def read(*paths):
    """Build a file path from *paths* and return the contents."""
    with open(os.path.join(*paths), 'r') as f:
        return f.read()

here = path.abspath(path.dirname(__file__))

# Get the long description from the relevant file
with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='Xymon_Class',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # http://packaging.python.org/en/latest/tutorial.html#version
    version='0.1.0',

    description='A simple Class to manipulate Xymon test script',
    long_description=long_description,

    py_modules=['Xymon_Class'],

    # The project's main homepage.
    url='https://github.com/erikbeauvalot/Xymon_Class',

    # Author details
    author='Erik Beauvalot',
    author_email='erik@beauvalot.com',

  )


import numpy as np

# Create two matrices
m1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 7]])
m2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

# summing the matrices
matrix_sum = m1 + m2
print("Matrix Addition:\n", matrix_sum)

# Multiplying the matrices
matrix_prod = m1 @ m2
print("Matrix Multiplication:\n", matrix_prod)

# Transpose of matrix
matrix_trans = m1.T
print("Matrix Transpose:\n", matrix_trans)

# calculating the determinant
m_det = np.linalg.det(m1)
print("Matrix Determinant:", m_det)

# calculating the inverse
m_inv = np.linalg.inv(m1)
print("Matrix Inverse:\n", m_inv)

# calculating the rank
m_rank = np.linalg.matrix_rank(m1)
print("Matrix Rank:", m_rank)

# calculating the eigenvalues and eigenvectors
m_eig = np.linalg.eig(m1)
print("Eigenvalues and Eigenvectors:\n", m_eig)

# Solving the equations
eq1 = np.array([[1,2],[4,5]])
eq2 = np.array([7,5])
x = np.linalg.solve(eq1, eq2)
print("Solutions to Equations:", x)


### Machine-Learning Library - Datasets
### MLLIB template
### DataType - Vectors dense

from __future__ import print_function
from pyspark.ml.regression import LinearRegression
from pyspark.sql import SparkSession
from pyspark.ml.linalg import Vectors

def main():
    lines   = spark.sparkContext.textFile("/Users/atul/Google Drive/2.atul/Atul-work/spark/regression.txt")
    data    = lines.map(lambda x: x.split(",")).map(lambda x: (float(x[0]), Vectors.dense(float(x[1]))))

    ## Convert this RDD to a DataFrame
    colNames    = ["label", "features"]
    df          = data.toDF(colNames)

    ## Let's split our data into training data and testing data
    trainTest   = df.randomSplit([0.5, 0.5])
    trainingDF  = trainTest[0]
    testDF      = trainTest[1]

    ## Create Linear Regression model
    lir     = LinearRegression(maxIter=10, regParam=0.3, elasticNetParam=0.8)

    ## Train model using our training data
    model   = lir.fit(trainingDF)

    ## See if we can predict values in our test data.
    ## Generate predictions using our linear regression model for all features in our
    ## test dataframe:
    fullPredictions = model.transform(testDF).cache()

    ## Extract the predictions and the "known" correct labels.
    predictions     = fullPredictions.select("prediction").rdd.map(lambda x: x[0])
    labels          = fullPredictions.select("label").rdd.map(lambda x: x[0])

    ## Zip them together
    predictionAndLabel = predictions.zip(labels).collect()

    # Print out the predicted and actual values for each point
    for prediction in predictionAndLabel:
      print(prediction)

if __name__=='__main__':
    spark = SparkSession.builder.appName("LinearRegression").getOrCreate()
    main()
    spark.stop()
    print("\nDone")

spark.stop()


import os
from django.contrib.gis.utils import LayerMapping
from .models import Counties, Constituencies

counties_mapping = {
    'objectid' : 'OBJECTID',
    'id_field' : 'ID_',
    'county_nam' : 'COUNTY_NAM',
    'const_code' : 'CONST_CODE',
    'constituen' : 'CONSTITUEN',
    'county_cod' : 'COUNTY_COD',
    'shape_leng' : 'Shape_Leng',
    'shape_area' : 'Shape_Area',
    'geom' : 'MULTIPOLYGON',
}

county_shp = os.path .abspath(os.path.join(os.path.dirname(__file__),'data/counties/counties.shp'))

def load_counties_shapefile(verbose=True):
	lm = LayerMapping(Counties, county_shp, counties_mapping, transform= False, encoding='iso-8859-1')
	lm.save(strict=True,verbose=verbose)

constituencies_mapping = {
    'objectid' : 'OBJECTID',
    'county_nam' : 'COUNTY_NAM',
    'const_code' : 'CONST_CODE',
    'constituen' : 'CONSTITUEN',
    'county_ass' : 'COUNTY_ASS',
    'county_a_1' : 'COUNTY_A_1',
    'regist_cen' : 'REGIST_CEN',
    'registrati' : 'REGISTRATI',
    'county_cod' : 'COUNTY_COD',
    'shape_leng' : 'Shape_Leng',
    'shape_area' : 'Shape_Area',
    'geom' : 'MULTIPOLYGON',
}

constituency_shp = os.path .abspath(os.path.join(os.path.dirname(__file__),'data/constituencies/constituencies.shp'))

def load_constituencies_shapefile(verbose=True):
	lm = LayerMapping(Constituencies, constituency_shp, constituencies_mapping, transform= False, encoding='iso-8859-1')
	lm.save(strict=True,verbose=verbose)


def object_to_id(obj):
    """
    Return list representing the cubit object. The first entry is the python id
    of the object, the second entry is the string representation.
    """
    return ["cp2t3id_" + str(id(obj)), str(obj)]

def cubit_item_to_id(cubit_data_list):
    """Return the id from a cubit data list."""
    if not isinstance(cubit_data_list, list):
        return None
    if len(cubit_data_list) == 0:
        return None
    if not isinstance(cubit_data_list[0], str):
        return None
    if cubit_data_list[0].startswith("cp2t3id_"):
        return int(cubit_data_list[0][8:])
    else:
        return None

def is_base_type(obj):
    """
    Check if the object is of a base type that does not need conversion for the
    connection between python2 and python3.
    """
    if (
        isinstance(obj, str)
        or isinstance(obj, int)
        or isinstance(obj, float)
        or isinstance(obj, type(None))
    ):
        return True
    else:
        return False


import sys
import re

# Read the Git diff from standard input
diff = sys.stdin.read()

# Patterns for searching for added, removed, and modified entries
added_pattern = re.compile(r'^\+name = "(.*)"$')
removed_pattern = re.compile(r'^-name = "(.*)"$')
modified_pattern = re.compile(r'^@@.*name = "(.*)"$')

# Initialize lists for added, removed, and modified entries
added = []
removed = []
modified = []

# Process the lines of the Git diff
for line in diff.split('\n'):
    added_match = added_pattern.match(line)
    removed_match = removed_pattern.match(line)
    modified_match = modified_pattern.match(line)
    if added_match:
        added.append(added_match.group(1))
    elif removed_match:
        removed.append(removed_match.group(1))
    elif modified_match:
        modified.append(modified_match.group(1))

# Generate the desired output
with open("CHANGELOG.md", "r+") as file:
    first_line = file.readline()

    # Extract the version number from the first line
    match = re.match(r"# Release (\d+\.\d+\.\d+)", first_line)
    if match:
        old_version = match.group(1)
        print(f"Old version number: {old_version}")

        # Increase the version number by 1
        major, minor, patch = map(int, old_version.split("."))
        new_version = f"{major}.{minor}.{patch+1}"

        print(f"New version number: {new_version}")

        file.seek(0)
        content = file.read()
        file.seek(0)

        file.write(f"# Release {new_version}\n\n")
        file.write("## Changes\n")
        if modified:
            file.write('### Updated:\n')
            for item in modified:
                file.write(f' - {item}\n')

        if added:
            file.write('### Added:\n')
            for item in added:
                file.write(f' - {item}\n')

        if removed:
            file.write('### Removed:\n')
            for item in removed:
                file.write(f' - {item}\n')

        file.write("\n"+content)
        file.close


import streamlit as st
import pickle
import string
from nltk.corpus import stopwords
import nltk
nltk.download('all')
from nltk.stem.porter import PorterStemmer
import base64

ps = PorterStemmer()

def transform_text(text):
    text = text.lower()
    text = nltk.word_tokenize(text)

    y = []
    for i in text:
        if i.isalnum():
            y.append(i)

    text = y[:]
    y.clear()

    for i in text:
        if i not in stopwords.words('english') and i not in string.punctuation:
            y.append(i)

    text = y[:]
    y.clear()

    for i in text:
        y.append(ps.stem(i))

    return " ".join(y)

tfidf = pickle.load(open('C:/Users/user/PycharmProjects/ai-2/venv/vectorizer.pkl','rb'))
model = pickle.load(open('C:/Users/user/PycharmProjects/ai-2/venv/model.pkl','rb'))

main_bg = "C:/Users/user/PycharmProjects/ai-2/venv/img_16.png"
main_bg_ext = "png"

st.markdown(
    f"""

    """,
    unsafe_allow_html=True
)

st.markdown("
Tweet Spam Classifier
",unsafe_allow_html=True)
input_sms = st.text_area("",value='Enter a Tweet')

if st.button('Predict'):

        # 1. preprocess
        transformed_sms = transform_text(input_sms)
        # 2. vectorize
        vector_input = tfidf.transform([transformed_sms])
        # 3. predict
        result = model.predict(vector_input)[0]
        # 4. Display
        if result == 1:
            st.markdown("
Spam
",unsafe_allow_html=True)
        else:
            st.markdown("
Not Spam
",unsafe_allow_html=True)


# BeautifulSoup basics
# Selecting using CSS style selectors using .select()
# .select() returns a list of matching elements; cleaner alternative to .find() and .find_all()

# Select by id = #foobar
# Select by class = .foobar
# Select by children = foo > bar
# Select by descendants = foo bar

from bs4 import BeautifulSoup

# Mockup HTML file inside a multi-lined string
html = """
<!DOCTYPE html>

  <meta charset="UTF-8">
  <title>First HTML Page</title>

hi

more text.

This list item is special.

This list item is also special.

This list item is not special.

bye

"""

# Setup BeautifulSoup to read the mockup HTML content and parse the content using html.parser
soup = BeautifulSoup(html, "html.parser")

# Select by id
print("Select matching elements by CSS selector id")
matching_elements = soup.select("#first")       # Returns matching element(s) enclosed in a list
print(matching_elements)
print("\n")

# Select the 1st matching element by ID
print("Select matching 1st element by CSS selector id:")
matching_elements = soup.select("#first")[0]    # Returns a specific matching element; no long enclosed in a list
print(matching_elements)
print("\n")

# Select by class
print("Select matching elements by CSS selector class:")
matching_elements = soup.select(".special")
print(matching_elements)
print("\n")

# Select by element
print("Select matching elements by element name:")
matching_elements = soup.select("div")
print(matching_elements)
print("\n")

# Select by attribute
print("Select matching elements by attribute:")
matching_elements = soup.select("[data-example]")
print(matching_elements)
print("\n")


import argparse
import pandas as pd
from utils import Logo
from Data_prep import GEP_cleaning, Methyl_cleaning
from Feature_selection import MarginalRegression,getTopK_commonfeatures
import os
import torch
from Data_prep import Train_Tri_Data
import json
from DLModel import MOFUN_CCC
from DLModel import Model_save
from utils import pickle_save,generate_custom_message,Spinner

def restricted_float(x):
    try:
        x = float(x)
    except ValueError:
        raise argparse.ArgumentTypeError("%r not a floating-point literal" % (x,))

    if x < 0.0 or x > 1.0:
        raise argparse.ArgumentTypeError("%r not in range [0.0, 1.0]"%(x,))
    return x

def get_args_parser():
    parser = argparse.ArgumentParser('MOFUN-CCC model training', add_help=False)
    # Input/Output parameters
    parser.add_argument('--Count', type=str, required=True,
                        help='tab seperated txt cell counts file (rows: Samples, columns: Cell types)')
    parser.add_argument('--RNA', type=str, required=True,
                        help='tab seperated txt gene TPM file (rows: Genes, columns: Samples)')
    parser.add_argument('--DNAm', type=str,required=True,
                        help='tab seperated txt CpG Beta file (rows: CpGs, columns: Samples)')
    parser.add_argument('--Output', type=str,required=True,
                        help='Output folder path for model saving')
    #===============================================================================
    # Marker parameters
    parser.add_argument('--GEP_Marker', type=str,
                        help='Path of the marker file, list of Gene names or Association matrix, generate when leaving blank')
    parser.add_argument('--DNAm_Marker', type=str,
                        help='Path of the marker file, list of CpG names or Association matrix, generate when leaving blank')
    parser.add_argument('--Marker_Method', type=str, choices=["FC", "P"],
                        help='Marker selection method, FC: by fold change, P: by p-value')
    parser.add_argument('--RNA_Marker_num', type=int, default=6000,
                        help="Number of RNA markers to select based on Marker_Method")
    parser.add_argument('--DNAm_Marker_num', type=int, default=6000,
                        help="Number of DNAm markers to select based on Marker_Method")
    #===============================================================================
    # Training Data operation parameters
    parser.add_argument('--RNA_transform', type=str,default= "Range", choices=["Identity", "MeanStd", "Range"],
                        help='RNA transformation method')
    parser.add_argument('--DNAm_transform', type=str,default= "Range", choices=["Identity", "MeanStd", "Range", "Beta"],
                        help='DNAm transformation method')
    parser.add_argument('--transform_by_feature', action=argparse.BooleanOptionalAction,
                        help='Transform by feature if claimed otherwise by sample')
    parser.add_argument('--Data_augmentation', type=str, default="Zero", choices=["Zero", "Noise", "No"],
                        help='Data augmentation method')
    parser.add_argument('--scale_cellcounts', action=argparse.BooleanOptionalAction,default=True,
                        help='Scale cell counts by total cell counts')
    #===============================================================================
    # Model parameters
    parser.add_argument('--Model', type=str,default= "./Models/Default_structure.json",
                        help='Model structure file path (Json format)')
    parser.add_argument('--Loss', type=str,default= "L1loss", choices=["L1loss", "L2loss", "CrossEntropy"],
                        help='Loss function')
    parser.add_argument("--Activation", type=str, default="relu", choices=["relu", "leakyRelu", "Elu", "Celu", "Gelu"],
                        help="Activation function")
    parser.add_argument("--Dropout", default=0.2,type=restricted_float,
                        help="Dropout rate of the first layer")
    parser.add_argument("--Learning_rate", type=float, default=5e-5,
                        help="Learning rate")
    parser.add_argument("--Batch_num", type=int, default=80,
                        help="the number of Batches")
    parser.add_argument("--Epochs", type=int, default=100,
                        help="the number of Epochs")
    #===============================================================================
    # Other parameters
    parser.add_argument('--device', type=str, default="detect", choices=["cuda", "cpu", "detect"],
                        help='Device to use for training')
    parser.add_argument('--seed', type=int, default=424,
                        help='Random seed')
    parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
                    help='Show this help message and exit')

    #===============================================================================
    return parser

def main(args):
    Count_Data = pd.read_table(args.Count, index_col=0)
    GEP_Data = pd.read_table(args.RNA, index_col=0)
    DNAm_Data = pd.read_table(args.DNAm, index_col=0)
    CommonID = Count_Data.index.intersection(GEP_Data.columns).intersection(DNAm_Data.columns)
    Count_Data = Count_Data.loc[CommonID,:]
    GEP_Data = GEP_Data.loc[:,CommonID]
    DNAm_Data = DNAm_Data.loc[:,CommonID]
    OutputFolder = args.Output
    if not os.path.exists(OutputFolder):
        os.makedirs(OutputFolder)
    if not os.path.isdir(OutputFolder):
        raise ValueError("Output path is not a folder")
    generate_custom_message("Data loaded Successfully")
    #===============================================================================
    # Marker selection
    RNA_Marker_num = args.RNA_Marker_num
    DNAm_Marker_num = args.DNAm_Marker_num
    # RNA Marker
    if args.GEP_Marker==None:
        # Create marker file for user
        generate_custom_message("GEP Marker file was not input, generating")
        GEP_cleaned = GEP_cleaning(GEP_Data)
        Marker_Method = "FC" if args.Marker_Method == None else args.Marker_Method
        with Spinner():
            MarginalRegression(Count_Data,GEP_cleaned,filePath=f"{OutputFolder}/Gene_Marker_{Marker_Method}.txt", by = Marker_Method)
        GEP_marker_File = pd.read_table(f"{OutputFolder}/Gene_Marker_{Marker_Method}.txt", index_col=0)
        GEP_marker = getTopK_commonfeatures(RNA_Marker_num, GEP_marker_File, by_pvalue=Marker_Method=="P")
    else:
        GEP_marker_File = pd.read_table(args.GEP_Marker, index_col=0)
        if GEP_marker_File.shape[1] == 0:
            print("GEP Marker list was input")
            GEP_marker = GEP_marker_File.index.tolist()
            if len(GEP_marker) > RNA_Marker_num:
                GEP_marker = GEP_marker[:RNA_Marker_num]
        else:
            print("GEP Marker association matrix was input")
            if args.Marker_Method==None:
                raise ValueError("Please specify Marker_Method")
            GEP_marker = getTopK_commonfeatures(RNA_Marker_num, GEP_marker_File, by_pvalue=args.Marker_Method=="P")
    # DNAm Marker
    if args.DNAm_Marker==None:
        # Create marker file for user
        generate_custom_message("DNAm Marker file was not input, generating")
        print("This may take a while")
        DNAm_cleaned = Methyl_cleaning(DNAm_Data)
        Marker_Method = "FC" if args.Marker_Method == None else args.Marker_Method
        with Spinner():
            MarginalRegression(Count_Data,DNAm_cleaned,filePath=f"{OutputFolder}/CpGs_Marker_{Marker_Method}.txt", by = Marker_Method)
        Methyl_marker_File = pd.read_table(f"{OutputFolder}/CpGs_Marker_{Marker_Method}.txt", index_col=0)
        Methyl_marker = getTopK_commonfeatures(DNAm_Marker_num, Methyl_marker_File, by_pvalue=Marker_Method=="P")
    else:
        Methyl_marker_File = pd.read_table(args.DNAm_Marker, index_col=0)
        if Methyl_marker_File.shape[1] == 0:
            print("DNAm Marker list was input")
            Methyl_marker = Methyl_marker_File.index.tolist()
            if len(Methyl_marker) > DNAm_Marker_num:
                Methyl_marker = Methyl_marker[:DNAm_Marker_num]
        else:
            print("DNAm Marker association matrix was input")
            if args.Marker_Method==None:
                raise ValueError("Please specify Marker_Method")
            Methyl_marker = getTopK_commonfeatures(DNAm_Marker_num, Methyl_marker_File, by_pvalue=args.Marker_Method=="P")
    CommonGene = GEP_Data.index.intersection(GEP_marker)
    CommonCpG = DNAm_Data.index.intersection(Methyl_marker)
    generate_custom_message("Markers selection finished")
    #===============================================================================
    # Model training
    if args.device == "detect":
        device = 'cuda' if torch.cuda.is_available() else 'cpu'
    else:
        device = args.device
    aug = 0 if args.Data_augmentation =="No" else args.Data_augmentation
    # define training data
    Training_Data = Train_Tri_Data(GEP_Data, DNAm_Data, Count_Data,
                                   GEP_transform=args.RNA_transform, Methyl_transform=args.DNAm_transform,
                                   by_sample=not args.transform_by_feature, Augmentation=aug,
                                   scaleCT=args.scale_cellcounts,
                                   GEP_marker=CommonGene, Methyl_marker=CommonCpG, device=device)

    # specify model structure
    with open(args.Model,"r") as json_file:
        structure = json.load(json_file)
    structure["GEP_extract"] = [len(CommonGene)]+structure["GEP_extract"]
    structure["Methyl_extract"] = [len(CommonCpG)]+structure["Methyl_extract"]
    torch.manual_seed(args.seed)
    generate_custom_message("Start training")
    print("Train model on", device, "...\n")
    Model = MOFUN_CCC(structure, loss=args.Loss,
                      activation=args.Activation, dropout1st=args.Dropout).to(device)
    # start training
    Model_Stats = Model.runExp(Training_Data, lr=args.Learning_rate,
                               batch_size=round(Training_Data.GEP.shape[0]/args.Batch_num),
                               epochs=args.Epochs, optimizer="Adam")
    Model_save(Model,Training_Data,folderPath=OutputFolder)
    pickle_save(Model_Stats, f"{OutputFolder}/Model_Train_Stats.pkl")

    generate_custom_message("Model Training finished")
    return None

if __name__ == '__main__':
    args = get_args_parser()
    args = args.parse_args()

    print(Logo)
    print("Author: Molin Yue, Email: moy6@pitt.edu, Last updated: 2023-09-25")
    main(args)
    # args = argparse.Namespace(Count = "/ix1/wchen/Molin/Research/Paper_Prep/Data/Nasal3/CTcount.txt",
    #                           RNA="/ix1/wchen/Molin/Research/Paper_Prep/Data/Nasal3/rawTPM.txt",
    #                           DNAm="/ix1/wchen/Molin/Research/Paper_Prep/Data/Nasal3/rawBeta.txt",
    #                           GEP_Marker = "./test/Gene_Marker_FC.txt",
    #                           DNAm_Marker = "./test/CpGs_Marker_FC.txt",
    #                           Marker_Method = "FC",
    #                           Output="./test")
# Test Run
# python Main_train.py \
# --Count /ix1/wchen/Molin/Research/Paper_Prep/Data/FHS_Blood/CTcount.txt \
# --RNA /ix1/wchen/Molin/Research/Paper_Prep/Data/FHS_Blood/lg2Intensity.txt \
# --DNAm /ix1/wchen/Molin/Research/Paper_Prep/Data/FHS_Blood/rawBeta.txt \
# --Output ./
# python Main_train.py --Count /ix1/wchen/Molin/Research/Paper_Prep/Data/EVAPR_Blood/CTcount.txt --RNA /ix1/wchen/Molin/Research/Paper_Prep/Data/EVAPR_Blood/rawTPM.txt --DNAm /ix1/wchen/Molin/Research/Paper_Prep/Data/EVAPR_Blood/rawBeta.txt --Output ./test --GEP_Marker /ix1/wchen/Molin/Research/Paper_Prep/Algo/test/Gene_Marker_FC.txt --DNAm_Marker /ix1/wchen/Molin/Research/Paper_Prep/Algo/test/CpGs_Marker_FC.txt --Marker_Method FC


import bdb
from ctypes import resize
from tkinter import *
from turtle import width
from PIL import Image,ImageTk #pip install pillow
from employee import employeeClass
from supplier import supplierClass
from category import categoryClass
from product import productClass
from sales import salesClass
class IMS:
  def __init__(self,root):
    self.root=root
    self.root.geometry('1350x700+0+0')
    self.root.title("inventory management system ||  developed By 4S")
    self.root.config(bg="Cyan")

#     self.bg=ImageTk.PhotoImage(file="images/10158.jpg")
#     lbl_bg=Label(self.root,image=self.bg,padx=100,pady=200)
#     lbl_bg.place(x=190,y=100,width=1360,height=650)

    self.im1=Image.open("images/img1.png")
    self.im1=self.im1.resize((1500,710),Image.ANTIALIAS)
    self.im1=ImageTk.PhotoImage(self.im1)

    self.lbl_im1=Label(self.root,image=self.im1,bd=2,relief=RAISED)
    self.lbl_im1.place(x=50,y=20)

    #======================= Title ======================================
    self.icon_title=PhotoImage(file="images/logo1.png")
    title=Label(self.root,text="Inventory management System",image=self.icon_title,compound=LEFT,font=("times new roman",44,"bold"),bg="Blue",fg="white",anchor="w",padx=100).place(x=0,y=0,relwidth=1,height=70)

   #======================btn logout =======================================
    btn_logout=Button(self.root,text="Logout",font=("times new roman",11,"bold"),bg="orange",bd=5).place(x=1300,y=10,height=60,width=160)
    #================== clock ===============================================
    self.lbl_clock=Label(self.root,text="Welcome to Inventory management system\t\t Date=: DD-MM-YYYY\t\t Time: HH:MM:SS",font=("times new roman",20),bg="orangered",fg="white",bd=5)
    self.lbl_clock.place(x=0,y=70,relwidth=1,height=32)

    #================ Left menu ===================================================
    self.MenuLogo=Image.open("images/menu_im.png")
    self.MenuLogo=self.MenuLogo.resize((200,200),Image.ANTIALIAS)
    self.MenuLogo=ImageTk.PhotoImage(self.MenuLogo)

    Leftmenu=Frame(self.root,bd=2,relief=RIDGE,bg="Silver")
    Leftmenu.place(x=0,y=102,width=200,height=660)

    lbl_menuLogo=Label(Leftmenu,image=self.MenuLogo)
    lbl_menuLogo.pack(side=TOP,fill=X)

    lbl_menu=Label(Leftmenu,text="Menu",font=("times new roman",20),bg="crimson").pack(side=TOP,fill=X)
    btn_Employee=Button(Leftmenu,text="Employee",command=self.employee,font=("times new roman",20,"bold"),bg="white",bd=5,cursor="hand2").pack(side=TOP,fill=X)
    btn_supplier=Button(Leftmenu,text="Supplier",command=self.supplier,font=("times new roman",20,"bold"),bg="white",bd=5,cursor="hand2").pack(side=TOP,fill=X)
    btn_category=Button(Leftmenu,text="Category",command=self.category,font=("times new roman",20,"bold"),bg="white",bd=5,cursor="hand2").pack(side=TOP,fill=X)
    btn_product=Button(Leftmenu,text="Product",command=self.product,font=("times new roman",20,"bold"),bg="white",bd=5,cursor="hand2").pack(side=TOP,fill=X)
    btn_sales=Button(Leftmenu,text="Sales",command=self.sales,font=("times new roman",20,"bold"),bg="white",bd=5,cursor="hand2").pack(side=TOP,fill=X)
    btn_exit=Button(Leftmenu,text="Exit",font=("times new roman",20,"bold"),bg="white",bd=5,cursor="hand2").pack(side=TOP,fill=X)

    #================== content ================================================
    self.lbl_employee=Label(self.root,text="Total Employee \n[ 0 ]",bd=5,relief=RIDGE,bg="olive",fg="white",font=("time new roman",11,"bold"))
    self.lbl_employee.place(x=300,y=120,height=150,width=300)

    self.lbl_supplier=Label(self.root,text="Total Supplier \n[ 0 ]",bd=5,relief=RIDGE,bg="orange",fg="white",font=("time new roman",11,"bold"))
    self.lbl_supplier.place(x=650,y=120,height=150,width=300)

    self.lbl_category=Label(self.root,text="Total Category \n[ 0 ]",bd=5,relief=RIDGE,bg="lightgreen",fg="white",font=("time new roman",11,"bold"))
    self.lbl_category.place(x=1000,y=120,height=150,width=300)

    self.lbl_product=Label(self.root,text="Total Product \n[ 0 ]",bd=5,relief=RIDGE,bg="green",fg="white",font=("time new roman",11,"bold"))
    self.lbl_product.place(x=460,y=300,height=150,width=300)

    self.lbl_sales=Label(self.root,text="Total Sales \n[ 0 ]",bd=5,relief=RIDGE,bg="brown",fg="white",font=("time new roman",11,"bold"))
    self.lbl_sales.place(x=850,y=300,height=150,width=300)
    #================== footer ===============================================
    lbl_footer=Label(self.root,text="IMS.Inventory Management System || Developed by 4S\nfor any technical issue contact: 910XXXXX13 ",font=("times new roman",12),bg="deepskyblue",fg="white",bd=10).pack(side=BOTTOM,fill=X)
    #============================================================================

  def employee(self):
    self.new_win=Toplevel(self.root)
    self.new_win=employeeClass(self.new_win)

  def supplier(self):
        self.new_win=Toplevel(self.root)
        self.new_obj=supplierClass(self.new_win)

  def category(self):
        self.new_win=Toplevel(self.root)
        self.new_obj=categoryClass(self.new_win)

  def product(self):
        self.new_win=Toplevel(self.root)
        self.new_obj=productClass(self.new_win)

  def sales(self):
        self.new_win=Toplevel(self.root)
        self.new_obj=salesClass(self.new_win)

if __name__=="__main__":
    root=Tk()
    obj=IMS(root)
    root.mainloop()


#!/usr/bin/env python
# Utility for predicting labels based on pre-trained RF model
import sys
import pandas as pd
from joblib import load
from sklearn.metrics import classification_report

if __name__ == "__main__":
    features = pd.read_csv(sys.argv[1], header=0, index_col=0, sep="\t")
    outName = sys.argv[2]
    model = load(sys.argv[3])
    metadata = None
    if len(sys.argv) == 5:
        metadata = pd.read_csv(sys.argv[4], sep="\t", header=None, index_col=0, dtype=str)
        metadata.index = metadata.index.astype(str)

    M = features.shape[0]  # features count
    N = features.shape[1]  # samples  count

    X = features.T
    y_pred = model.predict(X)

    outFile = open(outName+".tsv", "w")
    for sam, pred in zip(X.index, y_pred):
        print(sam, pred, sep="\t", file=outFile)
    outFile.close()

    if metadata is not None:
        y = [metadata.loc[i, 1] for i in X.index]
        print("Predictions accuracy compared with given labels:")
        print(classification_report(y, y_pred, zero_division=0))


def insertion_sort(my_list):
    """ Sort a list using the insertion sort """

    # Start at the second element (pos 1).
    # Use this element to insert into the
    # list.
    for key_pos in range(1, len(my_list)):

        # Get the value of the element to insert
        key_value = my_list[key_pos]

        # Scan from right to the left (start of list)
        scan_pos = key_pos - 1

        # Loop each element, moving them up until
        # we reach the position the
        while (scan_pos >= 0) and (my_list[scan_pos] > key_value):
            my_list[scan_pos + 1] = my_list[scan_pos]
            scan_pos = scan_pos - 1

        # Everything's been moved out of the way, insert
        # the key into the correct location
        my_list[scan_pos + 1] = key_value

my_list = [1, 3, 7, 23, 0, 45, 3]

sorted_list = insertion_sort(my_list)
print(sorted_list)


pos = [0,0]

with open(r'c:\Users\gearp\Desktop\Coding\AoC_21\Day2\input.txt') as f:
  for line in f:
    split = line.split()
    num = int(split[1])
    if split[0] == 'forward': pos[0] += num
    elif split[0] == 'up': pos[1] -= num
    elif split[0] == 'down': pos[1] += num
    else: print('ERROR')

print(f'X: {pos[0]}   Y: {pos[1]}')
print(pos[0] * pos[1])


class Solution:
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        """列举所有的排列形式"""
        res = []
        nums.sort()
        self.my_dfs(nums, [], res)

        return res

    def my_dfs(self, nums, tmp, res):
        if len(tmp) == len(nums):
            import copy
            res.append(copy.copy(tmp))

            return
        for i in range(0, len(nums)):
            if nums[i] in tmp:
                continue
            tmp.append(nums[i])
            self.my_dfs(nums, tmp, res)
            tmp.remove(nums[i])

solution = Solution()
print(solution.permute([1, 2, 3]))


class Solution:
    def closedIsland(self, grid) -> int:
        grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
        def dfs(grid, x, y, m, n):
            if grid[x][y]==1:
                return
            grid[x][y] = 1
            points = [(0, 1), (0, -1), (-1,0), (1, 0)]
            for point in points:
                dx, dy = point
                if 0<=x+dx<m and 0<=y+dy<n:
                    if grid[dx+x][dy+y]==0:
                        dfs(grid, x+dx, y+dy, m, n)

        m = len(grid)
        n = len(grid[0])
        count = 0
        for i in range(m):
            dfs(grid, i,0, m, n)
            dfs(grid, i, n-1, m, n)
        for j in range(n):
            dfs(grid, 0, j, m ,n)
            dfs(grid, m-1, j, m, n)

        for i in range(m):
            for j in range(n):
                if grid[i][j]==0:
                    dfs(grid, i,j, m, n)
                    count += 1
        return count

a = Solution()
grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]

a.closedIsland(grid)


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
        stack = [root]
        vals = []

        while stack:
            cur = stack.pop()
            vals.append(cur.val)

            if cur.left is not None:
                stack.append(cur.left)

            if cur.right is not None:
                stack.append(cur.right)

        vals.sort()

        return vals[k - 1]