# -*- coding: utf-8 -*-
r"""Plot visit output

This exports:

  -

This depends on:

  -
Examples of usage:

  - default usage:

        python -m

descriptions

Notes:
    on the name standard:
        step - computational step in model
        vtu_step - indexing of steps of vtu outputs. (e.g. step = 0, vtu_step = 0; step = 1, vtu_step = 100)
        vtu_snapshot - indexing of output files of vtu (e.g. solution-00001.pvtu, then vtu_snapshot = 1)
"""
import numpy as np
import sys, os, argparse
import json, re
# import pathlib
import subprocess
import numpy as np
# from matplotlib import cm
from matplotlib import pyplot as plt
import shilofue.Plot as Plot
import shilofue.ParsePrm as ParsePrm
from shilofue.CaseOptions import CASE_OPTIONS
from shilofue.PlotDepthAverage import ExportData
import warnings

# directory to the aspect Lab
ASPECT_LAB_DIR = os.environ['ASPECT_LAB_DIR']
RESULT_DIR = os.path.join(ASPECT_LAB_DIR, 'results')
# directory to shilofue
shilofue_DIR = os.path.join(ASPECT_LAB_DIR, 'shilofue')

sys.path.append(os.path.join(ASPECT_LAB_DIR, 'utilities', "python_scripts"))
import Utilities

def Usage():
    print("\
Visualization with visit\n\
\n\
Examples of usage: \n\
\n\
  - translate script: \n\
\n\
        Lib_PlotVisit visit_options -i $TwoDSubduction_DIR/latent_heat_issue/cookbook_latent-heat -sr temperature.py\n\
            -sr: the relative path under the visit_script folder\n\
\n\
  - run script: \n\
        Lib_PlotVisit run -i $TwoDSubduction_DIR/non_linear34/eba_low_tol_newton_shift_CFL0.8/visit_scripts/slab.py\n\
\n\
  - run vtk scripts: \n\
        Lib_PlotVisit vtk_options -i /home/lochy/ASPECT_PROJECT/TwoDSubduction/non_linear34/eba_low_tol_newton_shift_CFL0.8_lh -p TwoDSubduction_MOW -s 25\n\
            -p operations, available operations are: \n\
                TwoDSubduction_MOW: pull out tentative MOW from temperature: \n\
                TwoDSubduction_SlabAnalysis: analyze slab morphology: \n\
        ")

class VISIT_OPTIONS(CASE_OPTIONS):
    """
    parse .prm file to a option file that bash can easily read
    """
    def Interpret(self, **kwargs):
        """
        Interpret the inputs, to be reloaded in children
        kwargs: options
            steps (int): plot some steps
            last_step(list): plot the last few steps
        """
        steps = kwargs.get('steps', None)
        last_step = kwargs.get('last_step', None)
        time_interval = kwargs.get('time_interval', None)
        # call function from parent
        CASE_OPTIONS.Interpret(self)
        # particle file
        particle_file = os.path.join(self._output_dir, 'particles.visit')
        if os.access(particle_file, os.R_OK):
            self.options["VISIT_PARTICLE_FILE"] = particle_file
        # visit file
        self.options["VISIT_FILE"] = self.visit_file
        self.options["PARAVIEW_FILE"] = self.paraview_file
        # data types
        self.options["HAS_DYNAMIC_PRESSURE"] = '0'
        try:
            visualization_output_variables = self.idict['Postprocess']['Visualization']['List of output variables']
        except KeyError:
            pass
        else:
            if re.match('.*nonadiabatic\ pressure', visualization_output_variables):
                self.options["HAS_DYNAMIC_PRESSURE"] = '1'

        # get all the available snaps for ploting by checking on the existence of the pvtu file
        # the correspondent, time, time step are also figured out.
        graphical_snaps_guess, times_guess, time_steps_guess = GetSnapsSteps(self._case_dir, 'graphical')
        graphical_snaps = []
        time_steps = []
        times = []
        for i in range(len(graphical_snaps_guess)):
            graphical_snap = graphical_snaps_guess[i]
            time_step = time_steps_guess[i]
            _time = times_guess[i]
            pvtu_file_path = os.path.join(self.options["DATA_OUTPUT_DIR"], "solution", "solution-%05d.pvtu" % graphical_snap)
            if os.path.isfile(pvtu_file_path):
                graphical_snaps.append(graphical_snap)
                time_steps.append(time_step)
                times.append(_time)
        self.all_graphical_snaps = graphical_snaps
        self.all_graphical_timesteps = time_steps
        self.options['ALL_AVAILABLE_GRAPHICAL_SNAPSHOTS'] = str(graphical_snaps)
        self.options['ALL_AVAILABLE_GRAPHICAL_TIMESTEPS'] = str(time_steps)
        self.options['ALL_AVAILABLE_GRAPHICAL_TIMES'] = str(times)
        particle_snaps, _, _ = GetSnapsSteps(self._case_dir, 'particle')
        self.options['ALL_AVAILABLE_PARTICLE_SNAPSHOTS'] = str(particle_snaps)
        particle_output_dir = os.path.join(self._output_dir, "slab_morphs")
        self.options["PARTICLE_OUTPUT_DIR"] = particle_output_dir
        # get the last step in the series
        try:
            self.last_step = max(0, graphical_snaps[-1] - int(self.options['INITIAL_ADAPTIVE_REFINEMENT']))  # it is the last step we have outputs
        except IndexError:
            # no snaps, stay on the safe side
            self.last_step = -1
        # set steps to plot
        # Priority:
        #   1. a list of steps
        #   2. the last few steps
        #   3. only the last step
        if type(steps) == list:
            for step in steps:
                assert(type(step) == int)
            self.options['GRAPHICAL_STEPS'] = steps  # always plot the 0 th step
        elif type(time_interval) is float and time_interval > 0.0:
            times_ndarray = np.array(times)
            time_series_from_interval = np.arange(0.0, times[-1], time_interval, dtype=float)
            self.options['GRAPHICAL_STEPS'] = []
            for i in range(time_series_from_interval.size):
                _time = time_series_from_interval[i]
                idx = np.argmin(abs(times_ndarray - _time))
                self.options['GRAPHICAL_STEPS'].append(graphical_snaps[idx] - int(self.options['INITIAL_ADAPTIVE_REFINEMENT']))
        elif type(last_step) == int:
            # by this option, plot the last few steps
            self.options['GRAPHICAL_STEPS'] = [0]  # always plot the 0 th step
            self.options['GRAPHICAL_STEPS'] += [i for i in range(max(self.last_step - last_step + 1, 0), self.last_step + 1)]
        else:
            # by default append the first and the computing step.
            self.options['GRAPHICAL_STEPS'] = [0]
            if self.last_step > 0:
                self.options['GRAPHICAL_STEPS'].append(self.last_step)

        # get time steps
        self.options['GRAPHICAL_TIME_STEPS'] = []
        for step in self.options['GRAPHICAL_STEPS']:
            found = False
            for i in range(len(graphical_snaps)):
                if step == max(0, graphical_snaps[i] - int(self.options['INITIAL_ADAPTIVE_REFINEMENT'])):
                    found = True
                    self.options['GRAPHICAL_TIME_STEPS'].append(time_steps[i])
            if not found:
                warnings.warn("%s: step %d is not found" % (Utilities.func_name(), step))
            # Utilities.my_assert(found, ValueError, "%s: step %d is not found" % (Utilities.func_name(), step))

    def visit_options(self, extra_options):
        '''
        deprecated
        '''
        # optional settings
        for key, value in extra_options.items():
            # slab
            if key == 'slab':
                self.options['IF_PLOT_SLAB'] = 'True'
                self.options['GRAPHICAL_STEPS'] = value.get('steps', [0])
                self.options['IF_DEFORM_MECHANISM'] = value.get('deform_mechanism', 0)
            # export particles for slab morph
            elif key == 'slab_morph':
                self.options['IF_EXPORT_SLAB_MORPH'] = 'True'
                # check directory
                if not os.path.isdir(particle_output_dir):
                    os.mkdir(particle_output_dir)

    def vtk_options(self, **kwargs):
        '''
        options of vtk scripts
        '''
        generate_horiz_file = kwargs.get('generate_horiz', False)
        operation = kwargs.get('operation', 'default')
        vtu_step = int(kwargs.get('vtu_step', 0))
        # houriz_avg file
        if generate_horiz_file:
            _time, time_step = self.get_time_and_step(vtu_step)
            depth_average_path = os.path.join(self.options["DATA_OUTPUT_DIR"], 'depth_average.txt')
            assert(os.path.isfile(depth_average_path))
            output_dir = os.path.join(self._case_dir, 'temp_output')
            try:  # This works better in parallel
                os.mkdir(output_dir)
            except FileExistsError:
                pass
            _, ha_output_file = ExportData(depth_average_path, output_dir, time_step=time_step, fix_time_step=True)
            self.options['VTK_HORIZ_FILE'] = ha_output_file
        else:
            self.options['VTK_HORIZ_FILE'] = os.path.join(ASPECT_LAB_DIR, 'output', 'depth_average_output')
        # directory to output from vtk script
        self.options['VTK_OUTPUT_DIR'] = os.path.join(self._case_dir, "vtk_outputs")
        if not os.path.isdir(self.options['VTK_OUTPUT_DIR']):
            os.mkdir(self.options['VTK_OUTPUT_DIR'])
        # file to read in vtk
        Utilities.my_assert((vtu_step >= 0 and vtu_step <= self.last_step), ValueError, "vtu_step needs to be within the range of [%d, %d]" % (0, self.last_step))  # check the range of steps
        self.options['PVTU_FILE'] = os.path.join(self._output_dir, "solution", "solution-%05d.pvtu" % (vtu_step + int(self.options['INITIAL_ADAPTIVE_REFINEMENT'])))
        # type of operation
        self.options['OPERATION'] = operation

    def get_time_and_step(self, vtu_step):
        '''
        Convert vtu_step to step and time in model
        '''
        assert(len(self.all_graphical_snaps) > 0)
        assert(len(self.all_graphical_timesteps) > 0)
        # find step in all available steps
        found = False
        i = 0
        for snap_shot in self.all_graphical_snaps:
            if vtu_step == max(0, int(snap_shot) - int(self.options['INITIAL_ADAPTIVE_REFINEMENT'])):
                found = True
                step = int(self.all_graphical_timesteps[i])
            i += 1
        Utilities.my_assert(found, ValueError, "%s: vtu_step %d is not found" % (Utilities.func_name(), vtu_step))
        time = self.Statistics.GetTime(step)
        return time, step

    def get_time_and_step_by_snapshot(self, vtu_snapshot):
        '''
        Convert vtu_snapshot to step and time in model
        '''
        assert(len(self.all_graphical_snaps) > 0)
        assert(len(self.all_graphical_timesteps) > 0)
        # find step in all available steps
        found = False
        i = 0
        for snap_shot in self.all_graphical_snaps:
            if vtu_snapshot == snap_shot:
                found = True
                step = int(self.all_graphical_timesteps[i])
            i += 1
        Utilities.my_assert(found, ValueError, "%s: vtu_snapshot %d is not found" % (Utilities.func_name(), vtu_snapshot))
        time = self.Statistics.GetTime(step)
        return time, step

class PARALLEL_WRAPPER_FOR_VTK():
    '''
    a parallel wrapper for analyzing slab morphology
    Attributes:
        name(str): name of this plot
        case_dir (str): case directory
        module (a function): a function to use for plotting
        last_pvtu_step (str): restart from this step, as there was previous results
        if_rewrite (True or False): rewrite previous results if this is true
        pvtu_steps (list of int): record the steps
        outputs (list of str): outputs
    '''
    def __init__(self, name, module, **kwargs):
        '''
        Initiation
        Inputs:
            name(str): name of this plot
            module (a function): a function to use for plotting
            kwargs (dict)
                last_pvtu_step
                if_rewrite
        '''
        self.name = name
        self.module = module
        self.last_pvtu_step = kwargs.get('last_pvtu_step', -1)
        self.if_rewrite = kwargs.get('if_rewrite', False)
        self.do_assemble = kwargs.get('assemble', True)
        self.kwargs = kwargs
        self.pvtu_steps = []
        self.outputs = []
        pass

    def configure(self, case_dir):
        '''
        configure
        Inputs:
            case_dir (str): case diretory to assign
        '''
        os.path.isdir(case_dir)
        self.case_dir = case_dir

    def __call__(self, pvtu_step):
        '''
        call function
        Inputs:
            pvtu_step (int): the step to plot
        '''
        expect_result_file = os.path.join(self.case_dir, 'vtk_outputs', '%s_s%06d' % (self.name, pvtu_step))
        if pvtu_step <= self.last_pvtu_step and not self.if_rewrite:
            # skip existing steps
            return 0
        if os.path.isfile(expect_result_file) and not self.if_rewrite:
            # load file content
            print("%s: previous result exists(%s), load" % (Utilities.func_name(), expect_result_file))
            with open(expect_result_file, 'r') as fin:
                pvtu_step = int(fin.readline())
                output = fin.readline()
        else:
            if self.do_assemble:
                # here the outputs from individual steps are combined together
                if pvtu_step == 0:
                    # start new file with the 0th step
                    pvtu_step, output = self.module(self.case_dir, pvtu_step, new=True, **self.kwargs)
                else:
                    pvtu_step, output = self.module(self.case_dir, pvtu_step, **self.kwargs)
                with open(expect_result_file, 'w') as fout:
                    fout.write('%d\n' % pvtu_step)
                    fout.write(output)
                print("%s: pvtu_step - %d, output - %s" % (Utilities.func_name(), pvtu_step, output))
                self.pvtu_steps.append(pvtu_step) # append to data
                self.outputs.append(output)
            else:
                # otherwise, just call the module for each steps
                if pvtu_step == 0:
                    # start new file with the 0th step
                    self.module(self.case_dir, pvtu_step, new=True, **self.kwargs)
                else:
                    self.module(self.case_dir, pvtu_step, **self.kwargs)
        return 0

    def assemble(self):
        '''
        Returns:
            pvtu_steps
            outputs
        '''
        assert(len(self.pvtu_steps) == len(self.outputs))
        length = len(self.pvtu_steps)
        # bubble sort
        for i in range(length):
            for j in range(i+1, length):
                if self.pvtu_steps[j] < self.pvtu_steps[i]:
                    temp = self.pvtu_steps[i]
                    self.pvtu_steps[i] = self.pvtu_steps[j]
                    self.pvtu_steps[j] = temp
                    temp = self.outputs[i]
                    self.outputs[i] = self.outputs[j]
                    self.outputs[j] = temp
        return self.pvtu_steps, self.outputs

    def delete_temp_files(self, pvtu_steps):
        '''
        delete temp files
        Inputs:
            pvtu_steps(list of int): step to look for
        '''
        print('delete temp files')
        for pvtu_step in pvtu_steps:
            expect_result_file = os.path.join(self.case_dir, 'vtk_outputs', '%s_s%06d' % (self.name, pvtu_step))
            if os.path.isfile(expect_result_file):
                os.remove(expect_result_file)

    def clear(self):
        '''
        clear data
        '''
        self.pvtu_steps = []
        self.outputs = []

class PREPARE_RESULT_OPTIONS(CASE_OPTIONS):
    """
    parse .prm file to a option file that bash can easily read
    """
    def Interpret(self, **kwargs):
        """
        Interpret the inputs, to be reloaded in children
        kwargs: options
        """
        # call function from parent
        CASE_OPTIONS.Interpret(self)
        visual_step = kwargs.get('step', 0)
        # step & format for a visit output
        self.options['NUMERICAL_STEP'] = "%06d" % visual_step
        self.options['GRAPHICAL_STEP'] =  "%06d" % (visual_step + int(self.options['INITIAL_ADAPTIVE_REFINEMENT']))
        # time
        try:
            time_between_graphical_output = float(self.idict['Postprocess']['Visualization']['Time between graphical output'])
        except KeyError:
            time_between_graphical_output = 1e8
        time = visual_step * time_between_graphical_output
        step = self.Statistics.GetStep(time)
        self.options['STEP_TIME_STAMP'] = "step %d, %.4e yrs" % (step, time)
        self.options['NUMERICAL_TIME_4E'] = "%.4e" % time

def GetSnapsSteps(case_dir, type_='graphical'):
    '''
    Get snaps for visualization from the record of statistic file.
    This function requires a consistent statistic file with respect to the vtu files.
    Checking the statistic file is more on the safe side from checking the vtu file, since a newer run might overight the older result.
    '''
    case_output_dir = os.path.join(case_dir, 'output')

    # import parameters
    prm_file = os.path.join(case_dir, 'output', 'original.prm')
    Utilities.my_assert(os.access(prm_file, os.R_OK), FileNotFoundError,
              'case prm file - %s cannot be read' % prm_file)
    with open(prm_file, 'r') as fin:
        idict = ParsePrm.ParseFromDealiiInput(fin)

    # import statistics file
    Statistics = Plot.STATISTICS_PLOT_OLD('Statistics')
    statistic_file = os.path.join(case_output_dir, 'statistics')
    Utilities.my_assert(os.access(statistic_file, os.R_OK), FileNotFoundError,
              'case statistic file - %s cannot be read' % prm_file)
    Statistics.ReadHeader(statistic_file)
    Statistics.ReadData(statistic_file)
    col_time = Statistics.header['Time']['col']

    # final time
    final_time = Statistics.data[-1, col_time]

    # time interval
    # graphical
    try:
        time_between_graphical_output = float(idict['Postprocess']['Visualization']['Time between graphical output'])
    except KeyError:
        time_between_graphical_output = 1e8
    total_graphical_outputs = int(final_time / time_between_graphical_output) + 1
    graphical_times = [i*time_between_graphical_output for i in range(total_graphical_outputs)]
    graphical_steps = [Statistics.GetStep(time) for time in graphical_times]
    # particle
    try:
        time_between_particles_output = float(idict['Postprocess']['Particles']['Time between data output'])
        total_particles_outputs = int(final_time / time_between_particles_output) + 1
    except KeyError:
        time_between_particles_output = 1e8
        total_particles_outputs = 0
    particle_times = [i*time_between_particles_output for i in range(total_particles_outputs)]
    particle_steps = [Statistics.GetStep(time) for time in particle_times]

    # initial_snap
    try:
        initial_snap = int(idict['Mesh refinement']['Initial adaptive refinement'])
    except KeyError:
        initial_snap = 0

    # end snap
    snaps = [0]
    if type_ == 'graphical':
        start_ = initial_snap
        end_ = total_graphical_outputs + initial_snap
        snaps = list(range(start_, end_))
        times = graphical_times
        steps = graphical_steps
    elif type_ == 'particle':
        start_ = 0
        end_ = total_particles_outputs
        snaps = list(range(start_, end_))
        times = particle_times
        steps = particle_steps

    return snaps, times, steps

def RunScripts(visit_script):
    '''
    run script in visit
    Inputs:
    '''
    os.system("echo \"exit()\" | eval \"visit -nowin -cli -s %s\"" % visit_script)

def PrepareVTKOptions(VISIT_OPTIONS, case_dir, type, **kwargs):
    '''
    prepare vtk options for vtk scripts
    Inputs:
        VISIT_OPTIONS: class for the options of visit
        kwargs:
            output
            vtu_step
            include_step_in_filename
    '''
    vtu_step = kwargs.get('vtu_step', 0)
    generate_horiz = kwargs.get('generate_horiz', False)
    operation = kwargs.get('operation', 'default')
    include_step_in_filename = kwargs.get('include_step_in_filename', False)
    vtk_config_dir = os.path.join(ASPECT_LAB_DIR, 'vtk_scripts', "inputs")
    assert(os.path.isdir(vtk_config_dir))
    vtk_config_file = os.path.join(vtk_config_dir, "%s.input" % type)
    Visit_Options = VISIT_OPTIONS(case_dir)
    Visit_Options.Interpret()
    Visit_Options.vtk_options(vtu_step=vtu_step, generate_horiz=generate_horiz, operation=operation)
    Visit_Options.read_contents(vtk_config_file)
    Visit_Options.substitute()
    try:
        ofile = kwargs['output']
    except KeyError:
        if include_step_in_filename:
            ofile = os.path.join(Visit_Options.options['VTK_OUTPUT_DIR'], "%s_s%06d" % (os.path.basename(vtk_config_file), vtu_step))
        else:
            ofile = os.path.join(Visit_Options.options['VTK_OUTPUT_DIR'], os.path.basename(vtk_config_file))
    ofile_path = Visit_Options.save(ofile)
    print('%s: %s generated' % (Utilities.func_name(), ofile_path))
    # get time and step
    _time, step = Visit_Options.get_time_and_step(vtu_step)
    return ofile_path, _time, step

def RunVTKScripts(operation, vtk_option_path):
    '''
    run script of vtk
    Inputs:
    Return:
        filename (str): file generated from a vtk script
    '''
    vtk_executable = os.path.join(ASPECT_LAB_DIR, 'vtk_scripts', 'build', operation)
    print("run vtk script: \n%s %s" % (vtk_executable, vtk_option_path))
    completed_process = subprocess.run([vtk_executable, vtk_option_path], capture_output=True, text=True)
    print("\nOutput from vtk script:\n %s" % completed_process.stdout)
    return completed_process.stdout

def main():
    '''
    main function of this module
    Inputs:
        sys.arg[1](str):
            commend
        sys.arg[2, :](str):
            options
    '''
    _commend = sys.argv[1]
    # parse options
    parser = argparse.ArgumentParser(description='Parse parameters')
    parser.add_argument('-i', '--inputs', type=str,
                        default='',
                        help='Some inputs')
    parser.add_argument('-sr', '--script', type=str,
                        default='temperature.py',
                        help='script')
    parser.add_argument('-j', '--json_file', type=str,
                        default='./config_case.json',
                        help='Filename for json file')
    parser.add_argument('-p', '--operation', type=str,
                        default='',
                        help='operation to take')
    parser.add_argument('-s', '--step', type=int,
                        default='0',
                        help='step')
    parser.add_argument('-ls', '--last_step', type=bool,
                        default=False,
                        help='Only plot last step (bool value)')
    _options = []
    try:
        _options = sys.argv[2: ]
    except IndexError:
        pass
    arg = parser.parse_args(_options)

    # commands
    if _commend in ['-h', '--help']:
        # example:
        Usage()

    elif _commend == 'visit_options':
        # output bash options to a file that could be
        # read by bash script
        # initiate class object
        case_dir = arg.inputs
        Visit_Options = VISIT_OPTIONS(case_dir)
        # call function
        Visit_Options.Interpret()
        # ofile = os.path.join('visit_scripts', 'slab_sph.py')
        ofile = os.path.join('visit_scripts', os.path.basename(arg.script))
        visit_script = os.path.join(ASPECT_LAB_DIR, 'visit_scripts', arg.script)
        visit_base_script = os.path.join(ASPECT_LAB_DIR, 'visit_scripts', 'base.py')  # base.py : base file
        Visit_Options.read_contents(visit_base_script, visit_script)  # this part combines two scripts
        Visit_Options.substitute()  # substitute keys in these combined file with values determined by Interpret() function
        ofile_path = Visit_Options.save(ofile, relative=True)  # save the altered script
        pass

    elif _commend == 'vtk_options':
        vtk_option_path, _, _ = PrepareVTKOptions(VISIT_OPTIONS, arg.inputs, arg.operation, step=arg.step)
        RunVTKScripts(arg.operation, vtk_option_path)

    elif _commend == 'run':
        RunScripts(arg.inputs)

    else:
        # no such option, give an error message
        raise ValueError('No commend called %s, please run -h for help messages' % _commend)

# run script
if __name__ == '__main__':
    main()


import pyautogui

import os
import time

from pynput import mouse

import pynput, time

# 鼠标右键坏了 将鼠标中间按键按下模拟鼠标右键
def on_move(x, y):
    print('Pointer moved to {0}'.format((x, y)))

def on_click(x, y, button, pressed):
    print(button)
    # Button.middle left right
    if button==pynput.mouse.Button.middle:
        pyautogui.rightClick()
        return True
    return True

def on_scroll(x, y, dx, dy):
    print('Scrolled {0} at {1}'.format(
        'down' if dy < 0 else 'up',
        (x, y)))

# Collect events until released
with pynput.mouse.Listener(
        # on_move=on_move,
        on_click=on_click,
        on_scroll=on_scroll) as listener:

    listener.join()

# pyautogui.rightClick()


# -*- coding:utf-8 -*-
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, HttpResponseRedirect,JsonResponse
from django.shortcuts import redirect,render
from django.contrib.auth.decorators import login_required
from django.template.loader import get_template
from p_mes.models import SumReport
from django.db import connection
from django.db.models import Avg,Sum,Count
import time,datetime,json,pyodbc

# 日期转换
def timestr(str,tf):
    tsm=int(time.mktime(time.strptime(str,"%Y-%m-%d"))) + (tf*24*3600)
    return time.strftime("%Y-%m-%d",time.localtime(tsm))

#json自定义编码
class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8');
        elif isinstance(obj,pyodbc.Row):
            return str(list(obj)[0])
        return json.JSONEncoder.default(self, obj)

#SQL转JSON
def dictfetchall(cursor):
    columns = [col[0] for col in cursor.description]
    return [dict(zip(columns, row))for row in cursor.fetchall()]

#战情资讯
@login_required
@csrf_exempt
def dashboard(request):
    userCHNname =request.user.first_name
    username =request.user.username

    #局部刷新
    if  request.is_ajax():
        content={}
        dep=request.POST.get('dep')
        cell=request.POST.get('cell')
        std=request.POST.get('std')
        etd=request.POST.get('etd')
        dst=request.POST.get('dst')
        sle=request.POST.get('sle')
        if len(etd)==0:
            etd='2018-01-01'
            std='2018-01-01'
            std2='2018-01-01'
        std2=timestr(etd,-28)
        if (std2>std):
            std2=std
        if dst=='Weekly':
            sql2="select datepart(wk,WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by datepart(wk,WorkingDate) order by datepart(wk,WorkingDate)"
            sql3="select datepart(wk,WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where Cell='"+ cell +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by datepart(wk,WorkingDate) order by datepart(wk,WorkingDate)"
        elif dst=='Monthly':
            sql2="select month(WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by month(WorkingDate) order by month(WorkingDate)"
            sql3="select month(WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where Cell='"+ cell +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by month(WorkingDate) order by month(WorkingDate)"
        else:
            sql2="select right(convert(varchar(12),WorkingDate,112),4) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by WorkingDate order by WorkingDate"
            sql3="select right(convert(varchar(12),WorkingDate,112),4) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where Cell='"+ cell +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by WorkingDate order by WorkingDate"

        if sle=='1':
        #by dep
            sql="select b.name as cell  from p_admin_department a inner join p_admin_department b on a.code=b.parent_id where b.category='C' and a.name='" + dep + "' order by  b.name"
            sql5="select case when len(FailureMode)=0 then 'NA' else FailureMode end as FM,sum(FailQty) as Qty from MES2017.dbo.v_FQC_Detail where Qdate between '"+ std2 +"' and '"+ etd +"' and PL='" + dep + "'  and PPM>0 group by FailureMode order by sum(FailQty) desc"
            cells=dictfetchall(connection.cursor().execute(sql))
            DQ=SumReport.objects.filter(PL='' + dep + '',WorkingDate__range=('' + std + '', ''+etd+'')).values('PL').annotate(FG=Sum('CompleteQty'),PQ=Sum('PQ'),UPPH=Avg('UPPH'),URT=Avg('URT'),PPM=Avg('PPM'),FCY=Avg('FCY'),ACR=Avg('Productivity'),EFY=Avg('Efficiency')).values('UPPH','FCY','PPM','URT','ACR','EFY','FG','PQ')
        else:
            if sle=='3':
                #update datesum to mes2018
                mes_refresh="exec P_ReportSum '"+ std +"','"+ etd +"'"
                connection.cursor().execute(mes_refresh)
        #by cell,std,etd
            if cell=='CellSUM':
                sql5="select case when len(FailureMode)=0 then 'NA' else FailureMode end as FM,sum(FailQty) as Qty from MES2017.dbo.v_FQC_Detail where Qdate between '"+ std2 +"' and '"+ etd +"' and PL='" + dep + "'  and PPM>0 group by FailureMode order by sum(FailQty) desc"
                DQ=SumReport.objects.filter(PL='' + dep + '',WorkingDate__range=('' + std + '', ''+etd+'')).values('PL').annotate(FG=Sum('CompleteQty'),PQ=Sum('PQ'),UPPH=Avg('UPPH'),URT=Avg('URT'),PPM=Avg('PPM'),FCY=Avg('FCY'),ACR=Avg('Productivity'),EFY=Avg('Efficiency')).values('UPPH','FCY','PPM','URT','ACR','EFY','FG','PQ')
            else:
                sql5="select case when len(FailureMode)=0 then 'NA' else FailureMode end as FM,sum(FailQty) as Qty from MES2017.dbo.v_FQC_Detail where Qdate between '"+ std2 +"' and '"+ etd +"' and Cell='" + cell + "'  and PPM>0 group by FailureMode order by sum(FailQty) desc"
                DQ=SumReport.objects.filter(Cell='' + cell + '',WorkingDate__range=('' + std + '', ''+etd+'')).values('Cell').annotate(FG=Sum('CompleteQty'),PQ=Sum('PQ'),UPPH=Avg('UPPH'),URT=Avg('URT'),PPM=Avg('PPM'),FCY=Avg('FCY'),ACR=Avg('Productivity'),EFY=Avg('Efficiency')).values('UPPH','FCY','PPM','URT','ACR','EFY','FG','PQ')
                sql2=sql3
            cells="CellSUM"
        #cellsum
        sql1="select Cell,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std +"' and '"+ etd +"' group by Cell"
        sql4="select tcode,cast(tucl as varchar(12)) as ucl,cast(tlcl as varchar(12)) as lcl from p_mes_deptarget a left join p_admin_department b on dep_id=b.code where name='"+ dep +"'"
        cellsum=dictfetchall(connection.cursor().execute(sql1))
        datesum=dictfetchall(connection.cursor().execute(sql2))
        targetsum=dictfetchall(connection.cursor().execute(sql4))
        fqcfmsum=dictfetchall(connection.cursor().execute(sql5))
        connection.close

        if DQ is not None:
            try:
                content['EFY']=('%d %%' % (100*float(dict(list(DQ)[0])['EFY'])))
                content['URT']=('%d %%' % (100*float(dict(list(DQ)[0])['URT'])))
                content['ACR']=('%d %%' % (100*float(dict(list(DQ)[0])['ACR'])))
                content['FG']=('%d' % float(dict(list(DQ)[0])['FG']))
                content['PQ']=('%d' % float(dict(list(DQ)[0])['PQ']))
                content['UPPH']=('%.2f' % float(dict(list(DQ)[0])['UPPH']))
                content['PPM']=('%d' % float(dict(list(DQ)[0])['PPM']))
                content['FCY']=('%d %%' % (100*float(dict(list(DQ)[0])['FCY'])))
            except:
                pass
        content['cells']=cells
        content['cellsum']=cellsum
        content['datesum']=datesum
        content['targetsum']=targetsum
        content['fqcfmsum']=fqcfmsum

        return JsonResponse(content)

    #首次加载
    else:
        sql0="select distinct code,name,url,icon,parent_id,level from p_admin_module left join p_admin_profile_module on code=module_id where profile_id='" + username + "' and level='1' UNION select DISTINCT code,name,url,icon,parent_id,level  from p_admin_module a left join p_admin_module_group b on code=module_id left join auth_user_groups c on b.group_id=c.group_id where user_id='" + username + "' and level='1'"
        menus=connection.cursor().execute(sql0).fetchall()

        sql="select a.name from p_admin_department a left join p_admin_profile_department b on  a.code=b.department_id where profile_id='" + username + "' and category='P'"
        deps=connection.cursor().execute(sql).fetchall()
        if len(deps):
            dept= deps[0].name
        else:
            template = get_template('index.html')
            return HttpResponse(template.render(locals()))

        sql="select b.name from p_admin_department a inner join p_admin_department b on a.code=b.parent_id where b.category='C' and a.name='" + dept + "' order by b.name"
        cells=connection.cursor().execute(sql).fetchall()
        connection.close
        startdate=(datetime.date.today()+datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
        enddate=(datetime.date.today()+datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
        template = get_template('pages/dashboard.html')
        return HttpResponse(template.render(locals()))

#子报表查询
@login_required
@csrf_exempt
def dashboardetail(request):
    pass


import functools
from typing import Optional

from binary_tree_with_parent_prototype import BinaryTreeNode
from test_framework import generic_test
from test_framework.binary_tree_utils import must_find_node
from test_framework.test_failure import TestFailure
from test_framework.test_utils import enable_executor_hook

"""

Compute lowest common ancestor given nodes that have parent pointers

Without parents, can't go back up the tree.
Have to start at root, find both nodes, then compare the paths.

With parent, can go back up the tree.
Build ancestor paths to the root...O(log n) (O(n) if unbalanced)
Iterate through the path nodes to find last match...O(log n) (O(n) if unbalanced)
-> O(log n)

If both nodes are same distance from root, LCA is first corresponding node in path starting from each node.

Worst case is always going all the way to the root, which has to take O(log n) or O(n) unabalanced

"""

def path_to_root(node):
    p = []
    while node != None:
        p.append(node)
        node = node.parent
    return p

def lca(node0: BinaryTreeNode,
        node1: BinaryTreeNode) -> Optional[BinaryTreeNode]:
    p1 = path_to_root(node0)
    p2 = path_to_root(node1)

    # paths are from nodes to root, so traverse from ends
    i = -1
    while p1[i] == p2[i]:
        i -= 1
        if abs(i) > len(p1) or abs(i) > len(p2):
            break

    return p1[i+1]

@enable_executor_hook
def lca_wrapper(executor, tree, node0, node1):
    result = executor.run(
        functools.partial(lca, must_find_node(tree, node0),
                          must_find_node(tree, node1)))

    if result is None:
        raise TestFailure('Result can\'t be None')
    return result.data

if __name__ == '__main__':
    exit(
        generic_test.generic_test_main('lowest_common_ancestor_with_parent.py',
                                       'lowest_common_ancestor.tsv',
                                       lca_wrapper))


from typing import List, Union
from project.vehicles.cargo_van import CargoVan
from project.vehicles.passenger_car import PassengerCar
from project.user import User
from project.route import Route

class ManagingApp:

    VALID_CAR_TYPES = {'PassengerCar': PassengerCar,
                       'CargoVan': CargoVan}

    def __init__(self):
        self.users: List[User] = []
        self.vehicles: List[Union[CargoVan, PassengerCar]] = []
        self.routes: List[Route] = []

    def register_user(self, first_name: str, last_name: str, driving_license_number: str):
        user = self._get_user_by_driving_license_number(driving_license_number)
        if user:
            return f'{driving_license_number} has already been registered to our platform.'
        reg_user = User(first_name, last_name, driving_license_number)
        self.users.append(reg_user)
        return f'{first_name} {last_name} was successfully registered under DLN-{driving_license_number}'

    def upload_vehicle(self, vehicle_type: str, brand: str, model: str, license_plate_number: str):
        if vehicle_type not in self.VALID_CAR_TYPES:
            return f'Vehicle type {vehicle_type} is inaccessible.'
        vehicle = self._get_vehicle_by_register_plate_number(license_plate_number)
        if vehicle:
            return f'{license_plate_number} belongs to another vehicle.'
        vehicle_to_reg = self.VALID_CAR_TYPES[vehicle_type](brand, model, license_plate_number)
        self.vehicles.append(vehicle_to_reg)
        return f'{brand} {model} was successfully uploaded with LPN-{license_plate_number}.'

    def allow_route(self, start_point: str, end_point: str, length: float):
        route_to_find = self._check_if_route_exists(start_point, end_point)
        if route_to_find:
            if route_to_find.length == length:
                return f'{start_point}/{end_point} - {length} km had already been added to our platform.'
            if route_to_find.length < length:
                return f'{start_point}/{end_point} shorter route had already been added to our platform.'
            if route_to_find.length > length:
                route_to_find.is_locked = True
        route_to_reg = Route(start_point, end_point, length, len(self.routes) + 1)
        self.routes.append(route_to_reg)
        return f'{start_point}/{end_point} - {length} km is unlocked and available to use.'

    def make_trip(self, driving_license_number: str, license_plate_number: str, route_id: int,  is_accident_happened: bool):
        user = self._get_user_by_driving_license_number(driving_license_number)
        if user.is_blocked:
            return f'User {driving_license_number} is blocked in the platform! This trip is not allowed.'

        vehicle = self._get_vehicle_by_register_plate_number(license_plate_number)
        if vehicle.is_damaged:
            return f'Vehicle {license_plate_number} is damaged! This trip is not allowed.'

        route = self._get_route_by_id(route_id)
        if route.is_locked:
            return f'Route {route_id} is locked! This trip is not allowed.'

        vehicle.drive(route.length)
        if is_accident_happened:
            vehicle.change_status()
            user.decrease_rating()
        else:
            user.increase_rating()
        return str(vehicle)

    def repair_vehicles(self, count: int):
        all_damaged_vehicles = [v for v in self.vehicles if v.is_damaged]
        sorted_vehicles = sorted(all_damaged_vehicles, key=lambda x: (x.brand, x.model))
        for vehicle in sorted_vehicles[:count]:
            vehicle.recharge()
            vehicle.change_status()
        return f'{len(sorted_vehicles[:count])} vehicles were successfully repaired!'

    def users_report(self):
        result = ['*** E-Drive-Rent ***']
        sorted_users = sorted(self.users, key=lambda x: -x.rating)
        for u in sorted_users:
            result.append(str(u))
        return '\n'.join(result)

    def _get_user_by_driving_license_number(self, driving_license_number):
        searched_user = next((u for u in self.users if u.driving_license_number == driving_license_number), None)
        return searched_user

    def _get_vehicle_by_register_plate_number(self, license_plate_number):
        searched_vehicle = next((v for v in self.vehicles if v.license_plate_number == license_plate_number), None)
        return searched_vehicle

    def _check_if_route_exists(self, start_point, end_point):
        is_route = next((r for r in self.routes if r.start_point == start_point and
                         r.end_point == end_point), None)
        return is_route

    def _get_route_by_id(self, route_id):
        route = next((r for r in self.routes if r.route_id == route_id), None)
        return route


#!/usr/bin/env python3

from typing import Tuple
from dataclasses import dataclass
import numpy as np
import torch as th
import cv2
nn = th.nn
F = nn.functional
from functools import partial
from tqdm.auto import tqdm
from torchvision.transforms import (
    Compose, ToTensor, Normalize, Resize)

from model import SlotAttentionAE
from dataset import CLEVRDataset
from util import (get_new_dir, ensure_dir, load_ckpt, save_ckpt)
import einops

@dataclass
class Config:
    batch_size: int = 16
    img_size: Tuple[int, int] = (128, 128)
    num_slots: int = 8
    ckpt_path: str = '/tmp/slot-attention/run-009/last.pt'
    num_workers:int = 4

def main():
    cfg = Config()
    device: th.device = th.device('cuda')
    model = SlotAttentionAE(cfg.img_size, cfg.num_slots).to(device)
    load_ckpt(cfg.ckpt_path, model)

    transform = Compose([
        ToTensor(),
        Resize(cfg.img_size),
        Normalize(0.5, 0.5, True),
    ])
    val_dataset = CLEVRDataset(
        '/media/ssd/datasets/CLEVR/CLEVR_v1.0/', 'val',
        transform=transform)
    val_data_loader = th.utils.data.DataLoader(val_dataset,
                                               batch_size=cfg.batch_size,
                                               num_workers=cfg.num_workers,
                                               shuffle=False)
    model.eval()
    for images in tqdm(val_data_loader):
        images = images.to(device)
        with th.no_grad():
            pred_imgs, recs, msks, _ = model(images)
            msks = msks[...,None,:,:]
            slot_imgs = recs * msks + (1 - msks)  # BxSxCXHXW
            slot_vis = slot_imgs * 0.5 + 0.5
            vis = einops.rearrange(slot_vis,
                    'b s c h w -> (b h) (s w) c')
        vis_np = vis.detach().cpu().numpy()
        cv2.imwrite('/tmp/vis.png', (vis_np * 255).astype(np.uint8) )
        break

if __name__ == '__main__':
    main()


from django.shortcuts import get_object_or_404
from products.models import Product
from shipping.models import Shipping
from decimal import Decimal

def cart_contents(request):
    """
    Ensures that the cart contents are available when rendering
    every page
    """
    cart = request.session.get('cart', {})

    cart_items = []
    sub_total = Decimal(0)
    product_count = 0

    for id, quantity in cart.items():
        product = get_object_or_404(Product, pk=id)
        item_total = quantity * product.price
        sub_total += item_total
        product_count += quantity
        cart_items.append({'id': id, 'quantity': quantity, 'product': product, 'total': item_total})

    regions = Shipping.objects.all()
    try:
        default_region = regions[0].name
    except IndexError:
        default_region = ""
    selected_region = request.session.get('selected_region', default_region)
    region_price = Decimal(0)
    for region in regions:
        if region.name == selected_region:
            region_price = region.price
            break

    total = sub_total + region_price

    return {
        'product_count': product_count,
        'cart_items': cart_items,
        'sub_total': sub_total,
        'total': total,
        'regions': regions,
        'selected_region': selected_region,
        'region_price': region_price
    }


# for windows
import pyttsx3

# for chromium
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from time import sleep

def SpeakWindows(query):
    engine = pyttsx3.init("sapi5")
    voices = engine.getProperty("voices")
    engine.setProperty('voices', voices[0].id)
    engine.setProperty('rate', 170)

    print("")
    print(f"You: {query}. ")
    print("")

    engine.say(query)
    engine.runAndWait()

chrome_options = Options()
chrome_options.add_argument("--log-level=3")
chrome_options.headless = True

# chrome driver path
Path = "src\\driver\\chromedriver.exe"

# linking to chrome
driver = webdriver.Chrome(Path, options=chrome_options)
driver.maximize_window()

FreeTTS_Engine_URL = r"https://ttsmp3.com/text-to-speech/British%20English/"
driver.get(FreeTTS_Engine_URL)
ButtonSelection = Select(driver.find_element(by=By.XPATH, value="/html/body/div[4]/div[2]/form/select"))
ButtonSelection.select_by_visible_text("British English / Brian")

def SpeakChromium(query):
    length_of_query = len(str(query))

    if length_of_query == 0:
        pass

    else:
        print("")
        print(f"Ultron: {query}. ")
        print("")

        Data = str(query)
        xpathofsec = '/html/body/div[4]/div[2]/form/textarea'
        driver.find_element(By.XPATH, value=xpathofsec).send_keys(Data)
        driver.find_element(By.XPATH, value='//*[@id="vorlesenbutton"]').click()
        driver.find_element(By.XPATH, value="/html/body/div[4]/div[2]/form/textarea").clear()

        if length_of_query >= 30:
            sleep(4)

        elif length_of_query >= 40:
            sleep(6)

        elif length_of_query >= 55:
            sleep(8)

        elif length_of_query >= 70:
            sleep(10)

        elif length_of_query >= 100:
            sleep(12)

        elif length_of_query >= 120:
            sleep(14)

        else:
            sleep(2)

# SpeakChromium("hello ultron, this is rintron glade")


from openpyxl.workbook import Workbook
from openpyxl import load_workbook

workbook = Workbook()
worksheet = workbook.active

ws1 = workbook.create_sheet('New Shiiit')
ws2 = workbook.create_sheet('Another sheet', 0)

worksheet.title = 'mySheet'

workbook2 = load_workbook('data/regions.xlsx')
new_sheet = workbook2.create_sheet('yet another sheet')
active_sheet = workbook2.active
cell = active_sheet['A1']

active_sheet['A1'] = f'helllllo'
workbook2.save('output/modified.xlsx')


import numpy as np
import os as os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pickle as pickle
from matplotlib import gridspec
import matplotlib.colors as colors
from deepres.simulator.periodic_field_functions import PeriodicPerturbations
proj_folder = os.path.dirname(os.path.realpath(__file__))
# path to save the training data file
save_folder = os.path.join(proj_folder, 'temp')
save_path = os.path.join(save_folder, 'data.npz')
grid_path = os.path.join(proj_folder, 'sample_scripts', 'script_files', '100_100_periodic.pkl')

#data = np.load(save_path)
#X = data['X']
#Y = data['Y']
#datFile =np.load('train_pressure.npz')

datFile =np.load('best_pred_pressure.npz')

X = datFile['perm']
pressure_actual = datFile['pressure']
p_pred = datFile['best_pred']

print(p_pred.shape)
with open(grid_path, 'rb') as input:
    grid = pickle.load(input)
dx, dy = grid.dx, grid.dy
nx = grid.m
gridx = grid.pores.x
gridy = grid.pores.y
x_mat = np.reshape(gridx, (nx,nx), order='F')
y_mat = np.reshape(gridy, (nx,nx), order='F')
# plot permeability

PI = PeriodicPerturbations(grid, None, None)
for i in range(10):

	fig = plt.figure(figsize=(9,4))

	gs = gridspec.GridSpec(1, 2)
	gs.update(left=0.1, right=0.86, hspace=0.1)
	gs1 = gridspec.GridSpec(1, 1)
	gs1.update(left=0.89, right=0.92)
	ax = plt.subplot(gs[0])
	ax2 = plt.subplot(gs[1])
	ax_cmap = plt.subplot(gs1[0])

	cmap = plt.cm.afmhot_r

	p = ax.pcolormesh(x_mat, y_mat, p_pred[i,:,:,0], cmap=plt.cm.coolwarm,vmin = -0.75,vmax =0.75 )

	ax.set_aspect('equal', 'box')

	p2 = ax2.pcolormesh(x_mat, y_mat, pressure_actual[i,:,:,0], cmap=plt.cm.coolwarm ,vmin = -0.75, vmax = 0.75)
	cbar = fig.colorbar(p2, ax_cmap, orientation='vertical')

	ax.set_aspect('equal', 'box')
	fig.savefig(os.path.join(save_folder, 'pressure_both{:}.png'.format(i)), format='png')

	# def compare_trans_mat_vtheta(trans_matrix_1, trans_matrix_2, figure_save_folder, prefix, refsize = 16):

	# fontsize = refsize * 0.8
	# fmt = 'pdf'
	# fig = plt.figure(figsize=(9,4))

	# gs = gridspec.GridSpec(1, 2)
	# gs.update(left=0.1, right=0.86, hspace=0.1)
	# gs1 = gridspec.GridSpec(1, 1)
	# gs1.update(left=0.89, right=0.92)
	# ax = plt.subplot(gs[0])
	# ax2 = plt.subplot(gs[1])
	# ax_cmap = plt.subplot(gs1[0])
	# cmap = plt.cm.afmhot_r
	# max_prob = 1.0
	# # print max_prob
	# p = ax.pcolor(np.sqrt(trans_matrix_1), norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,vmin=0, vmax=max_prob),
	# 	cmap=cmap, linewidth=0, rasterized=True)
	# p2 = ax2.pcolor(np.sqrt(trans_matrix_2), norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,vmin=0, vmax=max_prob),
	# 	cmap=cmap, linewidth=0, rasterized=True)

	# cbar = fig.colorbar(p, ax_cmap, orientation='vertical')
	# tick_array = [0.0, 0.1, 0.2, 0.3, 0.5, 0.7, 1.0]
	# cbar.set_ticks(tick_array)
	# # cbar.set_ticklabels()
	# lp  = 1
	# ax.set_xlabel('previous class ' + r'($v_n$)', labelpad=lp)
	# ax.set_ylabel('next class '+ r'($v_{n+1}$)', labelpad=lp)
	# ax2.set_xlabel('previous class '+ r'($\theta_n$)', labelpad=lp)
	# ax2.set_ylabel('next class '+ r'($\theta_{n+1}$)', labelpad=lp)
	# mat_size = trans_matrix_1.shape[1]
	# ax_bound = [0, mat_size]
	# ax.set_xbound(ax_bound)
	# ax.set_ybound(ax_bound)
	# mat_size = trans_matrix_2.shape[1]
	# ax_bound = [0, mat_size]
	# ax2.set_xbound(ax_bound)
	# ax2.set_ybound(ax_bound)
	# fig_name = prefix +'.'+fmt
	# file_name = os.path.join(figure_save_folder, fig_name)
	# fig.savefig(file_name, format=fmt)
	# plt.close(fig)


from ProductClass import *

# to Store class
# add_product(self, new_product) - takes a product and adds it to the store
# sell_product(self, id) - remove the product from the store's list of products

# given the id (assume id is the index of the product in the list) and print its info.

# inflation(self, percent_increase) - increases the price of each product
# by the percent_increase given (use the method you wrote in the Product class!)

# get_clearance(self, category, percent_discount) - updates all the products
# matching the given category by reducing the price by the percent_discount given
# (use the method you wrote in the Product class!)

import uuid
import time

class Store:
    def __init__(self, name = None, prod_list=[]):
        self.name = name
        self.prod_list = prod_list

    def add_product(self,new_product_name, price, category):
        id = "{}-{}".format(uuid.uuid4(), time.time())
        newprod = Product(id,new_product_name, price, category)
        self.prod_list.append(newprod)

    def sell_product(self, prod_id):
        self.prod_list.pop(prod_id)

    def print_prod_info(self, prod_id=None):
        if prod_id == None:
            print("All products in the store:")
            for item in self.prod_list:
                print(item)
        else:
            print("\nInfo for product with id:", prod_id)
            print(self.prod_list[prod_id], end ="")

    def inflation(self, id, percent_increase):
        prod = Product()
        upd_prod=prod.update_price(percent_increase, self.prod_list[id],is_increased=True)
        self.prod_list[id] = upd_prod

    def get_clearance(self, category_id, percent_discount):
        prod = Product()
        i = 0
        for prod in self.prod_list:
            if prod.category == category_id:
                print("found a catid match!", prod.category, category_id)
                upd_prod=prod.update_price(percent_discount, self.prod_list[i],is_increased=False)
                self.prod_list[i] = upd_prod
            i = i+1

    # def __str__(self):
    #     # Override to print a readable string presentation of your object
    #     # below is a dynamic way of doing this without explicity constructing the string manually
    #     return ', '.join(['{key}={value}'.format(key=key, value=self.__dict__.get(key)) for key in self.__dict__])

mystore= Store()
mystore.add_product("Buble", 20, 11)
mystore.add_product("Chair", 250, 8)
mystore.add_product("Table", 25, 3)
mystore.add_product("T", 5, 3)
mystore.add_product("B", 15, 3)
mystore.add_product("D", 3, 3)
mystore.add_product("Plate", 20, 11)

mystore.print_prod_info()
mystore.print_prod_info(1)

print("\nAfter inflation:")
mystore.inflation(0,2)

mystore.print_prod_info()
print('After clearence')
mystore.get_clearance(3,2)
mystore.print_prod_info()

mystore.sell_product(2)
print("\nSold product with id = 2")
mystore.print_prod_info()

#print(mystore)


"""
In this file, I have created the connection to my aws S3 bucket
Then i have uploaded my csv file to s3 bucket
"""

import boto
import boto3
import pandas as pd
from io import StringIO
import boto.s3.connection
from Perform_logging import log

class Bucket:
    """
    A class representing a bucket in Amazon S3.

    Attributes:
    -----------
    file_name : str
        The name of the CSV file to be uploaded to S3 bucket.
    access_key : str
        The AWS access key.
    secret_key : str
        The AWS secret access key.

    Methods:
    --------
    create_connection_bucket() -> None:
        Creates a connection to an S3 bucket using the given access and secret keys.
    upload_file_to_s3bucket() -> None:
        Uploads the CSV file to the S3 bucket.
    run() -> None:
        Runs the complete process of creating the connection to the S3 bucket and uploading the CSV file to the S3 bucket.
    """

    def __init__(self, file_name: str, access_key: str, secret_key: str) -> None:
        """
        Constructs a Bucket object.

        Parameters:
        -----------
        file_name : str
            The name of the CSV file to be uploaded to S3 bucket.
        access_key : str
            The AWS access key.
        secret_key : str
            The AWS secret access key.
        """

        self.CsvFileName = file_name
        self.__access_key = access_key
        self.__secret_key = secret_key

    def create_connection_bucket(self) -> None:
        """
        Creates a connection to an S3 bucket using the given access and secret keys.
        """
        log.info('Creating connection to S3 bucket...')
        conn = boto.connect_s3(
            aws_access_key_id=self.__access_key,
            aws_secret_access_key=self.__secret_key)

        buck1 = conn.create_bucket('my_data')
        log.info('Connection created successfully!')

    def upload_file_to_s3bucket(self) -> None:
        """
        Uploads the CSV file to the S3 bucket.
        """
        log.info('Uploading file to S3 bucket...')
        my_file = pd.read_csv(self.CsvFileName)

        s3 = boto3.client('s3',
                          aws_access_key_id=self.__access_key,
                          aws_secret_access_key=self.__secret_key)
        csv_buff = StringIO()
        my_file.to_csv(csv_buff, header=True, index=False)
        csv_buff.seek(0)
        s3.put_object(Bucket='Fininstrm_data',
                      Body=csv_buff.getvalue(), key='fininstrm_data')
        log.info('File uploaded successfully!')

    def run(self) -> None:
        """
        Runs the complete process of creating the connection to the S3 bucket and uploading the CSV file to the S3 bucket.
        """

        log.info(
            'Starting process to create connection to S3 bucket and upload file...')
        self.create_connection_bucket()
        self.upload_file_to_s3bucket()
        log.info('Process completed successfully!')


import pygame
import colors as col
import pickle
from Arena import *
from snake import *
import time
import argparse
from input import *

if __name__ == "__main__":
    # command line argument parser
    ap = argparse.ArgumentParser()
    ap.add_argument('-i', '--input', required=True, help='relative path of the saved pickle file')
    ap.add_argument('-s', '--start', type=int, help='relative start of the saved snakes')
    args = vars(ap.parse_args())
    # loading the saved snakes
    file = open(args['input'], 'rb')
    snakes = pickle.load(file)
    generation = 0
    if args['start'] is not None:
        start = args['start']
        snakes = snakes[start:]
        generation += start
    file.close()
    # pygame initialization
    pygame.init()
    pygame.font.init()
    myfont = pygame.font.SysFont('Bitstream Vera Serif', 20)
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption('Generation : 0\t\tScore : 0')
    # seed generated so that each snake sees same set of foods for performance comparison
    seed = random.random()
    arena = Arena(width, height, block_length)

    for saved_snake in snakes:
        t_snake = snake(width, height, brainLayer, block_length,
                        random_weights=False, random_bases=False)
        t_snake.Brain.weights = saved_snake.Brain.weights
        t_snake.Brain.bases = saved_snake.Brain.bases
        random.seed(seed)
        t_snake.Brain.setNextFood(arena.newFood(t_snake.list))
        screen = arena.setup(screen, col.bg, col.gray)
        screen = arena.drawFood(screen, col.pink)
        screen = t_snake.draw(screen, col.blue)
        pygame.display.update()
        checkloop = False
        while t_snake.isAlive():
            # checking for key presses and close button presses and pause-continue funcionality
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
                    pressed = True
                    while pressed:
                        for event in pygame.event.get():
                            if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
                                pressed = False
                if event.type == pygame.KEYDOWN and event.key == pygame.K_q:
                    t_snake.crash_wall = True
                    t_snake.crash_body = True
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
            # getting result from the neural network
            result = t_snake.Brain.decision_from_nn(
                t_snake.head_x, t_snake.head_y, t_snake.list, t_snake.direction)
            # moving the snake
            alive = t_snake.move(result)
            # checking for loops made by snake
            if t_snake.steps_taken > (len(t_snake.list)/5*100):
                if not checkloop:
                    checkloop = True
                    any_point = (t_snake.head_x, t_snake.head_y)
                    times = 0
                if (t_snake.head_x, t_snake.head_y) == any_point:
                    times += 1
                if times > 4:
                    t_snake.crash_wall = True
                    t_snake.crash_body = True
                    alive = False
            else:
                checkloop = False
            if not alive:
                text = 'Generation : ' + str(generation+1) + '\t\t' + \
                    'Score : ' + str(len(t_snake.list)-1)+'\t[Dead]'
                if t_snake.crash_wall and t_snake.crash_body:
                    print('killed,', 'generation : ', generation+1, 'score : ', len(t_snake.list)-1)
                elif t_snake.crash_wall and not t_snake.crash_body:
                    print('crashed on wall,', 'generation : ',
                          generation+1, 'score : ', len(t_snake.list)-1)
                else:
                    print('crashed on body,', 'generation : ',
                          generation+1, 'score : ', len(t_snake.list)-1)
                pygame.display.set_caption(text)
                time.sleep(2)
                break
            if (t_snake.head_x, t_snake.head_y) == arena.food:
                t_snake.steps_taken = 0
                next_position = t_snake.Brain.decision_from_nn(
                    t_snake.head_x, t_snake.head_y, t_snake.list, t_snake.direction)
                if not t_snake.increaseSize(next_position):
                    t_snake.crash_wall = True
                t_snake.Brain.setNextFood(arena.newFood(t_snake.list))
            screen = arena.setup(screen, col.bg, col.gray)
            screen = arena.drawFood(screen, col.pink)
            screen = t_snake.draw(screen, col.blue)
            pygame.display.update()
            text = 'Generation : ' + str(generation+1) + '\t\t' + \
                'Score : ' + str(len(t_snake.list)-1)+'\t[Press q to kill]'
            pygame.display.set_caption(text)
            pygame.display.update()
            time.sleep(0.03)
        generation += 1
    pygame.quit()
    quit()


import porespy as ps
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as spim
import scipy.io as sio
import pickle
from matplotlib import cm

### Load pore structure and LB simulation data

velocityNormData = np.loadtxt('weibullCubeVelocityNorm_300.dat')
simSize = 300
velocityNormData = np.reshape(velocityNormData,(simSize,simSize,simSize))

data = np.loadtxt("weibull300_cube.txt",delimiter=',')
beadPack = np.reshape(data,(simSize,simSize,simSize))

beadPack = np.transpose(beadPack,(1,2,0))

plt.figure(2)
plt.imshow(velocityNormData[:,:,100], cmap=plt.cm.nipy_spectral)

plt.imshow(beadPack[:,:,100],cmap=plt.cm.nipy_spectral)

plt.imshow((beadPack*velocityNormData)[:,:,100],cmap=plt.cm.nipy_spectral)

### Compute pore network information

dt = spim.distance_transform_edt(input=beadPack)
dt = spim.gaussian_filter(input=dt, sigma=0.4)
peaks = ps.network_extraction.find_peaks(dt=dt, r=4)

peaks = ps.network_extraction.trim_saddle_points(peaks=peaks, dt=dt)
peaks = ps.network_extraction.trim_nearby_peaks(peaks=peaks, dt=dt)

regions = ps.network_extraction.partition_pore_space(im=dt, peaks=peaks)

plt.figure(1)
plt.imshow((regions*beadPack)[:, :, 50], cmap=plt.cm.nipy_spectral)
plt.axis('off')

im = regions*beadPack
im = im.astype(np.int64)
net = ps.network_extraction.extract_pore_network(im=im, dt=dt)

cubeSize = len(beadPack)

velocities = {}

regionMap = regions*beadPack

for a in range(0, cubeSize):
    for b in range(0, cubeSize):
        for c in range(0, cubeSize):

            #print(beadPack[a,b,c])

            key = str(regionMap[a,b,c])
            if key != '0.0':
                if key in velocities:
                    velocities[key].append(velocityNormData[a,b,c])
                else:
                    velocities.setdefault(key,[])
                    velocities[key].append(velocityNormData[a,b,c])

numPores = len(net['pore.label'])
avgVelocity = []
for a in range(0, numPores):
    key = "%0.1f" % net['pore.label'][a]

    avgVelocity.append(sum(velocities[key])/len(velocities[key]))

net['avgVelocity'] = avgVelocity

#props = ps.metrics.regionprops_3D(im)

#plt.figure(5)
#plt.scatter(net['pore.diameter'],net['avgVelocity'])
#plt.show

# Save output data via pickle
outFile = 'weibullCubeNetworkInfo_300.pkl'

output = open(outFile,'wb')
pickle.dump(net, output)
pickle.dump(velocities, output)

#pickle.dump(props, output)

output.close()


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

import scrapy
from parqueInmobiliario.items import InmuebleItem
from urllib.parse import urlparse

class FotocasaSpider(scrapy.Spider):
    name = 'fotocasa'
    def __init__(self, *args, **kwargs):

        super(FotocasaSpider, self).__init__(*args, **kwargs)
        self.start_urls = [
            "https://www.fotocasa.es/es/comprar/viviendas/"+ kwargs.get('ciudad') +"-provincia/todas-las-zonas/l/"
        ]

    def parse(self, response):

        for i in range(1,20):
            link = response.url + str(i)
            yield scrapy.Request(link, self.parse_pagina)

    def parse_pagina(self, response):
        links = response.xpath('//a[@class="re-CardImage-link"]/@href').extract()
        precios = response.xpath('//span[@class="re-Card-price"]/text()').extract()
        #habitaciones = response.xpath('//span[@class="re-Card-wrapperFeatures"]/span/text()')[0].extract()
        #superficies = response.xpath('//span[@class="re-Card-wrapperFeatures"]/span/text()')[1].extract()
        print(links)
        for i in range(0,len(links)):
            item = InmuebleItem()
            item['precio'] = precios[i]
            item ['link'] = 'https://www.fotocasa.es' + links[i]
            request = scrapy.Request(item['link'], callback=self.parse_vivienda, meta={'item': item})
            yield request

    def parse_vivienda(self, response):

        item = InmuebleItem()
        item['ciudad'] = response.xpath( '//a[@class = "re-Breadcrumb-link"]/text()' )[2].get()
        #item['codigoPostal'] = response.xpath( '//p[@class = "fc-DetailDescription"]/text()' ).re_first( r'[0-9]{5}' )
        item['comunidad'] = response.xpath( '//a[@class = "re-Breadcrumb-link"]/text()' )[0].get()
        item['habitaciones'] = response.xpath( '//li[@class = "re-DetailHeader-featuresItem"]/text()' )[0].re_first( r"([0-9]+) hab" )
        item['banos'] = response.xpath( '//li[@class = "re-DetailHeader-featuresItem"]/text()' )[1].re_first( r'([0-9]+) baño' )
        item['superficie'] = response.xpath( '//li[@class = "re-DetailHeader-featuresItem"]/text()' )[2].re_first( r'[0-9]+' )
        item['precio'] = response.xpath( '//span[@class = "re-DetailHeader-price"]/text()' ).re_first( r'[0-9]+\.[0-9]+' )
        item['referencia'] = response.xpath( '//span[@class = "re-DetailReference"]/text()' ).re_first( r'[0-9]+' )
        item['particular'] = 'Profesional' if response.xpath( '//div[@class = "re-ContactDetail-inmo"]' ) else 'Particular'
        item['web'] = 'Fotocasa'
        item['link'] = response.url

        yield item


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Libvirt exporter
================

Libvirt node export for OpenStack deployments.

Exporter relies on specific OS metadata.

Dependency on packages:
* python3-libvirt

======= =========
Stats   Reference
======= =========
CPU:    :py:func:`node_exporters.libvirt.libvirt_only.get_cpu_stats`
Traffic :py:func:`node_exporters.libvirt.libvirt_only.get_net_stats`
Disk    :py:func:`node_exporters.libvirt.libvirt_only.get_disk_io_stats`
Memory  :py:func:`node_exporters.libvirt.libvirt_only.get_mem_stats`
======= =========

.. list-table:: Time Units
   :widths: 25 75
   :header-rows: 1

   * - Code
     - Unit
   * - time
     - seconds
   * - mtime
     - milliseconds
   * - utime
     - microseconds (export)
   * - ntime
     - nanoseconds (cgroup stats)

"""
import argparse
import sys

from prometheus_client import start_http_server
from prometheus_client.core import REGISTRY
from prometheus_client.core import GaugeMetricFamily

try:
    from libvirtmetadata import LibvirtMetadata
except Exception:
    libvirtmetadata = None
try:
    from scheduler import Scheduler
except Exception:
    scheduler = None

class CustomCollector(object):
    def __init__(self, helper, helper_name='unknown', libv_meta=None):
        self.ALL_STATS = []
        self.HELPER = helper
        self.HELPER_NAME = helper_name
        self.libv_meta = libv_meta

    def collect(self):
        items = {}

        for stat in self.ALL_STATS:
            if stat[0] not in items:
                items[stat[0]] = []
            items[stat[0]].append(stat[1:])

        for key, values in items.items():
            labels = values[0][0]
            g = GaugeMetricFamily(key, self.HELPER, labels=labels)
            for labels, metadata, value in values:
                g.add_metric(metadata, value)
            yield g

        try:
            if self.libv_meta:
                g = GaugeMetricFamily(
                    'libvirt_connection_status',
                    'Libvirt status none (-1), connected (0), or error (1)',
                    labels=['node_exporter']
                )
                g.add_metric([self.HELPER_NAME], self.libv_meta.status)
                yield g
        except Exception:
            pass

def get_cpu_stats(stats):
    """
    Get cpu stats.

    .. list-table:: CPU stats
       :widths: 25 75
       :header-rows: 1

       * - Measure
         - Description
       * - User time (cpu_user_utime)
         - Amount of time the processor worked on the specific program
       * - System time (cpu_system_utime)
         - Amount of time the processor worked on OS functions connected to that specific program
       * - Total cpu time (cpu_total_utime)
         - Provided by OS (surprisingly it is not just a sum of user and system)
       * - VCPU time (vcpu_utime)
         - CPU time measured for each vcpu separately as one metric only, but can
           provide info about balancing inside (whether they actually use all their VCPUs)
       * - VCPU max count (vcpu_max_count)
         - How many VCPUs can be used by VM (quota)
       * - VCPU use count (vcpu_use_count)
         - How many VCPUs are currently used by VM (allocated)

    Limitations:
      * Domain total cpu data require cgroup CPUACCT controller to be mounted,
        in case of issues with measurement this has to be checked.

    :param dict stats: statistics data retrieved from libvirt.

    :return dict: stats
    """
    recount_cpu_time = False
    items = {
        'cpu_total_utime': int(stats.get('cpu.time', 0) / 1000),  # CPUACCT
        'cpu_user_utime': int(stats.get('cpu.user', 0) / 1000),  # CPUACCT
        'cpu_system_utime': int(stats.get('cpu.system', 0) / 1000),  # CPUACCT
        'cpu_max_count': stats.get('vcpu.maximum', 0),
        'cpu_use_count': 0,
        'variable': {},
    }

    if items['cpu_total_utime'] == 0:
        # If cgroup fails - missing measures in total
        recount_cpu_time = True

    for i in range(items.get('cpu_max_count')):
        if recount_cpu_time:
            items['cpu_total_utime'] += int(
                stats.get('vcpu.{}.time'.format(i), 0) / 1000)
        vcpu_state = stats.get('vcpu.{}.state'.format(i), 0)
        items['cpu_use_count'] += 1 if vcpu_state == 1 else 0
        items['variable']['vcpu:{}'.format(i)] = {
            'vcpu_utime': int(stats.get('vcpu.{}.time'.format(i), 0) / 1000),
        }
    return items

def get_net_stats(stats):
    """
    Get network traffic stats.

    Covers all interfaces - it is a sum of internal and external traffic - not just ingress/egress

    .. list-table:: Traffic stats
       :widths: 25 75
       :header-rows: 1

       * - Measure
         - Description
       * - TX count (net_tx_count)
         - Amount of processed transmissions
       * - RX count (net_rx_count)
         - Amount of processed receives
       * - TX bytes (net_tx_bytes)
         - Total transmitted volume in bytes
       * - RX bytes (net_rx_bytes)
         - Total received volume in bytes

    To measure ingress/egress we don't use data from compute nodes.

    TODO:
      * Could also get drop and error counts if wanted.

    :param dict stats: statistics data retrieved from libvirt.

    :return dict: stats
    """
    items = {
        'net_tx_count': 0,
        'net_rx_count': 0,
        'net_tx_bytes': 0,
        'net_rx_bytes': 0,
        'net_use_count': stats.get('net.count', 0),
    }

    for i in range(items.get('net_use_count')):
        items['net_tx_count'] += stats.get('net.{}.tx.pkts'.format(i), 0)
        items['net_rx_count'] += stats.get('net.{}.rx.pkts'.format(i), 0)
        items['net_tx_bytes'] += stats.get('net.{}.tx.bytes'.format(i), 0)
        items['net_rx_bytes'] += stats.get('net.{}.rx.bytes'.format(i), 0)
    return items

def get_disk_io_stats(stats):
    """
    Get disk IO stats.

    IO stats are available both as total and per device.

    .. list-table:: IO stats
       :widths: 25 75
       :header-rows: 1

       * - Measure
         - Description
       * - Read count (disk_read_count)
         - The number of read input/output operations generated by a process,
           including file, network, and device I/Os. I/O Reads directed to CONSOLE
           (console input object) handles are not counted.
       * - Write count (disk_write_count)
         - The number of write input/output operations generated by a process,
           including file, network, and device I/Os. I/O Writes directed to
           CONSOLE (console input object) handles are not counted.
       * - Read bytes (disk_read_bytes)
         - The number of bytes read in input/output operations generated by
           a process, including file, network, and device I/Os. I/O Read Bytes
           directed to CONSOLE (console input object) handles are not counted.
       * - Write bytes (disk_write_bytes)
         - The number of bytes written in input/output operations generated by
           a process, including file, network, and device I/Os. I/O Write Bytes
           directed to CONSOLE (console input object) handles are not counted.
       * - Disk use count (disk_use_count)
         - Amount of used devices.

    Limitations:
      * CPU read/write (IO) provide different information than storage (e.g. ceph) stats would show,
        because for cpu IO operations are not just disk operations (includes file, network, and device I/Os).
      * https://superuser.com/questions/993966/what-does-i-o-reads-or-writes-and-i-o-read-bytes-or-write-bytes-mean

    TODO:
      * Could also get flush reqs count and times

    :param dict stats: statistics data retrieved from libvirt.

    :return dict: stats
    """
    items = {
        'disk_write_bytes': 0,
        'disk_read_bytes': 0,
        'disk_write_count': 0,
        'disk_read_count': 0,
        'disk_use_count': stats.get('block.count', 0),
        'variable': {},
    }

    for i in range(items.get('disk_use_count', 0)):
        items['disk_write_bytes'] += stats.get(
            'block.{}.wr.bytes'.format(i), 0)
        items['disk_read_bytes'] += stats.get('block.{}.rd.bytes'.format(i), 0)
        items['disk_write_count'] += stats.get(
            'block.{}.wr.times'.format(i), 0)
        items['disk_read_count'] += stats.get('block.{}.rd.times'.format(i), 0)
        disk_name = stats.get('block.{}.name'.format(i), None)
        if disk_name:
            items['variable']['device:{}'.format(disk_name)] = {
                'dev_total_bytes': stats.get('block.{}.physical'.format(i), 0),
                'dev_write_bytes': stats.get('block.{}.wr.bytes'.format(i), 0),
                'dev_read_bytes': stats.get('block.{}.rd.bytes'.format(i), 0),
                'dev_write_count': stats.get('block.{}.wr.times'.format(i), 0),
                'dev_read_count': stats.get('block.{}.rd.times'.format(i), 0),
            }
    return items

def get_mem_stats(domain, stats):
    """
    Get memory stats.

    .. list-table:: Memory stats
       :widths: 25 75
       :header-rows: 1

       * - Measure
         - Description
       * - Total (mem_max_bytes)
         - Amount of memory specified on domain creation of VM
       * - Used (mem_use_bytes)
         - Returns values close to total, because VM holds all the memory needed as
           appears from the outside and does not reflect internal state of OS here.
       * - Free (mem_free_bytes)
         - Amount of memory left unused by the system (opposite)
       * - Rss (mem_rss_bytes)
         - Resident Set Size of the process running the domain

    Limitations:
      * both used and free reflect the process, but not what is seen inside VM, so it may
        not be useful for user or billing. Will have to review with higher versions of
        Openstack when we move on.

    :param domain: libvirt domain object for extra stats.
    :param dict stats: statistics data retrieved from libvirt.

    :return dict: stats
    """
    items = {}
    try:
        mem = domain.memoryStats()
        items = {
            'mem_use_bytes': 1000 * mem.get('available', 0) if mem.get('available', 0) >= 0 else 0,
            'mem_rss_bytes': 1000 * mem.get('rss', 0) if mem.get('rss', 0) >= 0 else 0,
            'mem_free_bytes': 1000 * mem.get('unused', 0) if mem.get('unused', 0) >= 0 else 0,
            'mem_max_bytes': 1000 * mem.get('actual', 0) if mem.get('actual', 0) >= 0 else 0,
        }
    except Exception:
        items = {
            'mem_use_bytes': 1000 * stats.get('balloon.current', 0),
            'mem_max_bytes': 1000 * stats.get('balloon.maximum', 0),
        }
    return items

def prom_stats(libv_meta, cc):
    """Gather and export prometheus stats."""
    all_stats = []

    try:
        with libv_meta.libvirt_connection() as conn:
            # all_domain_stats = conn.getAllDomainStats(stats=libv_meta.STATS, flags=libv_meta.FLAGS)
            # for domain, stats in all_domain_stats:
            for dom in conn.listAllDomains(flags=libv_meta.LIST_DOMAINS_RUNNING):
                stat_list = []
                try:
                    state = int(dom.state()[0])
                except Exception:
                    state = 0
                try:
                    control_state = int(dom.controlInfo()[0])
                except Exception:
                    control_state = 4
                try:
                    control_time = int(dom.controlInfo()[-1] / 1000)
                except Exception:
                    control_time = -1
                try:
                    instance = dom.name()
                    metadata = libv_meta.get_instance_metadata(instance, dom)
                    try:
                        all_stats.extend(libv_meta.export({
                            'vm_control_time': control_time,
                            'vm_control_state': control_state,
                            'vm_state': state
                        }, instance, metadata=metadata))
                    except Exception:
                        pass
                    if state == libv_meta.DOMAIN_RUNNING and control_time < 300 and control_time >= 0:
                        # Ignore if domain not running or busy/locked for more than 5 min
                        stat_list = conn.domainListGetStats(
                            [dom], stats=libv_meta.STATS, flags=libv_meta.FLAGS)
                except Exception:
                    stat_list = []
                for domain, stats in stat_list:
                    try:
                        all_stats.extend(libv_meta.export(
                            get_cpu_stats(stats), instance, metadata=metadata))
                        all_stats.extend(libv_meta.export(
                            get_net_stats(stats), instance, metadata=metadata))
                        all_stats.extend(libv_meta.export(
                            get_disk_io_stats(stats), instance, metadata=metadata))
                        all_stats.extend(libv_meta.export(get_mem_stats(
                            domain, stats), instance, metadata=metadata))
                        all_stats.extend(libv_meta.export(libv_meta.get_cpu_meta(
                            domain), instance, metadata=metadata))
                    except Exception:
                        pass
                    try:
                        all_stats.extend(libv_meta.export(libv_meta.get_gpu_meta(
                            domain), instance, metadata=metadata, prefix='libv_'))
                    except Exception:
                        pass
            try:
                all_stats.extend(libv_meta.export(libv_meta.get_gpu_device_meta(), None, prefix='libv_'))
            except Exception as e:
                print(e)
    except Exception:
        libv_meta.status = 1  # error

    cc.ALL_STATS = all_stats

def shell(args):
    """Start iPython shell for direct management access."""
    from IPython import embed
    from datetime import datetime as dt  # noqa
    from datetime import timedelta as td  # noqa
    from datetime import time  # noqa
    import logging
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
    libv_meta = LibvirtMetadata()  # noqa
    embed(header="Welcome in iPython shell")

def main(args):
    scheduler = Scheduler()
    libv_meta = LibvirtMetadata()
    try:
        libv_meta.load_libvirt_metadata()
    except Exception:
        pass

    cc = CustomCollector('Libvirt instance stats',
                         helper_name='libvirt', libv_meta=libv_meta)
    REGISTRY.register(cc)
    start_http_server(args.port, addr=args.addr)
    scheduler.log(
        'Exposing metrics at: http://{}:{}/metrics'.format(args.addr, args.port))

    # Every 'wait_time' seconds
    scheduler.add_periodic_task(
        prom_stats, 'second', round=args.wait_time, args=(libv_meta, cc)
    )
    # Every 20 minutes
    scheduler.add_periodic_task(
        libv_meta.load_libvirt_metadata, 'minute', round=20)

    scheduler.run_concurrent(debug=args.debug)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Libvirt textfile exporter')
    parser.set_defaults(func=main)
    subparsers = parser.add_subparsers(
        dest='action', help='Choose an alternative action (optional)')

    parser.add_argument(
        '-p', '--port', dest='port', default=9121, type=int,
        help='Port to expose metrics'
    )
    parser.add_argument(
        '-i', '--ip', dest='addr', default='0.0.0.0',
        help='IP address to expose metrics'
    )
    parser.add_argument(
        '-t', '--wait-time', dest='wait_time', default=2, type=int,
        help='Time to sleep between measures [2-30]'
    )
    parser.add_argument('--debug', dest='debug',
                        action='store_true', help='Debug messages')
    subparsers.add_parser(
        'shell', help='Run iPython shell').set_defaults(func=shell)

    args = parser.parse_args()
    args.func(args)


import sys
from flask import make_response, request
from sqlalchemy import select, String
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.sql import cast
from functools import wraps
from database.schema import Account, Loginsession

def get_accounts(session: Session):
    account_query = select(
        Account.user_id,
        Account.username,
        cast(Account.pass_hash, String).label("pass_hash"),
    )
    accounts = session.execute(account_query).all()

    return {"data": [dict(account) for account in accounts]}

def get_tokens(session: Session):
    session_query = select(
        cast(Loginsession.token, String).label("token"), Loginsession.user_id
    )
    login_sessions = session.execute(session_query).all()

    return {"data": [dict(login_session) for login_session in login_sessions]}

def get_user(session: Session) -> Account:
    token = request.cookies.get("token")
    if token == None:
        return None
    session_query = (
        select(Account)
        .join(Loginsession)
        .where(Loginsession.token == token.encode("utf-8"))
    )
    print(session_query, file=sys.stderr)
    this_session = session.execute(session_query).one_or_none()

    return this_session.Account

def check_loggedin_token(token, session: Session):
    if token is None:
        return False

    session_query = select(Loginsession).where(
        Loginsession.token == token.encode("utf-8")
    )
    this_session = session.execute(session_query).one_or_none()

    # return dict(this_session) if this_session is not None else None
    return this_session is not None

def login_required(MakeSession: sessionmaker):
    def _wrapped_decorator(func):
        @wraps(func)
        def _wrapper(*args, **kwargs):

            cookie_token = request.cookies.get("token")
            cookie_username = request.cookies.get("username")

            with MakeSession() as session:
                is_logged_in = check_loggedin_token(cookie_token, session)

            # or cookie_username != user_info.get('username'):
            if not is_logged_in:
                resp = make_response({"message": "Incorrect Auth Token"})
                resp.delete_cookie("token")
                resp.status_code = 401
                return resp
            return func(*args, **kwargs)

        return _wrapper

    return _wrapped_decorator


import os
import tkinter as tk
import tkinter.messagebox
from DN_tool.tagger import FlaskServer
import pandas as pd
import numpy as np
import urllib
import json
from datetime import datetime

class check_nodule():
    def __init__(self):
        self.WIDTH = 40
        self.columns = ['seriesuid', 'coordZ', 'coordY', 'coordX', 'diameter_mm', 'probability', 'is_nodule']
        self.check_nodule_win = tk.Toplevel()
        self.check_nodule_win.geometry("800x800")
        self.check_nodule_win.title("Draw controus")
        npz_dir = tk.Label(self.check_nodule_win, font=('Times', 15, 'bold'), fg='blue',
                             text="NPZ Dir:")
        npz_dir.place(x=20, y=20, anchor='nw')
        csv_file = tk.Label(self.check_nodule_win, font=('Times', 15, 'bold'), fg='blue',
                              text="CSV File `seriesuid,coordZ,coordY,coordX, diameter_mm,probability,is_nodule`:")
        csv_file.place(x=20, y=60, anchor='nw')
        out_label = tk.Label(self.check_nodule_win, font=('Times', 15, 'bold'), fg='blue', text="new csvfile outpath:")
        out_label.place(x=20, y=100, anchor='nw')

        port = tk.Label(self.check_nodule_win, font=('Times', 15, 'bold'), fg='blue', text="server port:")
        port.place(x=20, y=140, anchor='nw')

        self.npzstr = tk.StringVar()
        self.csvstr = tk.StringVar()
        self.outpath = tk.StringVar()
        self.port = tk.IntVar()
        tk_path = tk.Entry(self.check_nodule_win, textvariable=self.npzstr, width=80)
        tk_path.place(x=20, y=40, anchor='nw')
        tk_path = tk.Entry(self.check_nodule_win, textvariable=self.csvstr, width=80)
        tk_path.place(x=20, y=80, anchor='nw')
        tk_path = tk.Entry(self.check_nodule_win, textvariable=self.outpath, width=80)
        tk_path.place(x=20, y=120, anchor='nw')
        tk_path = tk.Entry(self.check_nodule_win, textvariable=self.port, width=10)
        tk_path.place(x=180, y=145, anchor='nw')

        tk_btm0 = tk.Button(self.check_nodule_win, text="疑似结节人工确认", bg='green', fg='red',
                           font=('Helvetica', 15), command=self.check)
        tk_btm0.place(x=200, y=200, anchor='center')

        tk_btm1 = tk.Button(self.check_nodule_win, text="立刻保存结果", bg='green', fg='red',
                           font=('Helvetica', 15), command=self.save)
        tk_btm1.place(x=500, y=200, anchor='center')

        tk_btm2 = tk.Button(self.check_nodule_win, text="stop", bg='green', fg='red',
                            font=('Helvetica', 15), command=self.stop)
        tk_btm2.place(x=700, y=200, anchor='center')
        # create server
        self.server = FlaskServer()

    def check(self):
        if not os.path.exists(self.npzstr.get()):
            tkinter.messagebox.showerror(title='Error', message='Exception happen, Pls check npz path.')
            return 0
        if not os.path.exists(self.csvstr.get()):
            tkinter.messagebox.showerror(title='Error', message='Exception happen, Pls check csv path.')
            return 0
        if not os.path.exists(self.outpath.get()):
            tkinter.messagebox.showerror(title='Error', message='Exception happen, Pls check out path.')
            return 0
        if (int(self.port.get()) < 5005) | (int(self.port.get()) > 6000):
            tkinter.messagebox.showerror(title='Error', message='suggest port  5005 < id < 6000')
            return 0
        df = pd.read_csv(self.csvstr.get())
        if len(set(df.columns) & set(self.columns)) != len(self.columns):
            tkinter.messagebox.showerror(title='Error', message='Exception happen, Pls check csvfile columns.')
            return 0
        check_names = sorted(df['seriesuid'].unique())
        for i in check_names:
            uid = os.path.basename(i)
            try:
                data, roi = self.uid_to_info(uid, df)
            except:
                continue
            self.server.serve(field=uid, data=data, roi=roi)
        self.server.run(port=int(self.port.get()))
        tkinter.messagebox.showinfo(title='Info', message="please run `http://localhost:{port}`")

    def uid_to_info(self, uid, df):
        ret = []
        path = os.path.join(self.npzstr.get(), uid + ".npz")
        print(path)
        data = np.load(path)['raw']
        data = np.clip(data, -1000, 400)
        data = (((data - data.min()) / (data.max() - data.min())) * 255).astype(np.uint8)
        cand_df = df[df['seriesuid'] == uid]
        for _, row in cand_df.iterrows():
            x, y, z, is_nodule, p = row.loc[['coordX', 'coordY', 'coordZ', 'is_nodule', 'probability']]
            # x,y,z, = row.loc[['coordX','coordY','coordZ']]
            # is_nodule = "yes"
            # p = 0.1
            if np.isnan(is_nodule):
                tag = "pass"
            elif (is_nodule == 0) | (is_nodule == "no"):
                tag = "no"
            elif (is_nodule == 1) | (is_nodule == "yes"):
                tag = "yes"

            ret.append({'p':p,'r':self.WIDTH,'x':x,'y':y,'z':z,'tag':tag})
        return data, ret

    def save(self):
        try:
            port = int(self.port.get())
            out = self.outpath.get()
            now = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
            with urllib.request.urlopen('http://localhost:{}/rois/'.format(port)) as j:
                lst = json.loads(j.read().decode('utf-8'))['rois']
                df = pd.DataFrame(lst, columns=('seriesuid', 'coordX', 'coordY', 'coordZ', 'probability', 'tag'))
            df.to_csv(os.path.join(out, now + ".csv"),index=None)
            tkinter.messagebox.showinfo(title='Info', message='save success')

        except:
            tkinter.messagebox.showerror(title='Error', message='save error, pls check')

    def stop(self):
        self.server.reset()


path_to_png_file = r"/home/varderes/Desktop/GitHub/SorinLab/Clustering/cityofangels"

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from kMeansClustering import KMeans

# construct new image
def recolor(pixel):
    cluster = clusterer.classify(pixel)     # index of the closest cluster
    return clusterer.means[cluster]         # mean of the closest cluster

img = mpimg.imread(path_to_png_file)

top_row = img[0]
top_left_pixel = top_row[0]
red, green, blue = top_left_pixel

# get a flattened list of all the pixels as:
pixels = [pixel for row in img for pixel in row]

# feed to clusterer
clusterer = KMeans(5, 20)
clusterer.train(pixels)

new_img = [[recolor(pixel) for pixel in row] for row in img]

plt.imshow(new_img)
plt.axis('off')
plt.show()


import pyinotify
import time

wm = pyinotify.WatchManager()  # Watch Manager
mask = pyinotify.IN_CLOSE_WRITE  # watched events

class FileEventHandler(pyinotify.ProcessEvent):
     def process_IN_CLOSE_WRITE(self, event):
        print("Changed" + event.pathname)

#******************************************************
#Thread listening to the CAN-bus and broadcast the messages to the other threads
#******************************************************

notifier = pyinotify.ThreadedNotifier(wm, FileEventHandler())
notifier.start()
wdd = wm.add_watch('/home/pi/datalogger/loggerconfigs/', mask, rec=False)
time.sleep(10)
notifier.stop()


import math
from collections import deque

text = open("input.txt").read()

blizards = set()
lines = text.split("\n")
for y, line in enumerate(lines):
    for x, c in enumerate(line):
        if c in '><^v':
            blizards.add((x,y,c))
W, H = len(lines[0]), len(lines)
PERIOD = math.lcm(W-2, H-2)

DIRS = {'<': (-1, 0), '>': (1,0), '^': (0, -1), 'v': (0, 1), 'x': (0,0)}
BLIZARDS = {}

def simulate(blizards):
    new = set()
    for x, y, dir in blizards:
        dx, dy = DIRS[dir]
        x2 = (x+dx-1)%(W-2)+1
        y2 = (y+dy-1)%(H-2)+1
        new.add((x2,y2,dir))
    return new

def BFS(blizards, target):
    queue = deque([(1, 0, 0, 0)])
    bests = {}
    bests[queue[-1]] = 0
    last_time = -1
    ile = 0
    while queue:
        ile += 1
        item = queue.popleft()
        x, y, time, count = item

        if time != last_time:
            last_time = time
            if time not in BLIZARDS:
                b =simulate(blizards)
                BLIZARDS[time] = (b, {(x,y) for x,y,_ in b})
            blizards, cells = BLIZARDS[time]

        if (x,y,count) == target:
            return bests[item]

        new_count = count
        if (count%2 == 0) and (x, y)== (W-2, H-1):
            new_count += 1
        elif (count % 2 == 1) and (x,y) == (1,0):
            new_count += 1

        for dx, dy in DIRS.values():
            p = x2, y2 = x+dx, y+dy

            if p==(1,0) or p == (W-2, H-1) or ((1 <= x2 < W-1) and (1 <= y2 < H-1)):
                item2 = (x2, y2, (time+1)%PERIOD, new_count)

                if (p not in cells) and (item2 not in bests):
                    queue.append(item2)
                    bests[item2] = bests[item]+1

print("a:", BFS(blizards, (W-2,H-1,0)))
print("b:", BFS(blizards, (W-2,H-1,2)))