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

# file lyxpreview2bitmap.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.

# author Angus Leeming
# with much advice from members of the preview-latex project:
# David Kastrup, dak@gnu.org and
# Jan-Åke Larsson, jalar@mai.liu.se.

# Full author contact details are available in file CREDITS

# This script takes a LaTeX file and generates a collection of
# png or ppm image files, one per previewed snippet.

# Pre-requisites:
# * python 2.4 or later (subprocess module);
# * A latex executable;
# * preview.sty;
# * dvipng;
# * dv2dt;
# * pngtoppm (if outputing ppm format images).

# preview.sty and dvipng are part of the preview-latex project
# http://preview-latex.sourceforge.net/

# preview.sty can alternatively be obtained from
# CTAN/support/preview-latex/

# Example usage:
# lyxpreview2bitmap.py --bg=faf0e6 0lyxpreview.tex

# This script takes one obligatory argument:
#
#   <input file>:  The name of the .tex file to be converted.
#
# and these optional arguments:
#
#   --png, --ppm:  The desired output format. Either 'png' or 'ppm'.
#   --dpi=<res>:   A scale factor, used to ascertain the resolution of the
#                  generated image which is then passed to gs.
#   --fg=<color>:  The foreground color as a hexadecimal string, eg '000000'.
#   --bg=<color>:  The background color as a hexadecimal string, eg 'faf0e6'.
#   --latex=<exe>: The converter for latex files. Default is latex.
#   --bibtex=<exe>: The converter for bibtex files. Default is bibtex.
#   --lilypond:    Preprocess through lilypond-book. Default is false.
#   --lilypond-book=<exe>:
#                  The converter for lytex files. Default is lilypond-book.
#
#   -d, --debug    Show the output from external commands.
#   -h, --help     Show an help screen and exit.
#   -v, --verbose  Show progress messages.

# Decomposing TEXFILE's name as DIR/BASE.tex, this script will,
# if executed successfully, leave in DIR:
# * a (possibly large) number of image files with names
#   like BASE[0-9]+.png
# * a file BASE.metrics, containing info needed by LyX to position
#   the images correctly on the screen.

# What does this script do?
# 1) Call latex/pdflatex/xelatex/whatever (CONVERTER parameter)
# 2) If the output is a PDF fallback to legacy
# 3) Otherwise check each page of the DVI (with dv2dt) looking for
#    PostScript literals, not well supported by dvipng. Pages
#    containing them are passed to the legacy method in a new LaTeX file.
# 4) Call dvipng on the pages without PS literals
# 5) Join metrics info coming from both methods (legacy and dvipng)
#    and write them to file

# dvipng is fast but gives problem in several cases, like with
# PSTricks, TikZ and other packages using PostScript literals
# for all these cases the legacy route is taken (step 3).
# Moreover dvipng can't work with PDF files, so, if the CONVERTER
# paramter is pdflatex we have to fallback to legacy route (step 2).

from __future__ import print_function

import getopt, glob, os, re, shutil, sys, tempfile

import lyxpreview_tools

from legacy_lyxpreview2ppm import extract_resolution, legacy_conversion_step1

from lyxpreview_tools import bibtex_commands, check_latex_log, copyfileobj, \
     error, filter_pages, find_exe, find_exe_or_terminate, \
     join_metrics_and_rename, latex_commands, latex_file_re, make_texcolor, \
     pdflatex_commands, progress, run_command, run_latex, run_tex, \
     warning, write_metrics_info

PY2 = sys.version_info[0] == 2

def usage(prog_name):
    msg = """
Usage: %s <options> <input file>

Options:
  --dpi=<res>:   Resolution per inch (default: 128)
  --png, --ppm:  Select the output format (default: png)
  --fg=<color>:  Foreground color (default: black, ie '000000')
  --bg=<color>:  Background color (default: white, ie 'ffffff')
  --latex=<exe>: Specify the executable for latex (default: latex)
  --bibtex=<exe>: Specify the executable for bibtex (default: bibtex)
  --lilypond:    Preprocess through lilypond-book (default: false)
  --lilypond-book=<exe>:
                 The executable for lilypond-book (default: lilypond-book)

  -d, --debug:   Show the output from external commands
  -h, --help:    Show this help screen and exit
  -v, --verbose: Show progress messages

The colors are hexadecimal strings, eg 'faf0e6'."""
    return msg % prog_name

# Returns a list of tuples containing page number and ascent fraction
# extracted from dvipng output.
# Use write_metrics_info to create the .metrics file with this info
def extract_metrics_info(dvipng_stdout):
    # "\[[0-9]+" can match two kinds of numbers: page numbers from dvipng
    # and glyph numbers from mktexpk. The glyph numbers always match
    # "\[[0-9]+\]" while the page number never is followed by "\]". Thus:
    page_re = re.compile(r"\[([0-9]+)[^]]");
    metrics_re = re.compile("depth=(-?[0-9]+) height=(-?[0-9]+)")

    success = 0
    page = ""
    pos = 0
    results = []
    while 1:
        match = page_re.search(dvipng_stdout, pos)
        if match == None:
            break
        page = match.group(1)
        pos = match.end()
        match = metrics_re.search(dvipng_stdout, pos)
        if match == None:
            break
        success = 1

        # Calculate the 'ascent fraction'.
        descent = float(match.group(1))
        ascent  = float(match.group(2))

        frac = 0.5
        if ascent < 0:
            # This is an empty image, forbid its display
            frac = -1.0
        elif ascent >= 0 or descent >= 0:
            if abs(ascent + descent) > 0.1:
                frac = ascent / (ascent + descent)

            # Sanity check
            if frac < 0:
                frac = 0.5

        results.append((int(page), frac))
        pos = match.end() + 2

    if success == 0:
        error("Failed to extract metrics info from dvipng")

    return results

def fix_latex_file(latex_file, pdf_output):
    # python 2 does not allow to declare a string as raw byte so we double
    # the backslashes and remove the r preffix
    def_re = re.compile(b"(\\\\newcommandx|\\\\global\\\\long\\\\def)"
                        b"(\\\\[a-zA-Z]+)")

    tmp = tempfile.TemporaryFile()

    changed = False
    macros = []
    for line in open(latex_file, 'rb').readlines():
        if not pdf_output and line.startswith(b"\\documentclass"):
            changed = True
            line += b"\\PassOptionsToPackage{draft}{microtype}\n"
        else:
            match = def_re.match(line)
            if match != None:
                macroname = match.group(2)
                if macroname in macros:
                    definecmd = match.group(1)
                    if definecmd == b"\\newcommandx":
                        changed = True
                        line = line.replace(definecmd, b"\\renewcommandx")
                else:
                    macros.append(macroname)
        tmp.write(line)

    if changed:
        copyfileobj(tmp, open(latex_file,"wb"), 1)

    return changed

def convert_to_ppm_format(pngtopnm, basename):
    png_file_re = re.compile(r"\.png$")

    for png_file in glob.glob("%s*.png" % basename):
        ppm_file = png_file_re.sub(".ppm", png_file)

        p2p_cmd = '%s "%s"' % (pngtopnm, png_file)
        p2p_status, p2p_stdout = run_command(p2p_cmd)
        if p2p_status:
            error("Unable to convert %s to ppm format" % png_file)

        ppm = open(ppm_file, 'w')
        ppm.write(p2p_stdout)
        os.remove(png_file)

# Returns a tuple of:
# ps_pages: list of page indexes of pages containing PS literals
# pdf_pages: list of page indexes of pages requiring running pdflatex
# page_count: total number of pages
# pages_parameter: parameter for dvipng to exclude pages with PostScript/PDF
def find_ps_pages(dvi_file):
    # latex failed
    # FIXME: try with pdflatex
    if not os.path.isfile(dvi_file):
        error("No DVI output.")

    # Check for PostScript specials in the dvi, badly supported by dvipng,
    # and inclusion of PDF/PNG/JPG files.
    # This is required for correct rendering of PSTricks and TikZ
    dv2dt = find_exe_or_terminate(["dv2dt"])
    dv2dt_call = '%s "%s"' % (dv2dt, dvi_file)

    # The output from dv2dt goes to stdout
    dv2dt_status, dv2dt_output = run_command(dv2dt_call)
    psliteral_re = re.compile("^special[1-4] [0-9]+ '(\"|ps:)")
    hyperref_re = re.compile("^special[1-4] [0-9]+ 'ps:.*/DEST pdfmark")
    pdffile_re = re.compile("^special[1-4] [0-9]+ 'PSfile=.*\\.(pdf|png|jpg|jpeg|PDF|PNG|JPG|JPEG)")

    # Parse the dtl file looking for PostScript specials and pdflatex files.
    # Pages using PostScript specials or pdflatex files are recorded in
    # ps_pages or pdf_pages, respectively, and then used to create a
    # different LaTeX file for processing in legacy mode.
    # If hyperref is detected, the corresponding page is recorded in pdf_pages.
    page_has_ps = False
    page_has_pdf = False
    page_index = 0
    ps_pages = []
    pdf_pages = []
    ps_or_pdf_pages = []

    for line in dv2dt_output.split("\n"):
        # New page
        if line.startswith("bop"):
            page_has_ps = False
            page_has_pdf = False
            page_index += 1

        # End of page
        if line.startswith("eop") and (page_has_ps or page_has_pdf):
            # We save in a list all the PostScript/PDF pages
            if page_has_ps:
                ps_pages.append(page_index)
            else:
                pdf_pages.append(page_index)
            ps_or_pdf_pages.append(page_index)

        if psliteral_re.match(line) != None:
            # Literal PostScript special detected!
            # If hyperref is detected, put this page on the pdf pages list
            if hyperref_re.match(line) != None:
                page_has_ps = False
                page_has_pdf = True
            else:
                page_has_ps = True
        elif pdffile_re.match(line) != None:
            # Inclusion of pdflatex image file detected!
            page_has_pdf = True

    # Create the -pp parameter for dvipng
    pages_parameter = ""
    if len(ps_or_pdf_pages) > 0 and len(ps_or_pdf_pages) < page_index:
        # Don't process Postscript/PDF pages with dvipng by selecting the
        # wanted pages through the -pp parameter. E.g., dvipng -pp 4-12,14,64
        pages_parameter = " -pp "
        skip = True
        last = -1

        # Use page ranges, as a list of pages could exceed command line
        # maximum length (especially under Win32)
        for index in range(1, page_index + 1):
            if (not index in ps_or_pdf_pages) and skip:
                # We were skipping pages but current page shouldn't be skipped.
                # Add this page to -pp, it could stay alone or become the
                # start of a range.
                pages_parameter += str(index)
                # Save the starting index to avoid things such as "11-11"
                last = index
                # We're not skipping anymore
                skip = False
            elif (index in ps_or_pdf_pages) and (not skip):
                # We weren't skipping but current page should be skipped
                if last != index - 1:
                    # If the start index of the range is the previous page
                    # then it's not a range
                    pages_parameter += "-" + str(index - 1)

                # Add a separator
                pages_parameter += ","
                # Now we're skipping
                skip = True

        # Remove the trailing separator
        pages_parameter = pages_parameter.rstrip(",")
        # We've to manage the case in which the last page is closing a range
        if (not index in ps_or_pdf_pages) and (not skip) and (last != index):
                pages_parameter += "-" + str(index)

    return (ps_pages, pdf_pages, page_index, pages_parameter)

def main(argv):
    # Set defaults.
    dpi = 128
    fg_color = "000000"
    bg_color = "ffffff"
    bibtex = None
    latex = None
    lilypond = False
    lilypond_book = None
    output_format = "png"
    script_name = argv[0]

    # Parse and manipulate the command line arguments.
    try:
        (opts, args) = getopt.gnu_getopt(argv[1:], "dhv", ["bibtex=", "bg=",
            "debug", "dpi=", "fg=", "help", "latex=", "lilypond",
            "lilypond-book=", "png", "ppm", "verbose"])
    except getopt.GetoptError as err:
        error("%s\n%s" % (err, usage(script_name)))

    opts.reverse()
    for opt, val in opts:
        if opt in ("-h", "--help"):
            print(usage(script_name))
            sys.exit(0)
        elif opt == "--bibtex":
            bibtex = [val]
        elif opt == "--bg":
            bg_color = val
        elif opt in ("-d", "--debug"):
            lyxpreview_tools.debug = True
        elif opt == "--dpi":
            try:
                dpi = int(val)
            except:
                error("Cannot convert %s to an integer value" % val)
        elif opt == "--fg":
            fg_color = val
        elif opt == "--latex":
            latex = [val]
        elif opt == "--lilypond":
            lilypond = True
        elif opt == "--lilypond-book":
            lilypond_book = [val]
        elif opt in ("--png", "--ppm"):
            output_format = opt[2:]
        elif opt in ("-v", "--verbose"):
            lyxpreview_tools.verbose = True

    # Determine input file
    if len(args) != 1:
        err = "A single input file is required, %s given" % (len(args) or "none")
        error("%s\n%s" % (err, usage(script_name)))

    input_path = args[0]
    dir, latex_file = os.path.split(input_path)

    # Check for the input file
    if not os.path.exists(input_path):
        error('File "%s" not found.' % input_path)
    if len(dir) != 0:
        os.chdir(dir)

    if lyxpreview_tools.verbose:
        f_out = open('verbose.txt', 'a')
        sys.stdout = f_out
        sys.stderr = f_out

    # Echo the settings
    progress("Running Python %s" % str(sys.version_info[:3]))
    progress("Starting %s..." % script_name)
    if os.name == "nt":
        progress("Use win32_modules: %d" % lyxpreview_tools.use_win32_modules)
    progress("Output format: %s" % output_format)
    progress("Foreground color: %s" % fg_color)
    progress("Background color: %s" % bg_color)
    progress("Resolution (dpi): %s" % dpi)
    progress("File to process: %s" % input_path)

    # For python > 2 convert strings to bytes
    if not PY2:
        fg_color = bytes(fg_color, 'ascii')
        bg_color = bytes(bg_color, 'ascii')

    fg_color_dvipng = make_texcolor(fg_color, False)
    bg_color_dvipng = make_texcolor(bg_color, False)

    # For python > 2 convert bytes to string
    if not PY2:
        fg_color_dvipng = fg_color_dvipng.decode('ascii')
        bg_color_dvipng = bg_color_dvipng.decode('ascii')

    # External programs used by the script.
    latex = find_exe_or_terminate(latex or latex_commands)
    bibtex = find_exe(bibtex or bibtex_commands)
    if lilypond:
        lilypond_book = find_exe_or_terminate(lilypond_book or
            ["lilypond-book --safe"])

    # These flavors of latex are known to produce pdf output
    pdf_output = latex in pdflatex_commands

    progress("Latex command: %s" % latex)
    progress("Latex produces pdf output: %s" % pdf_output)
    progress("Bibtex command: %s" % bibtex)
    progress("Lilypond-book command: %s" % lilypond_book)
    progress("Preprocess through lilypond-book: %s" % lilypond)
    progress("Altering the latex file for font size and colors")

    # Make sure that multiple defined macros and the microtype package
    # don't cause issues in the latex file.
    fix_latex_file(latex_file, pdf_output)

    if lilypond:
        progress("Preprocess the latex file through %s" % lilypond_book)
        if pdf_output:
            lilypond_book += " --pdf"
        lilypond_book += " --latex-program=%s" % latex.split()[0]

        # Make a copy of the latex file
        lytex_file = latex_file_re.sub(".lytex", latex_file)
        shutil.copyfile(latex_file, lytex_file)

        # Preprocess the latex file through lilypond-book.
        lytex_status, lytex_stdout = run_tex(lilypond_book, lytex_file)

    if pdf_output:
        progress("Using the legacy conversion method (PDF support)")
        return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
            bg_color, latex, pdf_output)

    # This can go once dvipng becomes widespread.
    dvipng = find_exe(["dvipng"])
    if dvipng == None:
        progress("Using the legacy conversion method (dvipng not found)")
        return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
            bg_color, latex, pdf_output)

    dv2dt = find_exe(["dv2dt"])
    if dv2dt == None:
        progress("Using the legacy conversion method (dv2dt not found)")
        return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
            bg_color, latex, pdf_output)

    pngtopnm = ""
    if output_format == "ppm":
        pngtopnm = find_exe(["pngtopnm"])
        if pngtopnm == None:
            progress("Using the legacy conversion method (pngtopnm not found)")
            return legacy_conversion_step1(latex_file, dpi, output_format,
                fg_color, bg_color, latex, pdf_output)

    # Compile the latex file.
    error_pages = []
    latex_status, latex_stdout = run_latex(latex, latex_file, bibtex)
    latex_log = latex_file_re.sub(".log", latex_file)
    if latex_status:
        progress("Will try to recover from %s failure" % latex)
        error_pages = check_latex_log(latex_log)

    # The dvi output file name
    dvi_file = latex_file_re.sub(".dvi", latex_file)

    # If there's no DVI output, look for PDF and go to legacy or fail
    if not os.path.isfile(dvi_file):
        # No DVI, is there a PDF?
        pdf_file = latex_file_re.sub(".pdf", latex_file)
        if os.path.isfile(pdf_file):
            progress("%s produced a PDF output, fallback to legacy." \
                % (os.path.basename(latex)))
            progress("Using the legacy conversion method (PDF support)")
            return legacy_conversion_step1(latex_file, dpi, output_format,
                fg_color, bg_color, latex, True)
        else:
            error("No DVI or PDF output. %s failed." \
                % (os.path.basename(latex)))

    # Look for PS literals or inclusion of pdflatex files in DVI pages
    # ps_pages: list of indexes of pages containing PS literals
    # pdf_pages: list of indexes of pages requiring running pdflatex
    # page_count: total number of pages
    # pages_parameter: parameter for dvipng to exclude pages with PostScript
    (ps_pages, pdf_pages, page_count, pages_parameter) = find_ps_pages(dvi_file)

    # If all pages need PostScript or pdflatex, directly use the legacy method.
    if len(ps_pages) == page_count:
        progress("Using the legacy conversion method (PostScript support)")
        return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
            bg_color, latex, pdf_output)
    elif len(pdf_pages) == page_count:
        progress("Using the legacy conversion method (PDF support)")
        return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
            bg_color, "pdflatex", True)

    # Retrieve resolution
    resolution = extract_resolution(latex_log, dpi)

    # Run the dvi file through dvipng.
    dvipng_call = '%s -Ttight -depth -height -D %d -fg "%s" -bg "%s" %s "%s"' \
        % (dvipng, resolution, fg_color_dvipng, bg_color_dvipng, pages_parameter, dvi_file)
    dvipng_status, dvipng_stdout = run_command(dvipng_call)

    if dvipng_status:
        warning("%s failed to generate images from %s... fallback to legacy method" \
              % (os.path.basename(dvipng), dvi_file))
        progress("Using the legacy conversion method (dvipng failed)")
        return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
            bg_color, latex, pdf_output)

    # Extract metrics info from dvipng_stdout.
    metrics_file = latex_file_re.sub(".metrics", latex_file)
    dvipng_metrics = extract_metrics_info(dvipng_stdout)

    # If some pages require PostScript pass them to legacy method
    if len(ps_pages) > 0:
        # Create a new LaTeX file just for the snippets needing
        # the legacy method
        legacy_latex_file = latex_file_re.sub("_legacy.tex", latex_file)
        filter_pages(latex_file, legacy_latex_file, ps_pages)

        # Pass the new LaTeX file to the legacy method
        progress("Pages %s include postscript specials" % ps_pages)
        progress("Using the legacy conversion method (PostScript support)")
        legacy_status, legacy_metrics = legacy_conversion_step1(legacy_latex_file,
            dpi, output_format, fg_color, bg_color, latex, pdf_output, True)

        # Now we need to mix metrics data from dvipng and the legacy method
        original_bitmap = latex_file_re.sub("%d." + output_format, legacy_latex_file)
        destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)

        # Join metrics from dvipng and legacy, and rename legacy bitmaps
        join_metrics_and_rename(dvipng_metrics, legacy_metrics, ps_pages,
            original_bitmap, destination_bitmap)

    # If some pages require running pdflatex pass them to legacy method
    if len(pdf_pages) > 0:
        # Create a new LaTeX file just for the snippets needing
        # the legacy method
        legacy_latex_file = latex_file_re.sub("_legacy.tex", latex_file)
        filter_pages(latex_file, legacy_latex_file, pdf_pages)

        # Pass the new LaTeX file to the legacy method
        progress("Pages %s require processing with pdflatex" % pdf_pages)
        progress("Using the legacy conversion method (PDF support)")
        legacy_status, legacy_metrics = legacy_conversion_step1(legacy_latex_file,
            dpi, output_format, fg_color, bg_color, "pdflatex", True, True)

        # Now we need to mix metrics data from dvipng and the legacy method
        original_bitmap = latex_file_re.sub("%d." + output_format, legacy_latex_file)
        destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)

        # Join metrics from dvipng and legacy, and rename legacy bitmaps
        join_metrics_and_rename(dvipng_metrics, legacy_metrics, pdf_pages,
            original_bitmap, destination_bitmap)

    # Invalidate metrics for pages that produced errors
    if len(error_pages) > 0:
        error_count = 0
        for index in error_pages:
            if index not in ps_pages and index not in pdf_pages:
                dvipng_metrics.pop(index - 1)
                dvipng_metrics.insert(index - 1, (index, -1.0))
                error_count += 1
        if error_count:
            warning("Failed to produce %d preview snippet(s)" % error_count)

    # Convert images to ppm format if necessary.
    if output_format == "ppm":
        convert_to_ppm_format(pngtopnm, latex_file_re.sub("", latex_file))

    # Actually create the .metrics file
    write_metrics_info(dvipng_metrics, metrics_file)

    return (0, dvipng_metrics)

if __name__ == "__main__":
    sys.exit(main(sys.argv)[0])


import pytest

import elasticsearch_dsl as es_dsl

from karp5.domain.services import search_service
from karp5.config import conf_mgr

@pytest.mark.parametrize("from_,size", [
    (0, 25),
    (1000, 25),
    (10000, 25),
    (0, None),
    (15000, 15875)
])
def test_large_lex(app_w_large_lex, from_, size):
    mode = "large_lex"
    total_num_entries = 20000
    es_search = es_dsl.Search(using=conf_mgr.elastic(mode), index=mode)
    result = search_service.execute_query(es_search, from_=from_, size=size)
    if size is None:
        expected_len_hits = total_num_entries - from_
    else:
        expected_len_hits = size if (from_ + size < total_num_entries) else total_num_entries - from_

    assert len(result["hits"]["hits"]) == expected_len_hits
    assert result["hits"]["total"] == total_num_entries
    for hit in result["hits"]["hits"]:
        assert "_source" in hit


from PyQt5 import Qt, QtCore, QtGui, QtWidgets
from ui.mainw import Ui_MainWindow
import os, json
import subprocess

class VirtualCD_Window(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(VirtualCD_Window, self).__init__()
        self.setupUi(self)
        self.setWindowOpacity(0.5)
        self.setWindowFlags(Qt.Qt.WindowCloseButtonHint | Qt.Qt.WindowMinimizeButtonHint | Qt.Qt.WindowStaysOnTopHint)
        self.wHeight = self.height()
        _result = subprocess.getstatusoutput('losetup -J')
        # print(_result)
        _loaded = False
        self._vcd_list = {}
        if _result[0] == 0:
            if _result[1] != '':
                self._vcd_list = json.loads(_result[1])
                # print(self._vcd_list)
                _loaded = True
                # print(_result[1].split('\n')[1])
        # print(_result)
        # print(type(_result))
        # print(_loaded)
        self.btnEject.setEnabled(_loaded)
        self.btnOpen.clicked.connect(self.loadISO)
        self.btnEject.clicked.connect(self.unloadISO)

    def loadISO(self):
        iso_file, file_type = QtWidgets.QFileDialog.getOpenFileName(self, caption='选择ISO文件', filter='光盘镜像文件(*.iso)')
        # print(iso_file, file_type)
        if iso_file != '':
            # print(f'选择文件为{iso_file}')
            result = subprocess.getstatusoutput(f'losetup -r /dev/loop0 {iso_file}')
            if result[0] != 0:
                result = subprocess.getstatusoutput(f'gksudo -m 加载虚拟光驱需要输入当前用户的密码才能继续 "losetup -r /dev/loop0 {iso_file}"')
            # print(result[0])
            if result[0] == 0:
                self.btnEject.setEnabled(True)
                _result = subprocess.getstatusoutput('losetup -J')
                self._vcd_list = json.loads(_result[1])
                # print(self._vcd_list)
            else:
                QtWidgets.QMessageBox.warning(self, '警告', f'文件"{iso_file}"加载失败！')
        # else:
            # print('没有选择任何文件')

    def unloadISO(self):
        iso_filename = os.path.basename(self._vcd_list['loopdevices'][0]['back-file'])
        iso_mnt_path = '/media/' + os.environ['USER'] + '/' +iso_filename[:iso_filename.rfind('.')]
        # print('iso filename', iso_filename, iso_mnt_path)
        if os.path.exists(iso_mnt_path):
            result = subprocess.getstatusoutput(f'umount {iso_mnt_path}')
            # print(result)
        result = subprocess.getstatusoutput('losetup -d /dev/loop0')
        # print(result)
        if result[0] != 0:
            result = subprocess.getstatusoutput('gksudo -m 弹出虚拟光驱需要输入当前用户的密码才能继续 "losetup -d /dev/loop0"')
            # print(result)
        if result[0]==0:
            self.btnEject.setEnabled(False)
        else:
            QtWidgets.QMessageBox.warning(self, '警告', '虚拟光驱弹出失败！')

if __name__=="__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    mainw = VirtualCD_Window()
    x = (app.desktop().width() - mainw.width()) / 2
    y = (app.desktop().height() - mainw.height()) / 2
    mainw.move(x, y)
    mainw.show()
    sys.exit(app.exec_())


import argparse
import logging
import subprocess
import sys

import stanza
from iso639 import languages

sys.path.append('./resources')
import unimorph_inflect

def download_unimorph(lg: str):
    logging.info("downloading UniMorph dictionary for %s" % lg)
    subprocess.run("mkdir -p unimorph_dicts", shell=True)
    subprocess.run(
        "wget -q https://raw.githubusercontent.com/unimorph/%s/master/%s -O unimorph_dicts/%s" % (
            lg, lg, lg),
        shell=True,
    )

def download_unimorph_inflect_models(lg: str):
    logging.info("downloading unimorph_inflect models for %s" % lg)
    unimorph_inflect.download(lg)

def prepare_label_vocab(lg: str):
    logging.info("preparing label vocabulary for %s" % lg)
    subprocess.run("mkdir -p label_vocab", shell=True)
    subprocess.run(
        "python morpheus_multilingual/utils/build_label_vocab.py -d unimorph_dicts/%s "
        "-l morpheus_multilingual/utils/dimension2label.txt -v label_vocab/%s" % (lg, lg),
        shell=True,
    )

def download_stanza_model(two_letter_lg: str):
    logging.info("downloading stanza model for %s" % two_letter_lg)
    stanza.download(two_letter_lg)

if __name__ == "__main__":

    logging.basicConfig(
        format="%(asctime)s - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
        level=logging.INFO,
        handlers=[logging.StreamHandler()],
    )
    parser = argparse.ArgumentParser(description="setup language specific resources")
    parser.add_argument("langs", type=str, nargs="+", help="list of language codes (e.g. rus, deu)")

    args = parser.parse_args()

    for lg in args.langs:
        download_unimorph(lg)
        download_unimorph_inflect_models(lg)
        prepare_label_vocab(lg)

        two_letter_lg = languages.get(part3="deu").alpha2
        download_stanza_model(two_letter_lg)


"""
Background

Algorithm Inorder(tree)
   1. Traverse the left subtree, i.e., call Inorder(left-subtree)
   2. Visit the root.
   3. Traverse the right subtree, i.e., call Inorder(right-subtree)
   **TODO: Can use a stack**

Algorithm Preorder(tree)
   1. Visit the root.
   2. Traverse the left subtree, i.e., call Preorder(left-subtree)
   3. Traverse the right subtree, i.e., call Preorder(right-subtree)

Algorithm Postorder(tree)
   1. Traverse the left subtree, i.e., call Postorder(left-subtree)
   2. Traverse the right subtree, i.e., call Postorder(right-subtree)
   3. Visit the root.
"""

"""
Notes:
It's known that inorder traversal of BST is an array sorted in the ascending order.
"""
from typing import List

class Node:
    def __init__(self, val):
        self.l_child = None
        self.r_child = None
        self.data = val

def binary_insert(root, node):
    if root is None:
        root = node
    else:
        if root.data > node.data:
            if root.l_child is None:
                root.l_child = node
            else:
                binary_insert(root.l_child, node)
        else:
            if root.r_child is None:
                root.r_child = node
            else:
                binary_insert(root.r_child, node)

"""
Trivial Solution:
1. First, in order traverse the tree and get the (almost) sorted list
2. Identify two swapped values in the list
3. Traverse the tree again to swap back
"""
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def recoverTree(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        nodes = self.in_order_traverse(root)
        print(f'nodes: {nodes}')
        self.first_val, self.second_val = self.find_swap(nodes)
        print(f'self.first_val: {self.first_val}')
        print(f'self.second_val: {self.second_val}')
        self.dfs(root)

    def dfs(self, root: TreeNode):
        if root is None:
            return
        if root.val == self.first_val:
            root.val = self.second_val
        elif root.val == self.second_val:
            root.val = self.first_val
        else:
            pass
        self.dfs(root.left)
        self.dfs(root.right)

    def in_order_traverse(self, root: TreeNode) -> List[int]:
        nodes = [root.val]
        if root.left is None and root.right is None:
            return nodes
        if root.left is not None:
            nodes = self.in_order_traverse(root.left) + nodes
        if root.right is not None:
            nodes = nodes + self.in_order_traverse(root.right)
        return nodes

    def find_swap(self, l: List[int]):
        first_pos = -1
        first_val = None
        second_pos = -1
        second_val = None
        for i in range(len(l) - 1):
            if l[i] > l[i + 1]:
                if first_pos == -1:
                    first_pos = i
                else:
                    second_pos = i + 1
        if second_pos == -1:
            second_pos = first_pos + 1
        first_val = l[first_pos]
        second_val = l[second_pos]
        return first_val, second_val

    def in_order_find_swap(self, root: TreeNode):
        """ Use interative method to traverse and find swap """
        x, y = None, None
        pred = None
        stack = []
        while len(stack) > 0 or root:
            while root:
                stack.append(root)
                root = root.left
            root = stack.pop()
            if pred and root.val < pred.val:
                y = root
                if x is None:
                    x = pred
                else:
                    pass
            pred = root
            root = root.right
        x.val, y.val = y.val, x.val

"""
The idea of Morris algorithm is to set the temporary link between the node and its predecessor: predecessor.right = root. So one starts from the node, computes its predecessor and verifies if the link is present.

There is no link? Set it and go to the left subtree.

There is a link? Break it and go to the right subtree.

There is one small issue to deal with : what if there is no left child, i.e. there is no left subtree? Then go straightforward to the right subtree.
"""


import argparse

def parse_args():
    """
    parsing and configuration
    :return: parse_args
    """
    desc = "Tensorflow implementation of pix2pix"
    parser = argparse.ArgumentParser(description=desc)

    parser.add_argument('--module', type=str, default='test_dataset',
                        help='Module to select: train, test ...')
    parser.add_argument('--is_training', type=bool, default=True,
                        help='When the module is train, True, else False')
    parser.add_argument('--tfrecord_dir', type=str, default='data/VOC-tfrecord',
                        help='Path of the dataset transferred to tfrecord')
    parser.add_argument('--split', type=str, default='train',
                        help='Type of data to transfer: train, test ...')
    parser.add_argument("--l1_weight", type=float, default=100.0,
                        help="weight on L1 term for generator gradient")#L1正则化项的权重参数
    parser.add_argument("--gan_weight", type=float, default=1.0,
                        help="weight on GAN term for generator gradient")#生成器梯度的GAN的权重参数
    parser.add_argument("--dataset1", type=str, default='C:\\project\\PY\\GAN\\pix2pix-tensorflow-master\\pix2pix-tensorflow-master\\photos\\combined\\tfrecords',
                        help="Path of the dataset")
    parser.add_argument("--dataset", type=str, default='C:\\project\\PY\\GAN\\pix2pix-tensorflow-master\\pix2pix-tensorflow-master\\rain\\facades\\train\\tfrecords',
                        help="Path of the dataset")
    parser.add_argument('--batch_size', type=int, default=1,
                        help='The size of each training batch')
    parser.add_argument('--num_epochs', type=int, default=1,
                        help='The number of the total training epoch')
    parser.add_argument('--img_width', type=int, default=256,
                        help='The width of img')
    parser.add_argument('--img_height', type=int, default=256,
                        help='The height of img')
    parser.add_argument('--scale_size', type=int, default=530,
                        help='The scale of img for crop')
    parser.add_argument('--crop_size', type=int, default=512,
                        help='The crop_size of img')
    return parser.parse_args()


#%%
import pandas as pd
import numpy as np
import os
import re
import time
from functools import wraps, reduce
from copy import deepcopy
from sklearn.model_selection import train_test_split

#%%
project_dict_second = {
    '01':'有源手术器械',
    '02':'无源手术器械',
    '03':'神经和心血管手术器械',
    '04':'骨科手术器械',
    '05':'耳鼻喉手术器械',
    '06':'医用成像器械',
    '07':'医用诊察监护器械',
    '08':'呼吸麻醉急救器械',
    '09':'物理治疗器械',
    '10':'矫形外科(骨科)手术器械',
    '11':'医疗器械消毒灭菌器械',
    '12':'妇产科用手术器械',
    '13':'计划生育手术器械',
    '14':'注射护理防护器械',
    '16':'眼科器械',
    '17':'口腔科器械',
    '18':'妇产科辅助生殖和避孕器械',
    '19':'物理治疗及康复设备',
    '20':'中医器械',
    '21':'医用诊察和监护器械',
    '22':'医用光学器具仪器及内窥镜设备',
    '23':'医用超声仪器及有关设备',
    '24':'医用激光仪器设备',
    '25':'医用高频仪器设备',
    '26':'物理治疗及康复设备',
    '27':'中医器械',
    '28':'医用成像器械(磁共振)',
    '30':'医用成像器械(X射线)',
    '31':'医用成像器械(成像辅助)',
    '32':'放射治疗器械',
    '33':'医用核素设备',
    '40':'临床检验器械',
    '41':'临床检验器械',
    '45':'输血透析和体外循环器械',
    '46':'认知言语试听障碍康复设备',
    '50':'??????',
    '54':'手术室急救室诊疗室设备及器具',
    '55':'口腔科器械',
    '56':'病房护理设备及器具',
    '57':'医疗器械消毒灭菌器械',
    '58':'医用冷疗低温冷藏设备及器具',
    '63':'口腔科器械',
    '64':'注射护理防护器械',
    '65':'医用缝合材料及粘合剂',
    '66':'医用高分子材料及制品',
    '70':'医用软件',
    '77':'无源手术器械'
}

project_dict_third = {
    '01':'有源手术器械',
    '02':'无源手术器械',
    '03':'神经和心血管手术器械',
    '04':'骨科手术器械',
    '05':'放射治疗器械',
    '06':'医用成像器械',
    '07':'医用侦察和监护器械',
    '08':'呼吸麻醉急救器械',
    '09':'物理治疗器械',
    '10':'输血透析体外循环器械',
    '11':'医疗器械消毒灭菌器械',
    '12':'有源植入器械',
    '13':'无源植入器械',
    '14':'注射护理防护器械',
    '15':'注射护理防护器械',
    '16':'眼科器械',
    '17':'口腔科器械',
    '18':'妇产科辅助生殖和避孕器械',
    '19':'物理治疗及康复设备',
    '20':'中医器械',
    '21':'医用诊察和监护器械',
    '22':'医用光学器具仪器及内窥镜设备',
    '23':'医用超声仪器及有关设备',
    '24':'医用激光仪器设备',
    '25':'医用高频仪器设备',
    '26':'物理治疗及康复设备',
    '27':'中医器械',
    '28':'医用成像器械(磁共振)',
    '30':'医用成像器械(X射线)',
    '31':'医用成像器械(成像辅助)',
    '32':'放射治疗器械',
    '33':'医用核素设备',
    '40':'临床检验器械',
    '41':'临床检验器械',
    '45':'输血透析和体外循环器械',
    '46':'无源植入器械',
    '54':'手术室急救室诊疗室设备及器具',
    '58':'物理治疗器械',
    '63':'口腔科器械',
    '64':'注射护理防护器械',
    '65':'医用缝合材料及粘合剂',
    '66':'注输护理和防护器械',
    '70':'医用软件',
    '77':'神经和心血管手术器械'
}

#%%
def benchmark():
    def middle(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            t_start = time.time()
            result = f(*args, **kwargs)
            t_end = time.time()
            print(f'{f.__name__} takes {t_end-t_start}')
            return result
        return wrapper
    return middle

@benchmark()
def rm_element(content: list, condition: str) -> list:
    result = deepcopy(content)
    for i in result[::-1]:
        if re.match(re.compile(condition), i) is not None:
            pass
        else:
            result.remove(i)
    return result

@benchmark()
def data_preprocess(folder_name: str,
                    project_dict_second: dict,
                    project_dict_third: dict) -> pd.DataFrame:

    # get the list of data files
    file_list = os.listdir(folder_name)

    # rm the unrelated file
    df_list = rm_element(file_list, '[0-9a-zA-Z\_]+\.csv')

    # loop the list, transfer it into pd.DataFrame and concat
    for i, j in enumerate(df_list):
        df_list[i] = pd.read_csv(folder_name+'/'+j)

    df = pd.concat(df_list)

    # select the data from 2010-2019
    df = df[df['注册证编号'].str.match(r'[\u4e00-\u9fa5]{1,5}201[1-9]{1}[23]{1}[0-7]{1}[0-9]{1}[0-9]{4}')]

    # select the target columns
    df = df.loc[:, ['注册证编号','产品名称','结构及组成/主要组成成分', '适用范围/预期用途']]

    # replace the class row
    df['注册证编号'] = df['注册证编号'].str.extract(r'(201[1-9]{1}[23]{1}[0-7]{1}[0-9]{1}[0-9]{4})')
    df['注册证编号'] = df['注册证编号'].str[4:7]

    # fill NaN
    df = df.fillna(value='')

    # concat text columns
    def column_add(c1: pd.Series, c2: pd.Series) -> pd.Series:
        return c1.str.cat(c2, sep=' ')

    df['合并数据'] = reduce(column_add, [df[column] for column in df.columns][1:])

    df = df[['注册证编号','合并数据']]

    # reset Index
    df.reset_index(drop=True)

    # rename dataframe
    df = df.rename(columns={'注册证编号':'label', '合并数据':'text'})

    # drop the \r\n\t in the str in the text col
    df['text'] = df['text'].str.replace('(\\t|\\n|\\r)', '', regex=True)

    # drop 215 cause manual check for conflict
    df = df[(df['label'] != '215') & (df['label'] != '234')]

    # drop duplicates
    df = df.drop_duplicates()

    # drop the label which just got 6 or less examples
    df = df.groupby('label').filter(lambda x: len(x) > 13)

    # transfer label into str
    def project(x: int, project_dict_second: dict, project_dict_third:dict) -> str:
        # type transform
        try:
            data = str(x)
        except:
            pass

        data = str(x)

        # administration category
        if data[0] == '2':
            result = '第二类'
            result = result + project_dict_second[data[1:]]
        else:
            result = '第三类'
            result = result + project_dict_third[data[1:]]

        return result

    df['label'] = df['label'].apply(project, args=(project_dict_second, project_dict_third))

    # split them in to train, val, test
    modelling, val = train_test_split(df, test_size=0.1, stratify=df['label'], random_state=42)
    train, test = train_test_split(modelling, test_size=0.11, stratify=modelling['label'], random_state=42)

    return train, val, test

#%%
if __name__ == '__main__':
    train, val, test = data_preprocess('data_folder', project_dict_second, project_dict_third)

    print(len(train))
    print(len(val))
    print(len(test))

    train.to_csv('cleaned_data/training_data.txt', index=False, header=False, sep='\t', encoding='utf-8')
    val.to_csv('cleaned_data/val_data.txt', index=False, header=False, sep='\t', encoding='utf-8')
    test.to_csv('cleaned_data/test_data.txt',index=False, header=False, sep='\t', encoding='utf-8')

    print(train.groupby(['label']).count())

    print(val.groupby(['label']).count())

    print(test.groupby(['label']).count())

    print(train.head())

    # when filter is > 11, label = 63
    # when filter is > 6, label = 67


import os
import pandas as pd
import random
import numpy as np
import tensorflow as tf
from datetime import datetime
import models
from sklearn.metrics import r2_score
import math

IDS_TRAIN = []
IDS_TRAIN.append([12, 0, 1, 11])
IDS_TRAIN.append([7, 0, 17, 11])
IDS_TRAIN.append([12, 5, 15, 9])
IDS_TRAIN.append([8, 22, 3, 11])
IDS_TRAIN.append([7, 0, 14, 9])

if not len(IDS_TRAIN):
    n_combs = 5
else:
    n_combs = len(IDS_TRAIN)

os.environ["CUDA_VISIBLE_DEVICES"] = ""
np.random.seed(0)
random.seed(0)

params = {}

params["path_data"] = []
params["path_data"].append("../Dataset_Collection/Datasets/Exp_2021-08-01 15-18-24/")
params["path_data"].append("../Dataset_Collection/Datasets/Exp_2021-08-04 16-58-16/")

params["BATCH_SIZE"] = 32
params["LOSS"] = 'mse'
params["N_ITERATIONS_VAL"] = 0.0625
params["CATEGORY_VAL"] = []
params["TERRAIN_IDS"] = []

params["LENGTH_SHOTS"] = 3
params["LENGTH_META"] = 3
params["N_SHOTS"] = 3
params["N_META"] = 1
params["LENGTH_SEQUENCE"] = 3
params["LENGTH_PAST"] = 0
params["MERGED_SHOTS_GEOM"] = True
params["MERGED_META_GEOM"] = True
params["MERGED_SHOTS_OUTPUT"] = True
params["MERGED_META_OUTPUT"] = True
params["MERGED_MODEL"] = True
params["SHOTS_EXTRA_INFO"] = []
params["WHICH_MODELS"] = []

params["ENERGY_NORM_TYPE"] = "standardize"
params["REMOVE_ROUGH"] = False
params["MAX_VAR_ROUGH"] = 2.25

params["MODEL"] = "model_ST-Conv1D"
params["REF_MODEL_DIR"] = "./Exp00/log_ST_conv1d/"
params["MODEL_LOG_NAME"] = "conv_sum"

params["BATCH_TYPE"] = "mixed"

params["INPUT_FEATURES"] = ["wheel_trace"]
params["WHEEL_TRACE_SHAPE"] = (78,40)
params["REMOVE_CENTRAL_BAND"] = 0

params["path_sum_indices"] = []
params["path_sum_indices"].append("../Dataset_Collection/Datasets/Exp_2021-08-01 15-18-24/Merged_Data/sum_indices.csv")
params["path_sum_indices"].append("../Dataset_Collection/Datasets/EExp_2021-08-04 16-58-16/Merged_Data/sum_indices.csv")

CATEGORIES = []
CATEGORIES.append([7, 8, 10, 12]) # Clay high moisture content
CATEGORIES.append([5, 0, 22]) # Loose frictional
CATEGORIES.append([1, 3, 4, 13, 14, 15, 16, 17]) # Compact frictional
CATEGORIES.append([9, 11]) # Dry clay

class Batch_Generator():
    """
    Keras Sequence object to train a model on larger-than-memory data.
    modified from: https://stackoverflow.com/questions/51843149/loading-batches-of-images-in-keras-from-pandas-dataframe
    """

    def __init__(self, df, df_idx, batch_size,  mode='train'):
        self.len_df = len(df)
        self.batch_size = batch_size
        self.mode = mode  # shuffle when in train mode
        self.df = {}
        self.df_idx = {}
        self.terrain_ids = list(df["terrain_id"].drop_duplicates().values)
        self.datasets = list(df["dataset"].drop_duplicates().values)
        for terrain_id in self.terrain_ids:
            self.df["{}".format(terrain_id)] = {}
            dfi = df[df.terrain_id==terrain_id]
            self.df_idx["{}".format(terrain_id)] = df_idx[df_idx.terrain_id==terrain_id]
            for dataset in self.datasets:
                self.df["{}".format(terrain_id)]["{}".format(dataset)] = dfi[dfi.dataset==dataset]

        if self.mode == 'train':
            self.total_steps =  int(math.ceil(self.len_df / float(self.batch_size))*params["N_ITERATIONS_TRAIN"])
        elif self.mode == 'validation':
            self.total_steps =  int(math.ceil(self.len_df / float(self.batch_size))*params["N_ITERATIONS_VAL"])
        self.step = 0
        self.epoch = 0

        self.on_epoch_end()

    def on_epoch_end(self):
        # Shuffles indexes after each epoch if in training mode
        self.indexes = range(self.len_df)
        if self.mode == 'train':
            self.indexes = random.sample(self.indexes, k=len(self.indexes))

    def get_batch(self):
        for i in range(self.batch_size):
            terrain_id = random.sample(self.terrain_ids,1)[0]
            dfx = self.df_idx["{}".format(terrain_id)]

            if params["BATCH_TYPE"] == "mixed" and len(self.datasets)>1:
                if random.random() > 1/3:
                    dataset_id = random.sample(self.datasets, 1)[0]
                    dfx = dfx[dfx["dataset"]==dataset_id]

            row = dfx.sample(n=params["N_SHOTS"]+params["N_META"])
            row_shots = row.sample(n=params["N_SHOTS"])
            row_meta = row.drop(row_shots.index)

            if len(self.datasets)>1:
                shot_tot = pd.DataFrame()
                for (k,d) in zip(row_shots.k.values,row_shots.dataset.values):
                    shot = self.df["{}".format(terrain_id)]["{}".format(d)].iloc[[k+z for z in range(params["LENGTH_SHOTS"])]]
                    shot_tot = pd.concat([shot_tot,shot])
                meta_tot = pd.DataFrame()
                for (k,d) in zip(row_meta.k.values,row_meta.dataset.values):
                    meta = self.df["{}".format(terrain_id)]["{}".format(d)].iloc[[k+z for z in range(params["LENGTH_META"])]]
                    meta_tot = pd.concat([meta_tot,meta])
            else:
                idx = []
                for k in row_shots.k.values:
                    idx.extend(k+z for z in range(params["LENGTH_SHOTS"]))
                shot_tot = self.df["{}".format(terrain_id)]["0"].iloc[idx]
                idx = []
                for k in row_meta.k.values:
                    idx.extend(k+z for z in range(params["LENGTH_META"]))
                meta_tot = self.df["{}".format(terrain_id)]["0"].iloc[idx]

            xe_shots = shot_tot.loc[:, ["energy"]].values
            ye_meta = meta_tot.loc[:, ["energy"]].values

            if params["SHOTS_EXTRA_INFO"]:
                xei_shots = shot_tot.loc[:, params["SHOTS_EXTRA_INFO"]].values

            xg_shots_string = shot_tot.loc[:, ["wheel_trace"]].values
            for p in range(len(xg_shots_string)):
                xg = np.array([float(v) for v in xg_shots_string[p][0].split(' ')]).reshape(params["WHEEL_TRACE_SHAPE"])
                if not p:
                    xg_shots = np.expand_dims(xg, axis=0)
                else:
                    xg_shots = np.concatenate(
                        [xg_shots, np.expand_dims(xg, axis=0)], axis=0)
            xg_meta_string = meta_tot.loc[:, ["wheel_trace"]].values
            for p in range(len(xg_meta_string)):
                xg = np.array([float(v) for v in xg_meta_string[p][0].split(' ')]).reshape(params["WHEEL_TRACE_SHAPE"])
                if not p:
                    xg_meta = np.expand_dims(xg, axis=0)
                else:
                    xg_meta = np.concatenate(
                        [xg_meta, np.expand_dims(xg, axis=0)], axis=0)

            if params["MERGED_SHOTS_GEOM"]:
                xg_shots_tot = np.empty((params["N_SHOTS"],
                                         xg_shots.shape[-2]+16 *
                                         (params["LENGTH_SHOTS"]-1),
                                         xg_shots.shape[-1]))
                zrel = shot_tot.loc[:, ["zrel"]].values
                for z in range(params["N_SHOTS"]):
                    # Merge the shots in a single terrain trace
                    xg_shots_tot[z, :xg_shots.shape[-2]
                                 ] = xg_shots[z*params["LENGTH_SHOTS"]]
                    for p in range(1, params["LENGTH_SHOTS"]):
                        xg_shots_tot[z, xg_shots.shape[-2]+16*(p-1):xg_shots.shape[-2]+16*p] = xg_shots[p+z *
                                                                                params["LENGTH_SHOTS"], -16:]+zrel[p+z*params["LENGTH_SHOTS"]]-zrel[z*params["LENGTH_SHOTS"]]
            else:
                xg_shots_tot = xg_shots
            if params["MERGED_META_GEOM"]:
                xg_meta_tot = np.empty((params["N_META"],
                                        xg_meta.shape[-2]+16 *
                                        (params["LENGTH_META"]-1),
                                        xg_meta.shape[-1]))
                zrel = meta_tot.loc[:, ["zrel"]].values
                for z in range(params["N_META"]):
                    # Merge the shots in a single terrain trace
                    xg_meta_tot[z, :xg_meta.shape[-2]
                                ] = xg_meta[z*params["LENGTH_META"]]
                    for p in range(1, params["LENGTH_META"]):
                        xg_meta_tot[z, xg_meta.shape[-2]+16*(p-1):xg_meta.shape[-2]+16*p] = xg_meta[p+z *
                                                                                params["LENGTH_META"], -16:]+zrel[p+z*params["LENGTH_META"]]-zrel[z*params["LENGTH_META"]]
            else:
                xg_meta_tot = xg_meta

            if not i:
                XG_SHOTS = np.expand_dims(xg_shots_tot, axis=0)
                XE_SHOTS = np.expand_dims(xe_shots, axis=0)
                XG_META = np.expand_dims(xg_meta_tot, axis=0)
                YE_META = np.expand_dims(ye_meta, axis=0)
                if params["SHOTS_EXTRA_INFO"]:
                    XEI_SHOTS = np.expand_dims(xei_shots, axis=0)
            else:
                XG_SHOTS = np.concatenate(
                    [XG_SHOTS, np.expand_dims(xg_shots_tot, axis=0)], axis=0)
                XE_SHOTS = np.concatenate(
                    [XE_SHOTS, np.expand_dims(xe_shots, axis=0)], axis=0)
                XG_META = np.concatenate(
                    [XG_META, np.expand_dims(xg_meta_tot, axis=0)], axis=0)
                YE_META = np.concatenate(
                    [YE_META, np.expand_dims(ye_meta, axis=0)], axis=0)
                if params["SHOTS_EXTRA_INFO"]:
                    XEI_SHOTS = np.concatenate(
                    [XEI_SHOTS, np.expand_dims(xei_shots, axis=0)], axis=0)

        if params["REMOVE_CENTRAL_BAND"]:
            XG_l = XG_SHOTS[:, :, :, :(
                params["WHEEL_TRACE_SHAPE"][1]-params["REMOVE_CENTRAL_BAND"])//2]
            XG_r = XG_SHOTS[:, :, :, -(params["WHEEL_TRACE_SHAPE"]
                                       [1]-params["REMOVE_CENTRAL_BAND"])//2:]
            XG_SHOTS = np.concatenate([XG_l, XG_r], axis=-1)
            XG_l = XG_META[:, :, :, :(
                params["WHEEL_TRACE_SHAPE"][1]-params["REMOVE_CENTRAL_BAND"])//2]
            XG_r = XG_META[:, :, :, -(params["WHEEL_TRACE_SHAPE"]
                                      [1]-params["REMOVE_CENTRAL_BAND"])//2:]
            XG_META = np.concatenate([XG_l, XG_r], axis=-1)

        if params["SHOTS_EXTRA_INFO"]:
            XEI_SHOTS_TOT = np.concatenate([XE_SHOTS, XEI_SHOTS], axis = -1)
        else:
            XEI_SHOTS_TOT = XE_SHOTS
        X = [XG_SHOTS, XEI_SHOTS_TOT, XG_META]
        Y = []
        for sh in range(params["N_SHOTS"]):
            if params["MERGED_META_OUTPUT"]:
                for z in range(params["N_META"]):
                    Y.append(
                        np.sum(YE_META[:, z*params["LENGTH_META"]:(z+1)*params["LENGTH_META"]], axis=1))
            else:
                for z in range(params["N_META"]*params["LENGTH_META"]):
                    Y.append(YE_META[:, z])

        Y = np.stack([y for y in Y],axis=1)

        if self.step == self.total_steps-1:
            self.step = 0
            self.epoch += 1
            self.on_epoch_end()
        else:
            self.step += 1

        if self.mode == "prediction":
            return X
        else:
            return X, Y

def reference_pred(ref_models, X):
    XG_SHOTS, XE_SHOTS, XG_META = X
    XE_SHOTS = np.sum(XE_SHOTS.reshape(params["BATCH_SIZE"],params["N_SHOTS"],params["LENGTH_SHOTS"]),axis=-1)
    for n_shots in range(1, params["N_SHOTS"]+1):
        # Identify most similar model based on shots
        error = np.empty((len(ref_models),params["BATCH_SIZE"]))
        for i, ref_model in enumerate(ref_models):
            for b in range(params["BATCH_SIZE"]):
                ye = ref_model["model"](XG_SHOTS[b,:n_shots])
                ye = ye.numpy().reshape((ye.shape[0],))
                if not b:
                    YE_SHOTS = np.expand_dims(ye,axis=0)
                else:
                    YE_SHOTS = np.concatenate([YE_SHOTS,np.expand_dims(ye,axis=0)],axis=0)
            YE_SHOTS = YE_SHOTS*ref_model["energy_val2"] + params["N_SHOTS"]*ref_model["energy_val1"]
            if params["LOSS"] == 'mse':
                error_ref_i = np.sum(np.square(YE_SHOTS-XE_SHOTS[:,:n_shots]),axis=1)
            elif params["LOSS"] == 'mae':
                error_ref_i = np.sum(np.abs(YE_SHOTS-XE_SHOTS[:,:n_shots]),axis=1)
            error[i] = error_ref_i.squeeze()
        best_m = np.argmin(error,axis=0)
        # Use it to make new prediction
        for i, b in enumerate(best_m):
            ye = ref_models[b]["model"](XG_META[i])
            ye = ye.numpy()*ref_models[b]["energy_val2"]+params["N_SHOTS"]*ref_models[b]["energy_val1"]
            if not i:
                YE_P_META = np.expand_dims(ye,axis=0)
            else:
                YE_P_META = np.concatenate([YE_P_META,np.expand_dims(ye,axis=0)],axis=0)

        if n_shots ==1:
            YE_P_META_TOT = YE_P_META
        else:
            YE_P_META_TOT = np.concatenate([YE_P_META_TOT,YE_P_META],axis=1)

    return YE_P_META_TOT

def isint(x):
    try:
        int(x)
        return True
    except ValueError:
        return False

def isfloat(x):
    if '[' in x:
        x = x[1:-1]
    try:
        float(x)
        return True
    except ValueError:
        return False

def main():
    if not params["TERRAIN_IDS"]:
        id_files = [".csv"]
    else:
        id_files = ["data_{}.csv".format(idt) for idt in params["TERRAIN_IDS"]]
    df = pd.DataFrame()
    for i, path_data in enumerate(params["path_data"]):
        files = os.listdir(path_data)
        for file in files:
            if any([p in file for p in id_files]):
                dfi = pd.read_csv(path_data+file)
                ## Removing some of data:
                # Samples without failures
                try:
                    dfi = dfi[dfi.goal==1]
                except:
                    pass
                # Samples without initial acceleration
                dfi = dfi[dfi.segment!=0]
                try:
                    # Samples without low mean speed
                    dfi = dfi[dfi.mean_speed>0.87]
                    # Samples without low initial speed
                    dfi = dfi[dfi.initial_speed>0.88]
                except:
                    # Samples without low mean speed
                    dfi = dfi[dfi.mean_speed_long>0.87]
                    # Samples without low initial speed
                    dfi = dfi[dfi.initial_speed_long>0.88]
                if params["REMOVE_ROUGH"]:
                    # Samples without rough pitch/roll variations
                    dfi = dfi.loc[(dfi.var_pitch_est <=params["MAX_VAR_ROUGH"]) | (dfi.var_roll_est <=params["MAX_VAR_ROUGH"])]
                # dfi["std_pitch_est"] = dfi["var_pitch_est"].pow(0.5)
                # dfi["std_roll_est"] = dfi["var_roll_est"].pow(0.5)
                # dfi["curvature"] = dfi["curvature"]*100
                # dfi["curvature_tm1"] = dfi["curvature_tm1"]*100

                try:
                    dfi = dfi.drop(columns=['wheel_types'])
                except:
                    pass
                dfi["energy"] = dfi["energy"].clip(lower = 0.0)

                dfi["dataset"] = [i]*len(dfi)
                df = pd.concat([df,dfi])

    df_sum_indices = pd.DataFrame()
    for i, path_idx in enumerate(params["path_sum_indices"]):
        dfi = pd.read_csv(path_idx).drop_duplicates()
        dfi["dataset"] = [i]*len(dfi)
        df_sum_indices = pd.concat([df_sum_indices,dfi], ignore_index=True)

    results = []
    for comb in range(n_combs):

        if not len(IDS_TRAIN):
            # Selection of terrains for training and validation by category
            id_val = []
            id_train = []
            for cat in range(len(CATEGORIES)):
                t_ids = CATEGORIES[cat]
                id_train.extend(random.sample(t_ids, params["TRAINING_DATASETS_PER_CATEGORY"][cat]))
                id_val.extend([t for t in t_ids if t not in id_train])
        else:
            id_train = IDS_TRAIN[comb]
            id_val = []
            for cat in range(len(CATEGORIES)):
                t_ids = CATEGORIES[cat]
                id_val.extend([t for t in t_ids if t not in id_train])

        df_train = df[df["terrain_id"].isin(id_train)]
        df_val = df[df["terrain_id"].isin(id_val)]

        print("Validation Samples: {}".format(len(df_val)))
        print("Training Terrains {}".format(id_train))
        print("Validation Terrains {}".format(id_val))

        # Ref models
        ref_models = []
        summary_flag = True
        for id_t in id_train:
            ref_models.append({"model": models.get_model(params, summary=summary_flag),
                               "id_t": str(id_t),
                               "energy_val1": 0, "energy_val2": 1})
            model_weights = "{}{}_{}/model_best.hdf5".format(params["REF_MODEL_DIR"],id_t,params["MODEL_LOG_NAME"])
            ref_models[-1]["model"].load_weights(model_weights)
            summary_flag = False
            if params["ENERGY_NORM_TYPE"]:
                log_dir = "{}{}_{}/log_params.txt".format(params["REF_MODEL_DIR"],id_t,params["MODEL_LOG_NAME"])
                l = open(log_dir, "r")
                cont = l.readlines()
                for line, c in enumerate(cont):
                    if params["ENERGY_NORM_TYPE"]:
                        if params["ENERGY_NORM_TYPE"] == 'standardize':
                            val1 = 'mean'
                            val2 = 'std'
                        elif params["ENERGY_NORM_TYPE"] == 'normalize':
                            val1 = 'min'
                            val2 = 'int'
                        if "energy_{}".format(val1) in c:
                            eval1 = [x for x in c.split() if isfloat(x)][0]
                            if '[' in eval1:
                                eval1 = eval1[1:-1]
                            eval1 = float(eval1)
                            ref_models[-1]["energy_val1"] = eval1
                        elif "energy_{}".format(val2) in c:
                            eval2 = [x for x in c.split() if isfloat(x)][0]
                            if '[' in eval2:
                                eval2 = eval2[1:-1]
                            eval2 = float(eval2)
                            ref_models[-1]["energy_val2"] = eval2

        bg_val = Batch_Generator(df_val, df_sum_indices,params["BATCH_SIZE"], mode='validation')
        # Losses
        ref_loss = []
        ref_r2_score = []
        for i in range(params["N_SHOTS"]):
            if params["LOSS"] == 'mse':
                ref_loss.append(tf.keras.metrics.MeanSquaredError(name='ref_loss_{}'.format(i)))
            else:
                ref_loss.append(tf.keras.metrics.MeanAbsoluteError(name='ref_loss_{}'.format(i)))
            ref_r2_score.append(tf.keras.metrics.Mean(name='ref_r2_score_{}'.format(i)))

        for i in range(len(ref_loss)):
            ref_loss[i].reset_states()
            ref_r2_score[i].reset_states()

        while bg_val.epoch < 1:
            if not bg_val.step%10:
                print("File: {}. Perc: {}%".format(comb, round(bg_val.step/bg_val.total_steps*100,2)))
            X, Y = bg_val.get_batch()
            # Reference Model
            Y_P = reference_pred(ref_models,X)
            for i in range(len(ref_loss)):
                ref_loss[i](Y[:,i],Y_P[:,i])
                ref_r2_score[i](r2_score(tf.reshape(Y[:,i],[-1]),tf.reshape(Y_P[:,i],[-1])))

        r = {"comb": comb}
        for i in range(params["N_SHOTS"]):
            r["mse_{}".format(i)] = ref_loss[i].result().numpy()
            r["r2_{}".format(i)] = ref_r2_score[i].result().numpy()

        results.append(r)
        print(results[-1])

    print()
    print(results)

if __name__ == "__main__":
    main()


import os
import sqlite3
from sqlite3.dbapi2 import Connection, Cursor
from typing import Any, AnyStr, Final, Optional

import codenotes.db.utilities.notes as notes
import codenotes.db.utilities.notes_categories as notes_categories
import codenotes.db.utilities.tasks as tasks
import codenotes.db.utilities.tasks_categories as tasks_categories

class SQLiteConnection:

    """Connection with SQLite3 class

    Class has the purpouse to manage the connection with the database created with
    sqlite3. Everytime the constructor is executed, it connects to the database, then
    execute the SQL statements that creates the tables if they not exist. Also, this class
    allows you to execute sql, commit the transactions and close the connection with
    the database.

    Attributes
    ---------
    BASE_DIR: Final[AnyStr]
        Root path where the __main__ is executed

    DATABASE_NAME:Final[str]
        Name of the database

    DATABASE_PATH: Final[str]
        Complete path where is the database (its getted after joinning BASE_DIR & DATABASE_NAME)

    connection: Connection
        Connection with the database specified in DATABASE_PATH

    cursor: Cursor
        Cursor created to interact with the database
    """

    BASE_DIR: Final[AnyStr] = os.path.dirname(
        os.path.dirname(os.path.abspath(__file__))
    )
    DATABASE_NAME: Final[str] = "codenotes.db"
    DATABASE_PATH: Final[str] = os.path.join(BASE_DIR, DATABASE_NAME)

    connection: Connection
    cursor: Cursor

    def __init__(self) -> None:
        """SQLiteConnection Constructor"""
        self.connection = sqlite3.connect(self.DATABASE_PATH)
        self.cursor = self.connection.cursor()

        self.exec_sql(notes_categories.CREATE_TABLE)  # Notes Category Table
        self.cursor.execute(
            notes_categories.INSERT_DEFAULT_CATEGORY
        )  # Insert Default Category
        self.exec_sql(notes.CREATE_TABLE)  # Notes Table

        self.exec_sql(tasks_categories.CREATE_TABLE)  # Task Category Table
        self.cursor.execute(
            tasks_categories.INSERT_DEFAULT_CATEGORY
        )  # Insert Default Category
        self.exec_sql(tasks.CREATE_TABLE)  # Tasks Table

        self.connection.commit()

    def exec_sql(self, sql: str, values: Optional[tuple[Any]] = None) -> Cursor:
        """Method that executes sql command

        Parameters
        ----------
        sql : str
            SQL statement to be executed

        values: tuple[Any]
            Optional argument typo of tuple, which contains the values the sql statement requires

        Returns
        -------
        cursor : Cursor
            Method will return the cursor that the method execute returns
        """
        if values is not None:
            return self.cursor.execute(sql, values)
        return self.cursor.execute(sql)

    def commit(self) -> None:
        """Commits the current transaction"""
        self.connection.commit()

    def close(self) -> None:
        """Close database and cursor connection"""
        self.cursor.close()
        self.connection.close()


import os
from PIL import Image
import PIL.ImageOps
import argparse
import torchvision as V
import matplotlib.pyplot as plt
import ujson as json
import glob
import re
from . import configs
import rich
from rich.progress import track
console = rich.get_console()

def mnist_burst(dest: str, split: str, *,
        mnist='mnist'):
    '''
    burst mnist dataset into png images

    svg background is white by default in vtracer,
    and hence the polygons draw the background.
    We need to invert the color, so that the paths are drawing
    the foreground object.
    '''
    assert(split in ('train', 'test'))
    assert(mnist in ('mnist', 'fashion'))
    if not os.path.exists(dest):
        os.mkdir(dest)
    if mnist == 'mnist':
        data = V.datasets.MNIST(root=configs.datasets.mnist.root,
                            train=True if split=='train' else False,
                            download=True)
    elif mnist == 'fashion':
        data = V.datasets.FashionMNIST(root=configs.datasets.fashion.root,
                            train=True if split=='train' else False,
                            download=True)
    for i, (x, y) in enumerate(data):
        fpath = os.path.join(dest, '%05d-%1d.png'%(i, y))
        xinv = PIL.ImageOps.invert(x)
        xinv.save(fpath)
        console.print(fpath)
        print(x, y)
    print(len(data))

def mnist_collect(src: str):
    '''
    collect processed data from
    '''
    dataset = []
    files = glob.glob(os.path.join(src, '*.json'))
    for file in track(files):
        with open(file, 'rt') as f:
            j = json.load(f)
        label = re.match(r'.*/\d+-(\d).json', file).groups()[0]
        j[0]['label'] = int(label)
        dataset.append(j)
    with open(src+'.json', 'wt') as f:
        json.dump(dataset, f)
    console.print('>_< done')

if __name__ == '__main__':
    ag = argparse.ArgumentParser('python3 -m veccls.mnist')
    ag.add_argument('action', choices=('burst', 'collect'))
    ag.add_argument('-d', '--destination', default='.', help='dest directory')
    ag.add_argument('-s', '--split', default='train', choices=('train', 'test'))
    ag = ag.parse_args()
    console.print(ag)

    if ag.action == 'burst':
        mnist_burst(ag.destination, ag.split)
    elif ag.action == 'collect':
        mnist_collect(ag.destination)


from scapy.all import *
from scapy.layers.dot11 import Dot11, Dot11Beacon, Dot11Elt, RadioTap, Dot11Deauth
from colorama import Fore
from string import Template
import run

class fakeAP:

    fake_ap_interface = "none"
    fake_ssid = "fake"
    sniffer = "none"

    def __init__(self, fake_ap_interface, fake_ssid, sniffer) -> None:

        self.fake_ap_interface = fake_ap_interface
        self.fake_ssid = fake_ssid
        self.sniffer = sniffer

        # Remove the build directory
        os.system('rm -rf build/')
        # Copy the Templates directory to a new build directory
        os.system('cp -r Templates build')

        # Clear port 53
        os.system('systemctl disable systemd-resolved.service >/dev/null 2>&1')
        os.system('systemctl stop systemd-resolved>/dev/null 2>&1')

        # Modify the hostapd.conf file with the access point interface and network name
        with open('build/hostapd.conf', 'r+') as f:
            template = Template(f.read())
            f.seek(0)
            f.write(template.substitute(INTERFACE=self.fake_ap_interface, NETWORK=self.fake_ssid))
            f.truncate()
        # Modify the dnsmasq.conf file with the access point interface
        with open('build/dnsmasq.conf', 'r+') as f:
            template = Template(f.read())
            f.seek(0)
            f.write(template.substitute(INTERFACE=self.fake_ap_interface))
            f.truncate()
        # Modify the prepareAP.sh file with the access point interface
        with open('build/prepareAP.sh', 'r+') as f:
            template = Template(f.read())
            f.seek(0)
            f.write(template.substitute(INTERFACE=self.fake_ap_interface))
            f.truncate()
        # Modify the cleanup.sh file with the sniffer and access point interfaces
        with open('build/cleanup.sh', 'r+') as f:
            template = Template(f.read())
            f.seek(0)
            f.write(template.substitute(SNIFFER=self.sniffer, AP=self.fake_ap_interface))
            f.truncate()

        # AP with address 10.0.0.1 on the given interface
        os.system(f"ifconfig {fake_ap_interface} up 10.0.0.1 netmask 255.255.255.0")
        print(Fore.YELLOW + "[-->] setting {} with ip 10.0.0.1 netmask 255.255.255.0".format(self.fake_ap_interface))

        # Clear all IP Rules
        os.system('iptables --flush')
        os.system('iptables --table nat --flush')
        os.system('iptables --delete-chain')
        os.system('iptables --table nat --delete-chain')
        print(Fore.YELLOW + "[-->] Clearing all IP Rules")

        # Redirect any request to the captive portal
        os.system(f'iptables -t nat -A PREROUTING  -i usb0 -p tcp --dport 80 -j DNAT  --to-destination 10.0.0.1:80')
        os.system(f'iptables -t nat -A PREROUTING  -i usb0 -p tcp --dport 443 -j DNAT  --to-destination 10.0.0.1:80')
        print(Fore.YELLOW + "[-->] Redirecting any request to the captive portal")

        # Enable internet access use the usb0 interface
        os.system(f'iptables -A FORWARD --in-interface {fake_ap_interface} -j ACCEPT')
        os.system(f'iptables -t nat -A POSTROUTING --out-interface usb0 -j MASQUERADE')
        print(Fore.YELLOW + "[-->] Enableing internet access")

        # Initial wifi interface configuration (seems to fix problems)
        os.system(f'ip link set {fake_ap_interface} down')
        os.system(f'ip addr flush dev {fake_ap_interface}')
        os.system(f'ip link set {fake_ap_interface} up')
        os.system(f'ip addr add 10.0.0.1/10 dev {fake_ap_interface}')

        # Enable IP forwarding from one interface to another
        os.system('echo 1 > /proc/sys/net/ipv4/ip_forward')
        os.system(f'sleep 3')
        print(Fore.YELLOW + "[-->] Enable IP forwarding from one interface to another")

        cmd = "sudo dnsmasq -C build/dnsmasq.conf"
        p = subprocess.Popen(cmd, shell=True, preexec_fn=os.setsid)

        os.system(f'route add default gw 10.0.0.1')

        self.start_apache()

        os.system("hostapd build/hostapd.conf -B >/dev/null 2>&1")

        print(Fore.GREEN + '[+] The Fake Access Point is now available using Name : {} '.format(fake_ssid))

        # listen_thread = Thread(target=start_listen, daemon=True)
        # listen_thread.start()
        while True:
            user_input = input(Fore.YELLOW + '[*] to turn off the Access Point Please press \"done\"\n\n')
            if user_input == 'done':
                run.exit_and_cleanup(0, 'Done! , thanks for using')
            else:
                print(Fore.RED + 'invalid option...')

    def start_apache(self):
        os.system('sudo rm -r /var/www/html/* 2>/dev/null')  # delete all folders and files in this directory
        os.system('sudo cp -r fake-facebook-website/* /var/www/html')
        os.system('sudo chmod 777 /var/www/html/*')
        os.system('sudo chmod 777 /var/www/html')

        # update rules inside 000-default.conf of apache2
        os.system('sudo cp -f 000-default.conf /etc/apache2/sites-enabled')
        os.system('a2enmod rewrite >/dev/null 2>&1')  # enable the mod_rewrite in apache
        os.system('service apache2 restart >/dev/null 2>&1')     # reload and restart apache2

        print(Fore.YELLOW + '\n[*] appache server start successfully')
        time.sleep(1)


import math
import random
import keyboard
from random import choice
import pygame
import pygame.freetype
from pygame.draw import *

pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.font.init()
pygame.init()
# Ускорение свободного падения
g = 3
# Громкость музыки и выбор музыки (менять не надо)
track = 0
vol = 0.5
# Разброс снарядов (+- по x и +- по y)
razbros = 0
# Количество жизний целей, бота, игрока
livetarget = 3
livebot = 10
livegun = 5
# Задаем ширину и высоту окна
WIDTH = 1500
HEIGHT = 700
# Делаем поверхности
screen = pygame.display.set_mode((WIDTH, HEIGHT))
boomscr = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
# Вывод текста
text2 = pygame.font.Font(None, 36)
text1 = pygame.font.Font(None, 150)
# Различные счетчики
schet = 0  # Счетчик счета
schetcounter = 0  # Счетчик тысяч очков, нужно для призыва нового бота
FPS = 60
clock = pygame.time.Clock()
tickcounter = 0  # Счетчик тиков до перезарядки
tcglobal = 0  # Глобальный счетчик тиков
tcshoot = 0  # Запоминает время предыдущего выстрела игрока
# Массивы
balls = []  # Снаряды
targets = []  # Пассивные цели
kapli = []  # Капли эффекта убийства
booms = []  # Эффект взрыва
# Флаги
Ps = True  # Флаг паузы музыки
Flag = True  # Изменение режима управления, сейчас управление мышкой
finished = False  # Флаг выхода из основного цикла
kt = 5  # Количество пассивных целей
auto = 0  # Отвечает за включение автоматического режима стрельбы тройным снарядом
# Цвета
RED = 0xFF0000
BLUE = 0x0000FF
YELLOW = 0xFFC91F
GREEN = 0x00FF00
MAGENTA = 0xFF03B8
CYAN = 0x00FFCC
BLACK = (0, 0, 0)
WHITE = 0xFFFFFF
GREY = 0x7D7D7D
GAME_COLORS = [RED, BLUE, YELLOW, GREEN, MAGENTA, CYAN]
# Музыка и звуки
pulemet = pygame.mixer.Sound("Звуки/pulemet.ogg")
pulemet.set_volume(0.1)
fon1 = "Звуки/фон1.ogg"
fon2 = "Звуки/фон2.ogg"
fon3 = "Звуки/фон3.ogg"
fon4 = "Звуки/фон4.ogg"
MUSIC = [fon1, fon2, fon3, fon4]

class Ball:
    def __init__(self, screen: pygame.Surface):
        """ Конструктор класса ball

        Args:
        x - начальное положение мяча по горизонтали
        y - начальное положение мяча по вертикали
        g - ускорение свободного падения
        """
        self.screen = screen
        self.g = g
        self.x = 40
        self.y = 450
        self.r = random.randint(20, 20)
        self.vx = 0
        self.vy = 0
        self.color = choice(GAME_COLORS)
        self.live = 30

    def move(self):
        """Переместить мяч по прошествии единицы времени.

        Метод описывает перемещение мяча за один кадр перерисовки. То есть, обновляет значения
        self.x и self.y с учетом скоростей self.vx и self.vy, силы гравитации, действующей на мяч,
        и стен по краям окна. Слева и сверху стен нет.
        """
        if self.x + self.r <= WIDTH:
            self.x = self.x + self.vx
        else:
            if self.x + self.r >= WIDTH:
                self.x = WIDTH - self.r
            self.vx = self.vx * -1
            self.x = self.x + self.vx

        if self.y + self.r <= HEIGHT:
            self.vy = self.vy + self.g
            self.y = self.y + self.vy - 1
        else:
            if self.y + self.r >= HEIGHT:
                self.y = HEIGHT - self.r
            self.vy = self.vy * -1
            self.vy = self.vy + self.g
            self.y = self.y + self.vy + 1

    def draw(self):
        pygame.draw.circle(self.screen, self.color, (self.x, self.y), self.r)

    def hittest(self, obj):
        if ((obj.x - self.x) ** 2 + (obj.y - self.y) ** 2 <= (obj.r + self.r) ** 2) and type(obj) != Gun and type(
                obj) != Ball:
            return True
        else:
            return False

class BallBot(Ball):
    def hittest(self, obj):
        if ((obj.x - self.x) ** 2 + (obj.y - self.y) ** 2 <= (obj.r + self.r) ** 2) and type(obj) != BotKiller and type(
                obj) != BallBot:
            return True
        else:
            return False

class Gun:
    def __init__(self, screen):
        self.live = livegun
        self.liveconst = self.live
        self.r = 10
        self.screen = screen
        self.f2_power = 10
        self.f2_on = 0
        self.an = 1
        self.color = GREY
        self.x = 20
        self.y = 450
        # vx и vy нужны для отправки в BotKiller для расчета упреждения
        self.vx = 0
        self.vy = 0
        self.mx = 0
        self.my = 0
    def fire2_start(self, k):
        self.f2_on = k

    def fire2_end(self, px, py):
        """Выстрел мячом.

        Происходит при отпускании кнопки мыши.
        Начальные значения компонент скорости мяча vx и vy зависят от положения мыши.
        """
        posx = px
        posy = py
        global balls
        new_ball = Ball(self.screen)
        if posx > self.x:
            self.an = math.atan((-posy + self.y) / (posx - self.x))
        if posx < self.x:
            self.an = math.pi + math.atan((-posy + self.y) / (posx - self.x))
        if posx == self.x:
            self.an = math.pi / 2
        new_ball.vx = self.f2_power * math.cos(self.an)
        new_ball.vy = -self.f2_power * math.sin(self.an)
        new_ball.x = math.cos(self.an) * (self.f2_power + 10) + self.x
        new_ball.y = -(math.sin(self.an) * (self.f2_power + 10)) + self.y
        balls.append(new_ball)

    def fire2_end2(self, px, py):
        """Выстрел тройным мячом.

        Происходит при отпускании кнопки мыши.
        Начальные значения компонент скорости мяча vx и vy зависят от положения мыши.
        """
        posx = px
        posy = py
        global balls
        new_ball1 = Ball(self.screen)
        new_ball2 = Ball(self.screen)
        new_ball3 = Ball(self.screen)
        if posx > self.x:
            self.an = math.atan((-posy + self.y) / (posx - self.x))
        if posx < self.x:
            self.an = math.pi + math.atan((-posy + self.y) / (posx - self.x))
        if posx == self.x:
            self.an = math.pi / 2
        new_ball1.vx = self.f2_power * math.cos(self.an)
        new_ball1.vy = -self.f2_power * math.sin(self.an)
        new_ball2.vx = self.f2_power * math.cos(self.an)
        new_ball2.vy = -self.f2_power * math.sin(self.an)
        new_ball3.vx = self.f2_power * math.cos(self.an)
        new_ball3.vy = -self.f2_power * math.sin(self.an)
        new_ball1.x = math.cos(self.an) * (self.f2_power + 10) + self.x
        new_ball1.y = -(math.sin(self.an) * (self.f2_power + 10)) + self.y
        new_ball2.x = math.cos(self.an) * (self.f2_power + 10) + self.x - (math.cos(self.an + math.pi / 2)) * 40
        new_ball2.y = -(math.sin(self.an) * (self.f2_power + 10)) + self.y + math.sin(self.an + math.pi / 2) * 40
        new_ball3.x = math.cos(self.an) * (self.f2_power + 10) + self.x + (math.cos(self.an + math.pi / 2)) * 40
        new_ball3.y = -(math.sin(self.an) * (self.f2_power + 10)) + self.y - math.sin(self.an + math.pi / 2) * 40
        balls.append(new_ball1)
        balls.append(new_ball2)
        balls.append(new_ball3)
        self.f2_power = 10

    def targetting(self, x, y):
        """Прицеливание. Зависит от положения мыши."""
        xm = x
        ym = y
        self.mx = x
        self.my = y
        if xm > self.x:
            self.an = math.atan((-ym + self.y) / (xm - self.x))
        if xm < self.x:
            self.an = math.pi + math.atan((-ym + self.y) / (xm - self.x))
        if xm == self.x:
            self.an = math.pi / 2

    def draw(self, tickcounter):
        if tickcounter >= FPS:
            tick = FPS
        else:
            tick = tickcounter
        '''Расчет полосочек хп и перезарядки, аналогично и у бота'''
        line(screen, self.color, (self.x, self.y),
             (math.cos(self.an) * (self.f2_power + 10) + self.x, -(math.sin(self.an) * (self.f2_power + 10)) + self.y),
             width=20)
        line(screen, BLUE, (self.x - 20, self.y - 30),
             (self.x - 20 + int(40 * (self.live / self.liveconst)), self.y - 30),
             width=4)
        rect(screen, BLACK, [(self.x - 20, self.y - 32), (41, 6)], width=1)
        line(screen, GREEN, (self.x - 20, self.y - 38), (self.x - 20 + int(40 * (tick / FPS)), self.y - 38), width=4)
        rect(screen, BLACK, [(self.x - 20, self.y - 40), (41, 6)], width=1)
        circle(screen, RED, (self.x, self.y), self.r)

    def power_up(self):
        '''Расчет силы выстрела и рисование прицела. '''
        if self.f2_on == 0:
            if self.f2_power < 100:
                self.f2_power += 1
            self.color = (int((100 - self.f2_power) * 2.5), 0, 0)
            for i in range(1, 10):
                circle(screen, RED, (
                    self.x + math.cos(self.an) * (self.f2_power + 10) + math.cos(self.an) * self.f2_power * i,
                    self.y - (math.sin(self.an) * (self.f2_power + 10)) - (
                            math.sin(self.an) * self.f2_power * i - (g * i ** 2) / 2)), 5)
        elif self.f2_on == 1:
            if self.f2_power < 100:
                self.f2_power += 1
            self.color = (int((100 - self.f2_power) * 2.5), 0, 0)
        else:
            self.color = GREY

    def move(self):
        '''Два типа управления.
        Переключаются флагом Flag в начале программы
        '''
        x0 = self.x
        y0 = self.y
        if Flag == False:
            if self.x < WIDTH:
                if keyboard.is_pressed('w'):
                    self.y -= 5
                if keyboard.is_pressed('a'):
                    self.x -= 5
                if keyboard.is_pressed('s'):
                    self.y += 5
                if keyboard.is_pressed('d'):
                    self.x += 5
            else:
                self.x = 40
        else:
            if self.x < WIDTH:
                if (self.mx - self.x) ** 2 + (self.my - self.y) ** 2 >= 900:
                    if keyboard.is_pressed('w'):
                        self.x += math.cos(self.an) * 10
                        self.y += -(math.sin(self.an) * 10)
                    if keyboard.is_pressed('a'):
                        self.x += -(math.cos(self.an - math.pi / 2) * 10)
                        self.y += (math.sin(self.an - math.pi / 2) * 10)
                    if keyboard.is_pressed('s'):
                        self.x += -(math.cos(self.an) * 10)
                        self.y += math.sin(self.an) * 10
                    if keyboard.is_pressed('d'):
                        self.x += math.cos(self.an - math.pi / 2) * 10
                        self.y += -(math.sin(self.an - math.pi / 2) * 10)
                else:
                    self.x += 0
                    self.y += 0

            else:
                self.x = 40
        self.vx = self.x - x0
        self.vy = self.y - y0

class BotKiller:
    def __init__(self, screen):
        self.live = livebot
        self.liveconst = self.live
        self.screen = screen
        self.an = 1
        self.an2 = 0
        self.color = GREY
        self.x = WIDTH - 20
        self.y = 400
        self.t = 30  # время полета снаряда в тиках, от него зависит скорость полета снаряда
        self.r = 30
        self.flag = False
        self.xlnach = 0
        self.vx = 0
        self.vy = 0
        self.gamerx = 0
        self.gamery = 0
    def fire(self           ):
        """Выстрел мячом.

        Происходит каждые 2 секунды
        Начальные значения компонент скорости мяча vx и vy зависят от положения игрока.
        """
        global balls
        new_ball = BallBot(self.screen)
        new_ball.vx = self.vx + random.randint(-razbros, razbros)  # Разброс
        new_ball.vy = self.vy + random.randint(-razbros, razbros)
        new_ball.x = math.cos(self.an2) * 60 + self.x
        new_ball.y = -(math.sin(self.an2) * 60) + self.y
        balls.append(new_ball)

    def targetting(self, obj):
        """Прицеливание. Зависит от положения игрока.

        Тут также расчитываются скорости снарядов по осям и угол, на который отклоняется сама пушка
        """
        self.gamerx = obj.x
        self.gamery = obj.y
        if obj.x > self.x:
            self.an = math.atan((-obj.y + self.y) / (obj.x - self.x))
        if obj.x < self.x:
            self.an = math.pi + math.atan((-obj.y + self.y) / (obj.x - self.x))
        if obj.x == self.x:
            self.an = math.pi / 2
        if self.flag == False:
            self.xlnach = abs(self.x - obj.x)
            self.flag = True
        t = self.t * (abs(self.x - obj.x) / self.xlnach) * 1.1
        if t == 0:
            t = 1
        self.vx = (obj.x + obj.vx * t - (math.cos(self.an2) * 60 + self.x)) / t
        self.vy = (obj.y + obj.vy * t - (-(math.sin(self.an2) * 60) + self.y)) / t - (g * t) / 2
        circle(screen, RED, (obj.x + obj.vx * t, obj.y + obj.vy * t), 10)
        xobj = self.x + self.vx * 5
        yobj = self.y + self.vy * 5
        if xobj > self.x:
            self.an2 = math.atan((-yobj + self.y) / (xobj - self.x))
        if xobj < self.x:
            self.an2 = math.pi + math.atan((-yobj + self.y) / (xobj - self.x))
        if xobj == self.x:
            self.an2 = math.pi / 2

    def draw(self, tickcounter):
        # Рисование пушки
        tick = tickcounter
        line(screen, self.color, (self.x, self.y),
             (math.cos(self.an2) * 60 + self.x, -(math.sin(self.an2) * 60) + self.y), width=20)
        line(screen, RED, (self.x - 20, self.y - 30),
             (self.x - 20 + int(40 * (self.live / self.liveconst)), self.y - 30), width=4)
        rect(screen, BLACK, [(self.x - 20, self.y - 32), (41, 6)], width=1)
        line(screen, GREEN, (self.x - 20, self.y - 38), (self.x - 20 + int(40 * (tick / (FPS * 2))), self.y - 38),
             width=4)
        rect(screen, BLACK, [(self.x - 20, self.y - 40), (41, 6)], width=1)
        circle(screen, BLUE, (self.x, self.y), 20)

    def move(self):
        if (self.gamerx - self.x) ** 2 + (self.gamery - self.y) ** 2 >= 90000:
            self.x += math.cos(self.an) * 1
            self.y -= math.sin(self.an) * 1
        else:
            self.x += 0
            self.y -= 0

class Target:
    def __init__(self):
        self.live = livetarget
        self.rmin = 10
        self.rmax = 50
        self.r = random.randint(self.rmin, self.rmax)
        self.x = random.randint(self.r + 100, WIDTH - self.r - 100)
        self.y = random.randint(self.r + 100, HEIGHT - self.r - 100)
        self.xspeed = random.randint(1, 10)
        self.yspeed = random.randint(1, 10)
        self.color = RED
        self.pr = 0  # Степень прозрачности
        self.zt = int(255 / self.live)  # Убывание прозрачности

    def move(self):
        if (self.x + self.r <= WIDTH) and (self.x - self.r >= 0):
            self.x = self.x + self.xspeed
        else:
            if self.x + self.r >= WIDTH:
                self.x = WIDTH - self.r
            if self.x - self.r <= 0:
                self.x = self.r
            self.xspeed = self.xspeed * -1
            self.x = self.x + self.xspeed

        if (self.y + self.r <= HEIGHT) and (self.y - self.r >= 0):
            self.y = self.y + self.yspeed
        else:
            if self.y + self.r >= HEIGHT:
                self.y = HEIGHT - self.r
            if self.y - self.r <= 0:
                self.y = self.r
            self.yspeed = self.yspeed * -1
            self.y = self.y + self.yspeed

    def draw(self):
        self.pr = self.zt * self.live
        if self.pr < 20:
            self.pr = 0
        circle(boomscr, (255, 0, 0, abs(self.pr)), (self.x, self.y), self.r)
        screen.blit(boomscr, (0, 0))

class KaplyaOfTarget:

    def __init__(self, xx, yy, speed, color, r):
        self.g = 1
        self.x = xx
        self.y = yy
        self.r = int(r / 5)
        self.color = color
        self.xspeed = (random.randint(-3, 3) + speed) * 1.5
        self.yspeed = random.randint(-5, 5)

    def move(self):
        self.yspeed = self.yspeed + self.g
        self.x = int(self.x + self.xspeed)
        self.y = int(self.y + self.yspeed)
        if self.y > HEIGHT + 10:
            self.yspeed = 0

    def draw(self):
        circle(screen, self.color, (self.x, self.y), self.r)

class Boom:
    def __init__(self, xx, yy, r):
        self.x = xx
        self.y = yy
        self.r = int(r / 2)
        self.dr = 7
        self.liveticks = 8
        self.death = 1
        self.pr = 255
        self.zt = int(self.pr / (self.liveticks / self.death))

    def draw(self):
        self.r += self.dr
        self.pr -= self.zt
        if self.pr < 20:
            self.pr = 0
        self.liveticks -= self.death
        circle(boomscr, (255, 0, 0, self.pr), (self.x, self.y), self.r)
        screen.blit(boomscr, (0, 0))

gun = Gun(screen)
targets.append(gun)
for t in range(kt):
    t = Target()
    targets.append(t)

while not finished:
    clock.tick(FPS)
    tickcounter += 1
    tcglobal += 1
    # Создание нового бота
    if schet // 1000 > schetcounter:
        schetcounter += 1
        bot = BotKiller(screen)
        targets.append(bot)

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()
        if event.type == pygame.QUIT:
            finished = True
        # При нажатии на esс игра заканчивается, при нажатии на пробел меняется флаг автоматической стрельбы
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                finished = True
            elif event.key == pygame.K_SPACE:
                auto = 1
            elif event.key == pygame.K_UP:
                if vol < 1:
                    vol = vol + 0.1
                else:
                    vol = 1
                pygame.mixer.music.set_volume(vol)
            elif event.key == pygame.K_DOWN:
                if vol > 0:
                    vol = vol - 0.1
                else:
                    vol = 0
                pygame.mixer.music.set_volume(vol)
            elif event.key == pygame.K_RIGHT:
                if track < 3:
                    track = track + 1
                else:
                    track = 3
                pygame.mixer.music.load(MUSIC[track])
                pygame.mixer.music.play(-1)
            elif event.key == pygame.K_LEFT:
                if track > 0:
                    track = track - 1
                else:
                    track = 0
                pygame.mixer.music.load(MUSIC[track])
                pygame.mixer.music.play(-1)
            if event.key == pygame.K_p:
                Ps = not Ps
                if Ps:
                    pygame.mixer.music.pause()
                else:
                    pygame.mixer.music.unpause()
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                auto = 0
        # Сама автоматическая стрельба
        elif auto == 1:
            gun.fire2_start(1)
            gun.f2_power = 100
            gun.targetting(pos[0], pos[1])
            if tcglobal - tcshoot >= FPS / 10:
                gun.fire2_end2(pos[0], pos[1])
                tcshoot = tcglobal
                pulemet.play()
        elif auto == 0:
            gun.fire2_start(2)
            gun.f2_power = 10
            auto = 2
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                gun.fire2_start(0)
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                if tcglobal - tcshoot >= FPS:
                    gun.fire2_end(pos[0], pos[1])
                    tcshoot = tcglobal
                    gun.fire2_start(2)
                    gun.f2_power = 10
                    pulemet.play()
                else:
                    gun.fire2_start(2)
                    gun.f2_power = 10

        else:
            gun.targetting(pos[0], pos[1])

    # Проверка убитых юнитов, проверка попаданий, запись новых эффектов в массивы, добавление игровых очков
    for b in balls:
        if b.x < -10:
            balls.remove(b)
        else:
            b.draw()
            b.move()
            for t in targets:
                if b.hittest(t) == True:
                    t.live -= 1
                    boom = Boom(b.x, b.y, b.r)
                    booms.append(boom)
                    if b in balls:
                        balls.remove(b)
                    k1 = random.randint(5, 10)
                    if t.live == 0:
                        if type(t) != BotKiller and type(t) != Gun:
                            for i in range(k1):
                                kaplya = KaplyaOfTarget(int(t.x), int(t.y), t.xspeed, t.color, t.r)
                                kapli.append(kaplya)
                            schet += int(100 - ((t.r - t.rmin) / (t.rmax - t.rmin)) * 100)
                        elif type(t) == BotKiller:
                            schet += 500
                        else:
                            finished = not finished
                        boom = Boom(t.x, t.y, t.r)
                        booms.append(boom)
                        targets.remove(t)
                        t = Target()
                        targets.append(t)
    for b1 in balls:
        for b2 in balls:
            if b1.hittest(b2) == True:
                boom = Boom((b1.x + b2.x) / 2, (b1.y + b2.y) / 2, b1.r / 2)
                booms.append(boom)
                balls.remove(b1)
                balls.remove(b2)
    # Отрисовка юнитов, стрельба ботов
    for t in targets:
        t.move()
        if type(t) == BotKiller:
            if tickcounter >= FPS * 2:
                t.fire()
                tickcounter = 0
                pulemet.play()
            else:
                t.targetting(gun)
            t.draw(tickcounter)
        elif type(t) == Gun:
            t.draw(tcglobal - tcshoot)
        else:
            t.draw()
    for k in kapli:
        if k.y > HEIGHT:
            kapli.remove(k)
        else:
            k.draw()
            k.move()
    for boom in booms:
        if boom.liveticks == 0:
            booms.remove(boom)
        else:
            boom.draw()
    gun.power_up()
    schetv = text2.render("score:" + " " + str(schet), True, (180, 0, 0))
    screen.blit(schetv, (10, 50))
    pygame.display.update()
    screen.fill(WHITE)
    boomscr.fill(WHITE)
# Конечная заставка
finished = not finished
while not finished:
    clock.tick(FPS)
    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()
        if event.type == pygame.QUIT:
            finished = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                finished = True

    schetv = text1.render("GAME OVER", True, (180, 0, 0))
    screen.blit(schetv, (WIDTH / 2 - 350, HEIGHT / 2 - 50))

    pygame.display.update()
    screen.fill(WHITE)
pygame.quit()

Игры/Пушка/gun.py


from PyMca5.PyMcaPhysics.xrf import FastXRFLinearFit
from XRDXRFutils.data import DataXRF, SyntheticDataXRF
import h5py
from os.path import basename, dirname, join, exists
from os import remove

class FastFit():
    def __init__(self, data = None, cfgfile = None, outputdir = None):
        self.data = data
        self.cfgfile = cfgfile
        self.outputdir = outputdir
        self.outputRoot = "IMAGES"
        if self.outputdir:
            self.filename = join(self.outputdir, f'{self.outputRoot}.h5')

    def fit(self):
        if type(self.data) != type(None) and self.cfgfile and self.outputdir:
            self.fastfit = FastXRFLinearFit.FastXRFLinearFit()
            self.fastfit.setFitConfigurationFile(self.cfgfile)

            outbuffer = self.fastfit.fitMultipleSpectra(
                y = self.data,
                weight = 1,
                refit = 1,
                concentrations = 0,
                outputDir = self.outputdir,
                outputRoot= self.outputRoot,
                h5  = True)

            zero = outbuffer['configuration']['detector']['zero']
            gain = outbuffer['configuration']['detector']['gain']
            self.parameters = (0, gain, zero)

            self._load_fit_results()

        return self

    def _load_fit_results(self):
        self.labels  = {}
        with h5py.File(self.filename, 'r') as f:
            keys = f['images']['xrf_fit']['results']['parameters'].keys()
            for k in keys:
                if 'errors' not in k:
                    label = f['images']['xrf_fit']['results']['parameters'][k][()]
                    self.labels[k.replace('_','-')] = label

    def get_labels(self):
        if hasattr(self, "labels"):
            return self.labels

    def get_calibration_pars(self):
        if hasattr(self, "parameters"):
            return self.parameters