text:
"""
 -----------------------------------------------------------------------------------------------------------
 Package:    AequilibraE

 Name:       Gravity model calibration
 Purpose:    Implement a procedure to calibrate gravity models

 Original Author:  Pedro Camargo (c@margo.co)
 Contributors:
 Last edited by: Pedro Camargo

 Website:    www.AequilibraE.com
 Repository:  https://github.com/AequilibraE/AequilibraE

 Created:    22/10/2016
 Updated:    02/10/2017
 Copyright:   (c) AequilibraE authors
 Licence:     See LICENSE.TXT
 -----------------------------------------------------------------------------------------------------------
"""

# The procedures implemented in this code are some of those suggested in
# Modelling Transport, 4th Edition
# Ortuzar and Willumsen, Wiley 2011

# The referred authors have no responsability over this work, of course

import sys, os
from time import clock, strftime, gmtime
from gravity_application import GravityApplication, SyntheticGravityModel
from ..matrix import AequilibraeMatrix, AequilibraEData
import numpy as np
import sys
import yaml
from ..parameters import Parameters

class GravityCalibration:
    """"
        where function is: 'EXPO' or 'POWER'. 'GAMMA' and 'FRICTION FACTORS' to be implemented at a later time
        parameters are: 'max trip length'
        """
    def __init__(self, **kwargs):

        self.__required_parameters = ['max trip length', 'max iterations', 'max error']
        self.parameters = kwargs.get('parameters', self.get_parameters())

        self.nan_as_zero = kwargs.get('nan_as_zero', False)
        self.matrix = kwargs.get('matrix')
        self.impedance = kwargs.get('impedance')
        deterrence_function = str(kwargs.get('function', '')).upper()

        if self.nan_as_zero:
            self.matrix = self.matrix.copy()
            self.impedance = self.impedance.copy()

        self.result_matrix = None
        self.rows = None
        self.columns = None
        self.gap = np.inf

        self.error = None
        self.gravity = None

        self.comput_core = None
        self.impedance_core = None

        self.itera = 0
        self.max_iter = None
        self.max_error = None
        self.gap = np.inf

        self.report = ['  #####    GRAVITY CALIBRATION    #####  ', '']
        self.report.append('Functional form: ' + deterrence_function)
        self.model = SyntheticGravityModel()
        if deterrence_function not in self.model.valid_functions:
            raise ValueError ('Function needs to be one of these: ' + ', '.join(self.model.valid_functions))
        else:
            self.model.function = deterrence_function

    def assemble_model(self, b1):
        # NEED TO SET PARAMETERS #
        if self.model.function == "EXPO":
            self.model.beta = float(b1)
        elif self.model.function == "POWER":
            self.model.alpha = float(b1)

    def calibrate(self):
        t = clock()
        # initialize auxiliary variables
        b0, b1, c0, c1 = None, None, None, None
        max_cost = self.parameters['max trip length']
        self.max_iter = self.parameters['max iterations']
        self.max_error = self.parameters['max error']

        # Check the inputs
        self.check_inputs()
        if self.model.function in ["EXPO", "POWER"]:
            # filtering for all costs over limit

            a = 1
            if max_cost > 0:
                a = (self.impedance.matrix[self.impedance_core][:, :] < max_cost).astype(int)

            #weighted average cost
            self.report.append('Iteration: 1')
            cstar = np.nansum(self.impedance.matrix[self.impedance_core][:,:] * self.result_matrix.gravity[:, :]  * a) / \
                    np.nansum(self.result_matrix.gravity[:, :]  * a)

            b0 = 1 / cstar

            self.assemble_model(b0)
            c0 = self.apply_gravity()
            for i in self.gravity.report:
                self.report.append('       ' + i)
            self.report.append('')
            self.report.append('')

            bm1 = b0
            bm = b0 * c0 / cstar

            self.report.append('Iteration: 2')
            self.assemble_model(bm)

            cm = self.apply_gravity()
            for i in self.gravity.report:
                self.report.append('       ' + i)
            self.report.append('Error: ' +  "{:.2E}".format(float(np.nansum(abs((bm / bm1) - 1)))))
            self.report.append('')
            cm1 = c0

        # While the max iterations has not been reached and the error is still too large
        self.itera = 2
        while self.itera < self.max_iter and self.gap > self.max_error:
            self.report.append('Iteration: ' + str(self.itera + 1))
            aux = bm
            bm = ((cstar - cm1) * bm - (cstar - cm) * bm) / (cm - cm1)
            bm1 = aux
            cm1 = cm

            self.assemble_model(bm1)
            cm = self.apply_gravity()

            for i in self.gravity.report:
                self.report.append('       ' + i)
            self.report.append('Error: ' + "{:.2E}".format(float(np.nansum(abs((bm / bm1) - 1)))))
            self.report.append('')

            # compute convergence criteria
            self.gap = abs((bm / bm1) - 1)
            self.itera += 1

        if self.itera == self.max_iter:
            self.report.append("DID NOT CONVERGE. Stopped in  " + str(self.itera) + "  with a global error of " + str(self.gap))
        else:
            self.report.append("Converged in " + str(self.itera) + "  iterations to a global error of " + str(self.gap))
        s = clock() - t
        m, s1 = divmod(s, 60)
        s -= m * 60
        h, m = divmod(m, 60)
        t =  "%d:%02d:%2.4f" % (h, m, s)

        self.report.append('Running time: ' + t)

    def check_inputs(self):
        if not isinstance(self.impedance, AequilibraeMatrix):
            raise TypeError('Impedance matrix needs to be an instance of AequilibraEMatrix')

        if not isinstance(self.matrix, AequilibraeMatrix):
            raise TypeError('Observed matrix needs to be an instance of AequilibraEMatrix')

        # Check data dimensions
        if not np.array_equal(self.impedance.index, self.impedance.index):
            raise ValueError('Indices from impedance matrix do not match those from seed matrix')

        # Check if matrices were set for computation
        mats = [(self.matrix, 'Observed matrix'), (self.impedance, 'Impedance matrix')]
        for matrix, title in mats:
            if matrix.matrix_view is None:
                raise ValueError(title + ' needs to be set for computation')
            else:
                if len(matrix.matrix_view.shape[:]) > 2:
                    raise ValueError(title + "' computational view needs to be set for a single matrix core")

            if np.nansum(matrix.matrix_view.data) == 0:
                raise ValueError(title + 'has only zero values')
            if np.nanmin(matrix.matrix_view.data) < 0:
                raise ValueError(title + 'has negative values')

        # Augment parameters if we happen to have only passed one
        default_parameters = self.get_parameters()
        for para in self.__required_parameters:
            if para not in self.parameters:
                self.parameters[para] = default_parameters[para]

        # Prepare the data for computation
        self.comput_core = self.matrix.view_names[0]

        self.result_matrix = self.matrix.copy(cores=[self.comput_core], names=['gravity'])

        self.rows = AequilibraEData()
        self.rows.create_empty(entries=self.matrix.zones, field_names=['rows'], memory_mode=True)
        self.rows.index[:] = self.matrix.index[:]
        self.rows.rows[:] = self.matrix.rows()[:]

        self.columns = AequilibraEData()
        self.columns.create_empty(entries=self.matrix.zones, field_names=['columns'], memory_mode=True)
        self.columns.index[:] = self.matrix.index[:]
        self.columns.columns[:] = self.matrix.columns()[:]

        self.impedance_core = self.impedance.view_names[0]

    def apply_gravity(self):
        args = {'impedance': self.impedance,
                'rows': self.rows,
                'row_field': 'rows',
                'columns': self.columns,
                'column_field': 'columns',
                'model': self.model,
                'parameters': self.parameters,
                'nan_as_zero': self.nan_as_zero}

        self.gravity = GravityApplication(**args)
        self.gravity.apply()
        self.result_matrix = self.gravity.output

        return np.nansum(self.impedance.matrix[self.impedance_core][:,:] * self.result_matrix.gravity[:, :]) \
               / np.nansum(self.result_matrix.gravity[:, :])

    def get_parameters(self):
        par = Parameters().parameters
        para = par['distribution']['ipf'].copy()
        para.update(par['distribution']['gravity'])
        return para

max_stars_repo_name: jamiecook/AequilibraE

max_issues_repo_name: jamiecook/AequilibraE

max_forks_repo_name: jamiecook/AequilibraE


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2021, NeXpy Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING, distributed with this software.
#-----------------------------------------------------------------------------

""" A minimal application using the Qt console-style Jupyter frontend.
"""

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import logging
import logging.handlers
import os
import pkg_resources
import shutil
import signal
import sys
import tempfile

from .pyqt import QtCore, QtGui, QtWidgets, QtVersion

from IPython import __version__ as ipython_version
from jupyter_client.consoleapp import JupyterConsoleApp, app_aliases, app_flags
from jupyter_core.application import JupyterApp, base_aliases, base_flags
from matplotlib import __version__ as mpl_version
from qtconsole import __version__, styles
from qtconsole.jupyter_widget import JupyterWidget
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from traitlets import Any, CBool, Dict, Unicode
from traitlets.config.application import boolean_flag, catch_config_error

from nexusformat.nexus import NXroot, nxclasses, nxload, nxversion

from .. import __version__ as nexpy_version
from .mainwindow import MainWindow
from .treeview import NXtree
from .utils import (NXConfigParser, NXGarbageCollector, NXLogger,
                    initialize_preferences, report_exception, timestamp_age)

#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------

_tree = None
_shell = None
_mainwindow = None
_nexpy_dir = None
_examples = """
nexpy                      # start the GUI application
"""

#-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------

flags = dict(base_flags)
qt_flags = {
    'plain' : ({'NXConsoleApp' : {'plain' : True}},
            "Disable rich text support."),
}
qt_flags.update(boolean_flag(
    'banner', 'NXConsoleApp.display_banner',
    "Display a banner upon starting the QtConsole.",
    "Don't display a banner upon starting the QtConsole."
))

# and app_flags from the Console Mixin
qt_flags.update(app_flags)
# add frontend flags to the full set
flags.update(qt_flags)

# start with copy of base jupyter aliases
aliases = dict(base_aliases)
qt_aliases = dict(
    style = 'JupyterWidget.syntax_style',
    stylesheet = 'NXConsoleApp.stylesheet',

    editor = 'JupyterWidget.editor',
    paging = 'ConsoleWidget.paging',
)
# and app_aliases from the Console Mixin
qt_aliases.update(app_aliases)
qt_aliases.update({'gui-completion':'ConsoleWidget.gui_completion'})
# add frontend aliases to the full set
aliases.update(qt_aliases)

# get flags&aliases into sets, and remove a couple that
# shouldn't be scrubbed from backend flags:
qt_aliases = set(qt_aliases)
qt_flags = set(qt_flags)

#-----------------------------------------------------------------------------
# NXConsoleApp
#-----------------------------------------------------------------------------

class NXConsoleApp(JupyterApp, JupyterConsoleApp):
    name = 'nexpy-console'
    version = __version__
    description = """
        The NeXpy Console.

        This launches a Console-style application using Qt.

        The console is embedded in a GUI that contains a tree view of
        all NXroot groups and a matplotlib plotting pane. It also has all
        the added benefits of a Jupyter Qt Console with multiline editing,
        autocompletion, tooltips, command line histories and the ability to
        save your session as HTML or print the output.

    """
    examples = _examples

    classes = [JupyterWidget] + JupyterConsoleApp.classes
    flags = Dict(flags)
    aliases = Dict(aliases)
    frontend_flags = Any(qt_flags)
    frontend_aliases = Any(qt_aliases)

    stylesheet = Unicode('', config=True,
        help="path to a custom CSS stylesheet")

    hide_menubar = CBool(False, config=True,
        help="Start the console window with the menu bar hidden.")

    plain = CBool(False, config=True,
        help="Use a plaintext widget instead of rich text.")

    display_banner = CBool(True, config=True,
        help="Whether to display a banner upon starting the QtConsole."
    )

    def _plain_changed(self, name, old, new):
        kind = 'plain' if new else 'rich'
        self.config.ConsoleWidget.kind = kind
        if new:
            self.widget_factory = JupyterWidget
        else:
            self.widget_factory = RichJupyterWidget

    # the factory for creating a widget
    widget_factory = Any(RichJupyterWidget)

    def parse_command_line(self, argv=None):
        super(NXConsoleApp, self).parse_command_line(argv)
        self.build_kernel_argv(argv)

    def init_dir(self):
        """Initialize NeXpy home directory"""
        home_dir = os.path.abspath(os.path.expanduser('~'))
        nexpy_dir = os.path.join(home_dir, '.nexpy')
        if not os.path.exists(nexpy_dir):
            parent = os.path.dirname(nexpy_dir)
            if not os.access(parent, os.W_OK):
                nexpy_dir = tempfile.mkdtemp()
            else:
                os.mkdir(nexpy_dir)
        for subdirectory in ['backups', 'functions', 'models', 'plugins',
                             'readers', 'scripts']:
            directory = os.path.join(nexpy_dir, subdirectory)
            if not os.path.exists(directory):
                os.mkdir(directory)
        global _nexpy_dir
        self.nexpy_dir = _nexpy_dir = nexpy_dir
        self.backup_dir = os.path.join(self.nexpy_dir, 'backups')
        self.plugin_dir = os.path.join(self.nexpy_dir, 'plugins')
        self.reader_dir = os.path.join(self.nexpy_dir, 'readers')
        self.script_dir = os.path.join(self.nexpy_dir, 'scripts')
        self.function_dir = os.path.join(self.nexpy_dir, 'functions')
        self.model_dir = os.path.join(self.nexpy_dir, 'models')
        sys.path.append(self.function_dir)
        self.scratch_file = os.path.join(self.nexpy_dir, 'w0.nxs')
        if not os.path.exists(self.scratch_file):
            NXroot().save(self.scratch_file)

    def init_settings(self):
        """Initialize access to the NeXpy settings file."""
        self.settings_file = os.path.join(self.nexpy_dir, 'settings.ini')
        self.settings = NXConfigParser(self.settings_file)
        initialize_preferences(self.settings)
        def backup_age(backup):
            try:
                return timestamp_age(os.path.basename(os.path.dirname(backup)))
            except ValueError:
                return 0
        backups = self.settings.options('backups')
        for backup in backups:
            if not (os.path.exists(backup) and
                    os.path.realpath(backup).startswith(self.backup_dir)):
                self.settings.remove_option('backups', backup)
            elif backup_age(backup) > 5:
                try:
                    shutil.rmtree(os.path.dirname(os.path.realpath(backup)))
                    self.settings.remove_option('backups', backup)
                except OSError:
                    pass
        self.settings.save()

    def init_log(self):
        """Initialize the NeXpy logger."""
        log_file = os.path.join(self.nexpy_dir, 'nexpy.log')
        handler = logging.handlers.RotatingFileHandler(log_file,
                                                       maxBytes=50000,
                                                       backupCount=5)
        fmt = '%(asctime)s - %(levelname)s - %(message)s'
        formatter = logging.Formatter(fmt, None)
        handler.setFormatter(formatter)
        try:
            if logging.root.hasHandlers():
                for h in logging.root.handlers:
                    logging.root.removeHandler(h)
        except Exception:
            pass
        logging.root.addHandler(handler)
        levels = {'CRITICAL':logging.CRITICAL, 'ERROR':logging.ERROR,
                  'WARNING':logging.WARNING, 'INFO':logging.INFO,
                  'DEBUG':logging.DEBUG}
        level = os.getenv("NEXPY_LOG")
        if level is None or level.upper() not in levels:
            level = 'INFO'
        else:
            level = level.upper()
        logging.root.setLevel(levels[level])
        logging.info('NeXpy launched')
        logging.info('Log level is ' + level)
        logging.info('Python ' + sys.version.split()[0] + ': ' + sys.executable)
        logging.info('IPython v' + ipython_version)
        logging.info('Matplotlib v' + mpl_version)
        logging.info('NeXpy v' + nexpy_version)
        logging.info('nexusformat v' + nxversion)
        sys.stdout = sys.stderr = NXLogger()

    def init_tree(self):
        """Initialize the NeXus tree used in the tree view."""
        global _tree
        self.tree = NXtree()
        _tree = self.tree

    def init_config(self):
        self.config.ConsoleWidget.input_sep = ''
        self.config.Completer.use_jedi = False

    def init_gui(self):
        """Initialize the GUI."""
        self.app = QtWidgets.QApplication.instance()
        if self.app is None:
            self.app = QtWidgets.QApplication(['nexpy'])
        self.app.setApplicationName('nexpy')
        sys.excepthook = report_exception
        try:
            if 'svg' in QtGui.QImageReader.supportedImageFormats():
                self.app.icon = QtGui.QIcon(
                    pkg_resources.resource_filename('nexpy.gui',
                                                    'resources/icon/NeXpy.svg'))
            else:
                self.app.icon = QtGui.QIcon(
                    pkg_resources.resource_filename('nexpy.gui',
                                                    'resources/icon/NeXpy.png'))
            QtWidgets.QApplication.setWindowIcon(self.app.icon)
            self.icon_pixmap = QtGui.QPixmap(
                self.app.icon.pixmap(QtCore.QSize(64,64)))
        except Exception:
            self.icon_pixmap = None
        self.app.setStyleSheet("""QToolTip {color:darkblue;
                                            background-color:beige}""");
        self.window = MainWindow(self, self.tree, self.settings, self.config)
        self.window.log = self.log
        self.gc = NXGarbageCollector(self.window)
        global _mainwindow
        _mainwindow = self.window

    def init_shell(self, args):
        """Initialize imports in the shell."""
        global _shell
        _shell = self.window.user_ns
        s = ("import nexusformat.nexus as nx\n"
             "from nexusformat.nexus import NXgroup, NXfield, NXattr, NXlink\n"
             "from nexusformat.nexus import *\n"
             "import nexpy\n"
             "from nexpy.gui.plotview import NXPlotView")
        exec(s, self.window.user_ns)

        s = ""
        for _class in nxclasses:
            s = "%s=nx.%s\n" % (_class,_class) + s
        exec(s, self.window.user_ns)

        config_file = os.path.join(self.nexpy_dir, 'config.py')
        if not os.path.exists(config_file):
            s = ["import sys\n",
                 "import os\n",
                 "import h5py as h5\n",
                 "import numpy as np\n",
                 "import numpy.ma as ma\n",
                 "import scipy as sp\n",
                 "import matplotlib as mpl\n",
                 "mpl.use('{}')\n".format(QtVersion),
                 "from matplotlib import pylab, mlab, pyplot\n",
                 "plt = pyplot\n",
                 "os.chdir(os.path.expanduser('~'))\n"]
            with open(config_file, 'w') as f:
                f.writelines(s)
        else:
            with open(config_file) as f:
                s = f.readlines()
        exec('\n'.join(s), self.window.user_ns)
        self.window.read_session()
        for i, filename in enumerate(args.filenames):
            try:
                fname = os.path.expanduser(filename)
                self.window.load_file(fname)
            except Exception:
                pass
        if args.restore:
            self.window.restore_session()

    def init_colors(self):
        """Configure the coloring of the widget"""
        self.window.console.set_default_style()

    def init_signal(self):
        """allow clean shutdown on sigint"""
        signal.signal(signal.SIGINT, lambda sig, frame: self.exit(-2))
        # need a timer, so that QApplication doesn't block until a real
        # Qt event fires (can require mouse movement)
        # timer trick from http://stackoverflow.com/q/4938723/938949
        timer = QtCore.QTimer()
        # Let the interpreter run each 200 ms:
        timer.timeout.connect(lambda: None)
        timer.start(200)
        # hold onto ref, so the timer doesn't get cleaned up
        self._sigint_timer = timer

    @catch_config_error
    def initialize(self, args, extra_args):
        if args.faulthandler:
            import faulthandler
            faulthandler.enable(all_threads=False)
        super(NXConsoleApp, self).initialize(extra_args)
        self.init_dir()
        self.init_settings()
        self.init_log()
        self.init_tree()
        self.init_config()
        self.init_gui()
        self.init_shell(args)
        self.init_colors()
        self.init_signal()

    def start(self):
        super(NXConsoleApp, self).start()

        # draw the window
        self.window.show()

        # Start the application main loop.
        self.app.exec_()

#-----------------------------------------------------------------------------
# Main entry point
#-----------------------------------------------------------------------------

def main(args, extra_args):
    app = NXConsoleApp()
    app.initialize(args, extra_args)
    app.start()
    sys.exit(0)

if __name__ == '__main__':
    main()

ext: py

max_stars_repo_name: nexpy/nexpy

max_issues_repo_name: nexpy/nexpy

max_forks_repo_name: nexpy/nexpy

reason: import numpy,import scipy


text:
"""
Convert LabeledVideoDataset to LabeledImageDataset

Copyright 2017-2019 Voxel51, Inc.
voxel51.com

Matthew Lightman, matthew@voxel51.com
Jason Corso, jason@voxel51.com
Ben Kane, ben@voxel51.com
Tyler Ganter, tyler@voxel51.com
"""
# pragma pylint: disable=redefined-builtin
# pragma pylint: disable=unused-wildcard-import
# pragma pylint: disable=wildcard-import
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import *

# pragma pylint: enable=redefined-builtin
# pragma pylint: enable=unused-wildcard-import
# pragma pylint: enable=wildcard-import

import logging
import os

import numpy as np

import eta.core.image as etai

from .labeled_datasets import LabeledImageDataset

logger = logging.getLogger(__name__)

# Functions involving LabeledDatasets

def sample_videos_to_images(
    video_dataset,
    image_dataset_path,
    stride=None,
    num_images=None,
    frame_filter=lambda labels: True,
    image_extension=".jpg",
    description=None,
):
    """Creates a `LabeledImageDataset` by extracting frames and their
    corresponding labels from a `LabeledVideoDataset`.

    Args:
        video_dataset: a `LabeledVideoDataset` instance from which to
            extract frames as images
        image_dataset_path: the path to the `manifest.json` file for
            the image dataset that will be written. The containing
            directory must either not exist or be empty
        stride: optional frequency with which to sample frames from
            videos
        num_images: optional total number of frames to sample from
            videos. If `stride` is not specified then it will be
            calculated based on `num_images`. If `stride` is
            specified, frames will be sampled at this stride until
            a total of `num_images` are obtained.
        frame_filter: function that takes an
            `eta.core.video.VideoFrameLabels` instance as input and
            returns False if the frame should not be included in the
            sample
        image_extension: optional extension for image files in new
            dataset (defaults to ".jpg")
        description: optional description for the manifest of the
            new image dataset

    Returns:
        image_dataset: `LabeledImageDataset` instance that points to
            the new image dataset
    """
    if stride is None and num_images is None:
        stride = 1

    _validate_stride_and_num_images(stride, num_images)

    if num_images is not None and stride is None:
        stride = _compute_stride(video_dataset, num_images, frame_filter)

    logger.info("Sampling video frames with stride %d", stride)

    image_dataset = LabeledImageDataset.create_empty_dataset(
        image_dataset_path, description=description
    )

    frame_iterator = _iter_filtered_video_frames(
        video_dataset, frame_filter, stride
    )
    for img_number, (frame_img, frame_labels, base_filename) in enumerate(
        frame_iterator, 1
    ):
        image_filename = "%s%s" % (base_filename, image_extension)
        labels_filename = "%s.json" % base_filename

        image_labels = etai.ImageLabels(
            filename=image_filename,
            attrs=frame_labels.attrs,
            objects=frame_labels.objects,
        )
        image_dataset.add_data(
            frame_img, image_labels, image_filename, labels_filename
        )

        if num_images is not None and img_number >= num_images:
            break

    if not image_dataset:
        logger.info(
            "All frames were filtered out in sample_videos_to_images(). "
            "Writing an empty image dataset to '%s'.",
            image_dataset_path,
        )

    image_dataset.write_manifest(image_dataset_path)

    return image_dataset

def _validate_stride_and_num_images(stride, num_images):
    if stride is not None and stride < 1:
        raise ValueError("stride must be >= 1, but got %d" % stride)

    if num_images is not None and num_images < 1:
        raise ValueError("num_images must be >= 1, but got %d" % num_images)

def _compute_stride(video_dataset, num_images, frame_filter):
    total_frames_retained = 0
    for video_labels in video_dataset.iter_labels():
        for frame_number in video_labels:
            frame_labels = video_labels[frame_number]
            if frame_filter(frame_labels):
                total_frames_retained += 1

    logger.info(
        "Found %d total frames after applying the filter",
        total_frames_retained,
    )

    # Handle corner cases
    if total_frames_retained < 2:
        return 1
    if num_images < 2:
        return total_frames_retained

    return _compute_stride_from_total_frames(total_frames_retained, num_images)

def _compute_stride_from_total_frames(total_frames, num_desired):
    if num_desired == 1:
        return total_frames

    stride_guess = (total_frames - 1) / (num_desired - 1)
    stride_guess = max(stride_guess, 1)
    stride_int_guesses = [np.floor(stride_guess), np.ceil(stride_guess)]
    actual_num_images = [
        total_frames / stride for stride in stride_int_guesses
    ]
    differences = [
        np.abs(actual - num_desired) for actual in actual_num_images
    ]
    return int(
        min(zip(stride_int_guesses, differences), key=lambda t: t[1])[0]
    )

def _iter_filtered_video_frames(video_dataset, frame_filter, stride):
    filtered_frame_index = -1
    for video_reader, video_path, video_labels in zip(
        video_dataset.iter_data(),
        video_dataset.iter_data_paths(),
        video_dataset.iter_labels(),
    ):
        video_filename = os.path.basename(video_path)
        video_name = os.path.splitext(video_filename)[0]
        with video_reader:
            for frame_img in video_reader:
                frame_num = video_reader.frame_number
                base_filename = "%s-%d" % (video_name, frame_num)

                frame_labels = video_labels[frame_num]
                if not frame_filter(frame_labels):
                    continue
                filtered_frame_index += 1

                if filtered_frame_index % stride:
                    continue

                yield frame_img, frame_labels, base_filename

max_stars_repo_name: ErfanTagh/eta

max_issues_repo_name: ErfanTagh/eta

max_forks_repo_name: ErfanTagh/eta

reason: import numpy


text:
import transbigdata as tbd
import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon

class TestVisualization:
    def setup_method(self):
        self.data = pd.DataFrame([[34745, '20:27:43', 113.80684699999999, 22.623248999999998, 1, 27],
                                  [34745, '20:24:07', 113.809898, 22.627399, 0, 0],
                                  [34745, '20:24:27', 113.809898, 22.627399, 1, 0],
                                  [34745, '20:22:07', 113.811348, 22.628067, 1, 0],
                                  [34745, '20:10:06', 113.81988500000001,
                                      22.6478, 1, 54],
                                  [34745, '19:59:48', 113.820213,
                                      22.674967000000002, 0, 23],
                                  [34745, '20:11:06', 113.82048, 22.6423, 0, 57],
                                  [34745, '20:13:46', 113.82676699999999,
                                      22.630899, 0, 66],
                                  [34745, '19:43:18', 114.828217, 22.7069, 0, 62],
                                  [34745, '19:42:18', 113.83161899999999,
                                      22.716998999999998, 0, 69],
                                  [22233, '14:41:40', 113.878571, 22.571199, 0, 0],
                                  [22233, '14:42:00', 113.879135, 22.571617, 0, 7],
                                  [22233, '14:45:40', 113.886253, 22.573217, 0, 25],
                                  [22233, '14:49:00', 113.8899,
                                      22.56956700000001, 0, 0],
                                  [22233, '14:55:15', 113.91093400000001,
                                      22.552383, 1, 60],
                                  [22233, '19:02:12', 113.927116,
                                      22.543948999999998, 1, 41],
                                  [22233, '14:32:47', 113.92831399999999,
                                      22.556867999999998, 1, 57],
                                  [22233, '14:57:35', 113.934036, 22.555267, 1, 65],
                                  [22233, '20:54:11', 113.942467, 22.507566, 0, 21],
                                  [22233, '18:51:30', 113.964569, 22.541849, 0, 0]], columns=['VehicleNum','Time','Lng','Lat','OpenStatus','Speed'])

    def test_visualization(self):
        data = self.data
        data['count']=1
        vmap = tbd.visualization_data(data,col = ['Lng','Lat'],accuracy=1000,height = 500)
        vmap = tbd.visualization_data(data,col = ['Lng','Lat','count'],accuracy=1000,height = 500)
        vmap = tbd.visualization_trip(data,col = ['Lng', 'Lat', 'VehicleNum', 'Time'])
        oddata = tbd.taxigps_to_od(data, col=['VehicleNum', 'Time', 'Lng', 'Lat', 'OpenStatus'])
        vmap = tbd.visualization_od(oddata,col=['slon', 'slat', 'elon', 'elat'])
        oddata['count'] = 1
        vmap = tbd.visualization_od(oddata,col=['slon', 'slat', 'elon', 'elat','count'])
        assert len(tbd.taxigps_traj_point(data,oddata,col=['VehicleNum', 'Time', 'Lng', 'Lat', 'OpenStatus'])[0])==5

max_issues_repo_name: anitagraser/transbigdata

max_forks_repo_name: anitagraser/transbigdata


text:
import numpy as np

import sys, os

parent_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_path+'/../mountainsort/packages/pyms')

from basic.p_compute_templates import compute_templates_helper
from basic.p_extract_clips import extract_clips_helper

from mlpy import readmda, writemda64, DiskReadMda

processor_name='pyms.apply_label_map'
processor_version='0.12'

def apply_label_map(*, firings, label_map, firings_out):
    """
    Apply a label map to a given firings, including masking and merging

    Parameters
    ----------
    firings : INPUT
        Path of input firings mda file
    label_map : INPUT
        Path of input label map mda file [base 1, mapping to zero removes from firings]
    firings_out : OUTPUT
        ...
    """
    firings =readmda(firings)
    label_map = readmda(label_map)
    label_map = np.reshape(label_map, (-1,2))
    label_map = label_map[np.argsort(label_map[:,0])] # Assure input is sorted

    #Propagate merge pairs to lowest label number
    for idx, label in enumerate(label_map[:,1]):
    	# jfm changed on 12/8/17 because isin() is not isin() older versions of numpy. :)
        #label_map[np.isin(label_map[:,0],label),0] = label_map[idx,0] # Input should be sorted
        label_map[np.where(label_map[:,0]==label)[0],0] = label_map[idx,0] # Input should be sorted

    #Apply label map
    for label_pair in range(label_map.shape[0]):
    	# jfm changed on 12/8/17 because isin() is not isin() older versions of numpy. :)
        #firings[2, np.isin(firings[2, :], label_map[label_pair, 1])] = label_map[label_pair,0]
        firings[2, np.where(firings[2, :] == label_map[label_pair, 1])[0]] = label_map[label_pair,0]

    #Mask out all labels mapped to zero
    firings = firings[:, firings[2, :] != 0]

    #Write remapped firings
    return writemda64(firings, firings_out)

apply_label_map.name = processor_name
apply_label_map.version = processor_version
apply_label_map.author = 'J Chung and J Magland'

max_stars_repo_name: magland/pyms

max_issues_repo_name: magland/pyms

max_forks_repo_name: magland/pyms


text:
# %%
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import h5py
import pyqmc.api as pyq
eV = 27.2114

# %%
cell, mf = pyq.recover_pyscf('../scf/r1.4/h1_r1.4_k40_pbc_scf_vdz.chk')
nkpts = len(mf.kpts)
natom = 1
homo = np.sort(np.asarray(mf.mo_energy_kpts).reshape(-1))[nkpts*natom-1]
lumo = np.sort(np.asarray(mf.mo_energy_kpts).reshape(-1))[nkpts*natom]
ef = (homo+lumo)/2*eV

# %%
r = 1.4
natom = 1
k_unit = 2*np.pi/(natom*r)

projections = ['s','px_py']
fig, ax = plt.subplots(1,len(projections),figsize=(5*len(projections),4))

for ip, proj in enumerate(projections):
  path = f"../{proj}"
  with h5py.File(f"{path}/wannier_bands.chk",'r') as f_w,\
    h5py.File(f"../scf/r1.4/h1_r1.4_k40_pbc_bands_vdz.chk",'r') as f:
    ax[ip].plot(f_w['kpts'][:,-1], f_w['eigvals'][...]*eV, label = f'wannier interpolated band')
    ax[ip].plot(f['kpts'][:,-1]/k_unit, f['energy'][0,:]*eV, color = 'k')
    ax[ip].set_ylim([-20,80])
    # plot the Fermi level
  ax[ip].hlines(xmin = -0.5, xmax = 0.5, y = ef, color='r', linestyle='dashed')
  #ax[ip].legend(loc='lower left')
  ax[ip].set_xlim([-0.5,0.5])
  ax[ip].set_xlabel(r'$k/(\frac{2\pi}{r})$')
  ax[ip].set_title(f"trial projection = {proj}")

ax[0].set_ylabel(r'$E$/eV')
plt.savefig(f'wannier_bands.pdf',bbox_inches='tight')


text:
import pandas as pd
import numpy as np
import sys
import matplotlib.pyplot as plt
import math
import copy

import Mesh_node

class Node_IR(object):
    def __init__(self, node1, node2, path_loss = 1):
        self.ir = math.sqrt(node1.pt * node1.gain * node2.gain * node1.height**2\
								 * node2.height**2 / node2.CS_th) * path_loss


text:
"""Test Topography class"""
import numpy
import pytest

from bmi_topography import Topography

API_URL = "https://portal.opentopography.org/API/globaldem"

def test_data_url():
    r = Topography.data_url()
    assert r == API_URL

def test_invalid_dem_type():
    with pytest.raises(ValueError):
        Topography(dem_type="foo", output_format=Topography.DEFAULT["output_format"])

def test_invalid_output_format():
    with pytest.raises(ValueError):
        Topography(dem_type=Topography.DEFAULT["dem_type"], output_format="foo")

def test_valid_bbox():
    topo = Topography(
        dem_type=Topography.DEFAULT["dem_type"],
        output_format=Topography.DEFAULT["output_format"],
        south=Topography.DEFAULT["south"],
        west=Topography.DEFAULT["west"],
        north=Topography.DEFAULT["north"],
        east=Topography.DEFAULT["east"],
    )
    assert topo.bbox.south == Topography.DEFAULT["south"]
    assert topo.bbox.west == Topography.DEFAULT["west"]
    assert topo.bbox.north == Topography.DEFAULT["north"]
    assert topo.bbox.east == Topography.DEFAULT["east"]
    assert topo.bbox.south < topo.bbox.north
    assert topo.bbox.west < topo.bbox.east

@pytest.mark.parametrize("cache_dir", (".", "./cache"))
def test_cache_dir(tmpdir, cache_dir):
    with tmpdir.as_cwd():
        topo = Topography(
            dem_type=Topography.DEFAULT["dem_type"],
            output_format=Topography.DEFAULT["output_format"],
            south=Topography.DEFAULT["south"],
            west=Topography.DEFAULT["west"],
            north=Topography.DEFAULT["north"],
            east=Topography.DEFAULT["east"],
            cache_dir=cache_dir,
        )
        assert topo.cache_dir.is_absolute()
        assert list(topo.cache_dir.glob("*.tif")) == []

def test_cached_data(tmpdir, shared_datadir):
    with tmpdir.as_cwd():
        Topography(
            dem_type=Topography.DEFAULT["dem_type"],
            output_format=Topography.DEFAULT["output_format"],
            south=Topography.DEFAULT["south"],
            west=Topography.DEFAULT["west"],
            north=Topography.DEFAULT["north"],
            east=Topography.DEFAULT["east"],
            cache_dir=shared_datadir,
        )
        assert len(tmpdir.listdir(fil=lambda f: f.ext == ".tif")) == 0

def test_fetch(tmpdir):
    new_south = numpy.mean([Topography.DEFAULT["south"], Topography.DEFAULT["north"]])
    new_west = numpy.mean([Topography.DEFAULT["west"], Topography.DEFAULT["east"]])
    with tmpdir.as_cwd():
        topo = Topography(
            dem_type=Topography.DEFAULT["dem_type"],
            output_format=Topography.DEFAULT["output_format"],
            south=new_south,
            west=new_west,
            north=Topography.DEFAULT["north"],
            east=Topography.DEFAULT["east"],
            cache_dir=".",
        )
        topo.fetch()
        assert len(tmpdir.listdir(fil=lambda f: f.ext == ".tif")) == 1

def test_load(tmpdir, shared_datadir):
    with tmpdir.as_cwd():
        topo = Topography(
            dem_type=Topography.DEFAULT["dem_type"],
            output_format=Topography.DEFAULT["output_format"],
            south=Topography.DEFAULT["south"],
            west=Topography.DEFAULT["west"],
            north=Topography.DEFAULT["north"],
            east=Topography.DEFAULT["east"],
            cache_dir=shared_datadir,
        )
        topo.load()
        assert topo.da is not None

max_stars_repo_name: csdms/bmi-srtm

max_issues_repo_name: csdms/bmi-srtm

max_forks_repo_name: csdms/bmi-srtm


text:
import os
import torch.utils.data as data
import torch
import torch.optim as optim
import torch.nn as nn
from torch.optim import lr_scheduler
from torchvision import datasets, models, transforms
from PIL import Image
import time
import copy
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def default_loader(path):
    with open(path, 'rb') as f:
        with Image.open(f) as img:
            return img.convert('RGB')

class CustomImageLoader(data.Dataset): # 定义自己的数据类
    ##自定义类型数据输入
    def __init__(self, RootImageFolderPath, dataset = '', data_transforms=None, loader = default_loader):
        im_list = []
        im_labels = []

        if RootImageFolderPath[-1] != "/":
            RootImageFolderPath += "/"

        ImageFolderPath_list = os.listdir(RootImageFolderPath)

        for ImageFolderPath in ImageFolderPath_list:
            currentImageFolderPath = RootImageFolderPath + ImageFolderPath
            if os.path.isdir(currentImageFolderPath):
                ImagePath_list = os.listdir(currentImageFolderPath)
                for ImagePath in ImagePath_list:
                    if "train" in ImagePath:
                        continue
                    if ImagePath.split(".")[1] == "jpg":
                        currentImagePath = currentImageFolderPath + "/" + ImagePath
                        im_list.append(currentImagePath)
                        im_labels.append(int(ImageFolderPath))

        self.imgs = im_list
        self.labels = im_labels
        self.data_tranforms = data_transforms
        self.loader = loader
        self.dataset = dataset

    def __len__(self):
        return len(self.imgs)

    def __getitem__(self, item):
        img_name = self.imgs[item]
        label = self.labels[item]
        img = self.loader(img_name)

        if self.data_tranforms is not None:
            try:
                img = self.data_tranforms[self.dataset](img)
            except:
                print("Cannot transform image: {}".format(img_name))
        return img, label

data_tranforms={
    'Train':transforms.Compose([
        # transforms.RandomResizedCrop(224), # 随机裁剪为不同的大小和宽高比,缩放所为制定的大小
        transforms.RandomHorizontalFlip(0.5),
        transforms.RandomVerticalFlip(0.5),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225]) # 各通道颜色的均值和方差,用于归一化
    ]),
    'Test':transforms.Compose([
        # transforms.Resize(256), # 变换大小
        # transforms.CenterCrop(224), # 中心裁剪
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])
    ])
}

def train_model(model, crtiation, optimizer, schedular, num_epochs, batch_size, device):
    image_datasets = {x : CustomImageLoader(os.getcwd() + "/resnet-test/sand-pic/", # 默认目录为根目录，配搭文件中使用全路径
                                            data_transforms=data_tranforms,
                                            dataset=x) for x in ['Train', 'Test']
                    }

    dataloders = {x: torch.utils.data.DataLoader(image_datasets[x],
                                                    batch_size=batch_size,
                                                    shuffle=True) for x in ['Train', 'Test']}

    dataset_sizes = {x: len(image_datasets[x]) for x in ['Train', 'Test']} # 数据大小

    begin_time = time.time()
    best_weights = copy.deepcopy(model.state_dict())#copy the weights from the model
    best_acc = 0.0
    arr_acc = [] # 用于作图

    for epoch in range(num_epochs):
        print("-*-" * 20)
        item_acc = []
        for phase in ['Train', 'Test']:
            if phase=='Train':
                schedular.step()
                model.train()
            else:
                model.eval()
            running_loss = 0.0
            running_acc = 0.0

            for images, labels in dataloders[phase]:
                images.to(device)
                labels.to(device)
                optimizer.zero_grad()

                with torch.set_grad_enabled(phase=='Train'):
                    opt = model(images.cuda())
                    # opt = model(images)
                    _,pred = torch.max(opt,1)
                    labels = labels.cuda()
                    loss = crtiation(opt, labels)
                    if phase=='Train':
                        loss.backward()
                        optimizer.step()

                running_loss += loss.item()*images.size(0)
                running_acc += torch.sum(pred==labels)
            epoch_loss = running_loss/dataset_sizes[phase]
            epoch_acc = running_acc.double()/dataset_sizes[phase]
            print('epoch={}, Phase={}, Loss={:.4f}, ACC:{:.4f}'.format(epoch, phase,
                                                                       epoch_loss, epoch_acc))
            item_acc.append(epoch_acc)

            if phase == 'Test' and epoch_acc>best_acc:
                # Upgrade the weights
                best_acc=epoch_acc
                best_weights = copy.deepcopy(model.state_dict())
                torch.save(model.state_dict(), os.getcwd() + "/resnet-test/models/chLi-ResNet-best-model.pkl")
        arr_acc.append(item_acc)
        time_elapes = time.time() - begin_time
        time_elapes // 60, time_elapes % 60

    print('Best Val ACC: {:}'.format(best_acc))

    model.load_state_dict(best_weights) # 保存最好的参数
    return model,arr_acc

if __name__ == '__main__':
    device = torch.device('cuda:0') # 默认使用 GPU
    NUMCLASS = 7 # 类别数

    model_ft = models.resnet50(pretrained=False)
    num_fits = model_ft.fc.in_features
    model_ft.fc = nn.Linear(num_fits, NUMCLASS) # 替换最后一个全连接层
    model_ft = model_ft.to(device)
    model_ft.cuda()
    # try:
    #     model_ft.load_state_dict(torch.load(os.getcwd() + "/resnet-test/models/chLi-ResNet-best-model.pkl"))
    # except:
    #     print("load model failed.")
    criterion = nn.CrossEntropyLoss()
    optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=10, gamma=0.1)

    model_ft,arr_acc = train_model(model=model_ft, \
                                    crtiation=criterion, \
                                    optimizer=optimizer_ft, \
                                    schedular=exp_lr_scheduler, \
                                    num_epochs=1000, \
                                    batch_size=1, \
                                    device=device)

    # ll = np.array(arr_acc)
    # plt.plot(ll[:,0])
    # plt.plot(ll[:,1])

    # plt.show()


text:
"""
This is where the user interface is made using the streamlit package.
"""

import numpy as np
import pandas as pd
import streamlit as st
import pathlib

# Creating questions with multiple choice answer
RADIO_QUESTIONS_LIST = ['What is your cadmium source?',
                        'What is your carboxylic acid source?',
                        'What is your amine source?',
                        'What is your phosphine source?',
                        'What is your first solvent?',
                        'What is your second solvent?'
                        ]
# Creating multiple choice answers for each question above
RADIO_SELECTIONS = [['cadmium stearate', 'cadmium oxide', 'dimethylcadmium',
                     'cadmium acetate', 'cadmium acetate dihydrate'],
                    ['None', 'myrstic acid', 'oleic acid', 'stearic acid',
                     'benzoic acid', 'dodecylphosphonic acid',
                     'ethylphosphonic acid', 'lauric acid'],
                    ['None', '2-6-dimethylpyridine', 'aniline', 'benzylamine',
                     'dioctylamine/hexadecylamine', 'dodecylamine',
                     'heptylamine', 'hexadecylamine', 'octadecylamine',
                     'octylamine', 'oleylamine', 'pyridine', 'trioctylamine'],
                    ['None', 'diphenylphosphine', 'tributylphosphine',
                     'trioctylphosphine', 'triphenylphosphine'],
                    ['None', 'liquid parafin', 'octadecene',
                     'phenyl ether', 'trioctylphosphine oxide'],
                    ['None', 'phosphinic acid', 'trioctylphosphine oxide']
                    ]
# Creating questions with slider
SLIDER_QUESTIONS_LIST = ['How much Cadmium do you plan to use? (mmol)',
                         'Selenium power is used; how much Selenium do you plan to use? (mmol)',
                         'How much carboxylic acid  do you plan to use? (mmol)',
                         'How much amine do you plan to use? (mmol)',
                         'How much phosphine do you plan to use? (mmol)',
                         'How much first solvent do you plan to use? (g)',
                         'How much second solvent do you plan to use? (g)',
                         'What is the growth temperature? (Degree Celsius)',
                         'How long do you plan to grow the quantum dots (minute)?'
                         ]
# Creating sliders for each question above
SLIDER_SELECTIONS = [[0.1, 14.0, 0.15, 0.001],
                     [0.001, 1.0, 0.01, 0.0001],
                     [0.0, 60.0, 10.0, 0.001],
                     [0.0, 40.0, 1.0, 0.001],
                     [0.0, 60.0, 1.0, 0.001],
                     [0.0, 60.0, 10.0, 0.1],
                     [0.0, 60.0, 10.0, 0.1],
                     [45.0, 350.0, 200.0, 1.0],
                     [0.5, 360.0, 50.0, 0.5], ]

# Initiate lists for answers
radio_answers = []
slider_answers = []

def get_radio_input(question, selection):
    """
    This function creates questions and
    multiple choice answers in the user interface.
    """
    answer = st.radio(question, selection)
    return answer

def get_slider_input(question, mmin_val, max_val, default_val, interval):
    """
    This function creates questions and
    slider answers in the user interface.
    """
    answer = st.slider(question, mmin_val, max_val, default_val, interval)
    return answer

# Get DOI
DOI_FILE_NAME = 'doi_list.txt'
DEFAULT_DOI = '10.1000/xyz123\n'
#  check if file not exist
if not pathlib.Path(DOI_FILE_NAME).exists():
    with open(DOI_FILE_NAME, 'w') as fp:
        pass
with open(DOI_FILE_NAME, 'r+') as f:
    doi_lists = f.readlines()

current_doi = st.text_input(label='Type or paste a DOI name, e.g., 10.1000/xyz123, into the text box below',
                            value=DEFAULT_DOI,
                            help='Be sure to enter all of the '
              'characters before and after the slash. Do not include extra characters, or sentence punctuation '
              'marks.')
if current_doi != DEFAULT_DOI:
    current_doi += '\n'
st.write(doi_lists)
if current_doi in doi_lists and current_doi != DEFAULT_DOI:
    st.write(f'The paper with this DOI "{current_doi}" has already been submitted.')
# elif current_doi != DEFAULT_DOI:
with open(DOI_FILE_NAME, 'a') as f:
    f.write(current_doi)
    f.close()
# List of answers for multiple choice questions
for i in range(len(RADIO_QUESTIONS_LIST)):
    radio_answers.append(get_radio_input(
        RADIO_QUESTIONS_LIST[i], RADIO_SELECTIONS[i]))

# List of answers for slider questions
for i in range(len(SLIDER_QUESTIONS_LIST)):
    slider_answers.append(get_slider_input(
        SLIDER_QUESTIONS_LIST[i], SLIDER_SELECTIONS[i][0],
        SLIDER_SELECTIONS[i][1], SLIDER_SELECTIONS[i][2],
        SLIDER_SELECTIONS[i][3]))

# Rearange users' choice into a list to input to the ML model
user_input = [slider_answers[7], radio_answers[0], slider_answers[0], slider_answers[1],
              radio_answers[1], slider_answers[2], radio_answers[2], slider_answers[3],
              radio_answers[3], slider_answers[4], radio_answers[4], slider_answers[5],
              radio_answers[5], slider_answers[6], slider_answers[8]
              ]

# Naming each choice in the user input
user_df = pd.DataFrame(np.array(user_input).reshape(1, -1), columns=['Growth Temp (Celsius)',
                                                                     'Metal_source', 'Metal_mmol (mmol)',
                                                                     'Chalcogen_mmol (mmol)', 'Carboxylic_Acid',
                                                                     'CA_mmol (mmol)', 'Amines', 'Amines_mmol (mmol)',
                                                                     'Phosphines', 'Phosphines_mmol (mmol)',
                                                                     'Solvent I', 'S_I_amount (g)',
                                                                     'Solvent II', 'S_II_amount (g)', 'Time_min (min)'
                                                                     ])

# Print user inputs
st.write(user_df)

# Scaling and encoding user input using the raw dataset
FILE_NAME = 'InP_data.csv'
st.write('Click submit when you\'re done')
if st.button('Submit Data'):
    if pathlib.Path(FILE_NAME).exists():
        user_df.to_csv(FILE_NAME, mode='a', header=False)
    else:
        user_df.to_csv(FILE_NAME, mode='a', header=True)
    st.write('Data submitted!')

st.write(pd.read_csv(FILE_NAME))

max_stars_repo_name: akkaneror/Quantum-Dots-Synthesis

max_issues_repo_name: akkaneror/Quantum-Dots-Synthesis

max_forks_repo_name: akkaneror/Quantum-Dots-Synthesis


text:
import numpy as np
import seaborn as sns
import pandas
import mcmc_tools
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve
from sklearn.metrics import auc

# ファイルの読み込み
# PersonID: 学生のID
# A: アルバイトが好きかどうか0:好きでない、1:好き
# Score: 学問への興味を数値化したもの
# Weather: 天気(A:晴れ、B:曇り、C:雨)
# Y: 授業に出席したかどうか0:欠席、1:出席
attendance_3 = pandas.read_csv('data-attendance-3.txt')
print(attendance_3.head())
print(attendance_3.describe())

# 解析の目的
# A, Score, Weather を使って、応答変数Yをどのくらい予測できるか

# 2値、カテゴリカル変数が多い場合、散布図ではなく集計でデータを観察すると良い
print(pandas.crosstab(index=attendance_3['Weather'],columns=attendance_3['Y']))

# 練習問題5(3)
print(pandas.crosstab(index=attendance_3['A'],columns=attendance_3['Y']))

# 背景知識を使った変換を実施
# カテゴリカル変数を数値に置き換える際、各カテゴリの応答変数に関する影響度を背景知識として使う方法がある
# ここでは、A、B、Cをそれぞれ0、0.2、1の影響度をもつ変数として変換する。
# 変換用辞書
weather_effec = {
    'A': 0,
    'B': 0.2,
    'C': 1
}

attendance_3_re = attendance_3.replace(weather_effec)
print(attendance_3_re.head())

# 0、1の2値の出現確率を推定したい場合、ベルヌーイ分布を使う
# ここでは、A、Score、WeatherからYを推定する。A、Score、Weatherは出席確率としてロジスティック関数を使って推定し、
# 推定した値qを使って、Yをベルヌーイ分布に従うものとして計算する。
Score = attendance_3_re['Score']/200

Y = attendance_3_re['Y']
A = attendance_3_re['A']
Weather = attendance_3_re['Weather']
N = len(Y)

stan_data = {
    'N': N,
    'Score': Score,
    'A': A,
    'Y': Y,
    'Weather': Weather
}

# コンパイル
filename = '../model/model5-5'
mcmc_result = mcmc_tools.sampling(filename, stan_data, n_jobs=4)
mcmc_sample = mcmc_result.extract()

# 取得データの行列を確認
# print(mcmc_sample['q'].shape)
# print(mcmc_sample['q'].T.shape)

q_sample = mcmc_sample['q'].T

# 各qの中央値を取得
q_median = []
for i in range(N):
    q_median.append(np.median(q_sample[i]))

# サンプリングしたqと応答変数の関係を確認
# 書籍とは転地した状態の図を描くほうがPythonでは簡単
# Y0が欠席、Y1が出席なので、qが小さいほどY0、大きいほどY1に振れていれば良い推定といえる。
# 今回はデータが振れていないので良い推定とはいえない。
plot_df = pandas.DataFrame({
    'Y': Y,
    'A': A,
    'q': q_median
})
sns.violinplot(x='Y', y='q', hue='A', data=plot_df)
plt.show()

# ROC曲線を確認
# PythonでROC曲線をえがく場合はskleranが便利
# ROC曲線をプロット
fpr, tpr, thresholds = roc_curve(Y, q_median)

# AUC（ROC曲線の面積）を求めるのもsklearnが便利
auc = auc(fpr, tpr)

# 書籍では閾値を0.6としているがsklearnでは最適な閾値を求める
# 今回の分類器では、AUCが0.62なので、結果は良くない。
plt.plot(fpr, tpr, label='ROC curve (area = %.2f)'%auc)
plt.legend()
plt.title('ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.grid(True)
plt.show()


text:
import numpy as np

from pararealml import *
from pararealml.core.operators.fdm import *

diff_eq = ConvectionDiffusionEquation(2, [2., 1.])
mesh = Mesh([(0., 50.), (0., 50.)], [.5, .5])
bcs = [
    (NeumannBoundaryCondition(
        lambda x, t: np.zeros((len(x), 1)), is_static=True),
     NeumannBoundaryCondition(
         lambda x, t: np.zeros((len(x), 1)), is_static=True))
] * 2
cp = ConstrainedProblem(diff_eq, mesh, bcs)
ic = GaussianInitialCondition(
    cp,
    [
        (
            np.array([12.5, 12.5]),
            np.array([
                [10., 0.],
                [0., 10.]
            ])
        )
    ],
    [100.]
)
ivp = InitialValueProblem(cp, (0., 30.), ic)

solver = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .01)
solver.solve(ivp).plot('2d_convection_diffusion', n_images=20, three_d=True)

max_stars_repo_name: ViktorC/PararealML

max_issues_repo_name: ViktorC/PararealML

max_forks_repo_name: ViktorC/PararealML


text:
# -*- coding: utf-8 -*-
from PySide2.QtCore import Qt, QRect, QTimer, QCoreApplication, QMetaObject, Signal
from PySide2.QtGui import QFont, QIcon, QCloseEvent, QKeySequence
from PySide2.QtWidgets import *
import UI

import os,sys,json,traceback,re
from decimal import Decimal as dec
import numpy as np

#settings
ASK_ON_CLOSE     = True
COMBO_INIT_ORDER = (1,0,0)

#전역 불변 변수 정의
CURRENT_PATH = os.path.dirname(os.path.abspath('__file__'))+'\\'
PROGRAM_PATH = os.path.dirname(os.path.abspath(sys.argv[0]))+'\\'
ESCAPE_SEQ   = QKeySequence.fromString('Escape')
CUT_RATIO    = (4,11,23,40,60,77,89,96,100)

#전역 변수 정의
current_exam    = '111'
last_file       = ''
saved           = True
current_num     = {}
result_semester = {}
current_wrong   = {}
current_result  = {}
result_exam     = {}

for a in range(1,4):
    for b in range(1,3):
        result_semester[str(10*a+b)]=[{},{}]
        for c in range(1,3):
            result_exam[str(100*a+10*b+c)]=[{},{}]

'''전역함수'''
#점수 계산
def get_score(subject=None):
    def calc(subject):
        if subject in current_wrong.keys(): #과목 존재?
            wrong=current_wrong[subject]
            score,err_cnt=dec(100),0
            #객관식 오답 계산
            wrong_sel=tuple(wrong[0].values())
            if wrong_sel:
                err_cnt+=len(wrong_sel)
                for j in range(len(wrong_sel)):
                    score-=dec(wrong_sel[j][2])
            #서술형 오답 계산
            wrong_exp=tuple(wrong[1].values())
            if wrong_exp:
                err_cnt+=len(wrong_exp)
                for j in range(len(wrong_exp)):
                    score-=dec(wrong_exp[j][1])-dec(wrong_exp[j][0])
            return score,err_cnt
        else:
            return None
    if subject:
        return calc(subject)
    else:
        result=[]
        for subject in current_subjects:
            result.append(calc(subject))
        return result

def get_current_num(*,name=False,dictionary=False):
    exam=int(current_exam[2])
    var=name*4+dictionary*2+(exam==3)#*1
    if var==5: #true: name, exam==3
        return tuple(current_num.keys())
    elif var==4: #true: name
        name=[]
        for subject in current_num:
            if current_num[subject][exam]:
                name.append(subject)
        return name
    elif var==3: #true: dictionary, exam==3
        tmp={}
        for subject in current_num:
            tmp[subject]=current_num[subject][0]
        return tmp
    elif var==2: #true: dictonary
        tmp={}
        for subject in current_num:
            if current_num[subject][exam]:
                tmp[subject]=current_num[subject][0]
        return tmp
    elif var==1: #true: exam==3
        return tuple(value[0] for value in current_num.values())
    elif var==0: #true: - (default)
        tmp=[]
        for value in current_num.values():
            if value[exam]:
                tmp.append(value[0])
        return tmp
    else:
        base_err=f'\nArguments:\nname:{name}\ndictionary:{dictionary}\nexam:{exam}'
        if var==6 or var==7:
            QMessageBox.warning(None,'Error','Error:\nname & dictonary == true')
        elif var<0:
            QMessageBox.critical(None,'Error',f'Unknown Error: var({var})<0'+base_err)
        elif var>5:
            QMessageBox.critical(None,'Error',f'Unknown Error: var({var})>5'+base_err)
        else:
            QMessageBox.critical(None,'Error',f'Unknown Error: var type: {type(var)}\nvalue: {var}'+base_err)

def resize_all(window,*wid):
    app.processEvents()
    for wid in reversed(wid):
        wid.resize(wid.sizeHint().width(),wid.sizeHint().height())
    window.resize(window.sizeHint().width(),window.sizeHint().height())
    app.processEvents()

def resize_height(window,*wid):
    app.processEvents()
    for wid in reversed(wid):
        wid.resize(wid.width(),wid.sizeHint().height())
    window.resize(window.width(),window.sizeHint().height())
    app.processEvents()

def center_geometry(win_size):
    x,y=screen_size
    w=win_size.width()
    h=win_size.height()
    return QRect((x-w)//2,(y-h)//2,w,h)

#읽기
def load(name=None,*,parent=None):
    def loader():
        global result_semester,result_exam,saved,last_file
        if name:
            try:
                global current_num,current_wrong,current_result
                with open(name,'r',encoding='utf-8') as file:
                    result_semester,result_exam=json.load(file)
                if current_exam[2]=='3': #학기말
                    current_num,current_result=result_semester[current_exam[:2]]
                else: #중간고사or기말고사
                    current_num=result_semester[current_exam[:2]][0]
                    current_wrong,current_result=result_exam[current_exam]
            except IOError as e:
                tmp=QMessageBox.critical(
                    parent,
                    'Load Error',
                    f'파일 읽기 오류 발생\nTraceBack:\n{e}\n재시도?',
                    QMessageBox.Retry|QMessageBox.Cancel
                )
                if tmp==QMessageBox.Retry:
                    load(name)
            except json.JSONDecodeError as e:
                QMessageBox.critical(parent,'Error','파일 형식 오류')
            except ValueError as e:
                QMessageBox.critical(parent,'Error','파일 내용 오류')
            else:
                saved=True
                last_file=name
            finally:
                pass
    if not name:
        name,_=QFileDialog.getOpenFileName(parent,'저장',CURRENT_PATH)
    if saved:
        loader()
    else:
        response=QMessageBox.question(
            parent,
            'Info',
            '저장 안됨\n저장?',
            QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel
        )
        if response==QMessageBox.Save:
            save()
            loader()
        elif response==QMessageBox.Discard:
            loader()

#쓰기
def save(last=False,*,name=None,parent=None):
    global result_semester,result_exam,last_file,saved
    if last:
        name=last_file
    if not name:
        name,_=QFileDialog.getSaveFileName(parent,'저장',CURRENT_PATH)
    if name:
        if current_exam[2]=='3': #학기말
            result_semester[current_exam[:2]]=[current_num,current_result]
        else: #중간고사or기말고사
            result_semester[current_exam[:2]][0]=current_num
            result_exam[current_exam]=[current_wrong,current_result]
        try:
            with open(name,'w',encoding='utf-8') as file:
                json.dump((result_semester,result_exam),file,indent=4,ensure_ascii=False)
        except IOError as e:
                response=QMessageBox.warning(
                    self,
                    'Save Error',
                    f'데이터 저장 중 오류 발생\nTraceBack:\n{e}\n재시도?',
                    QMessageBox.Retry|QMessageBox.Cancel
                )
                if response==QMessageBox.Retry:
                    save(name=name)
        else:
            last_file=name
            saved=True

'''창 클래스들'''

class DetailErr(QMessageBox):
    def __init__(self,parent,title,text,detail_text,icon=QMessageBox.Warning):
        super().__init__(parent)

        self.setWindowTitle(title)
        self.setText(text)
        self.setIcon(icon)

        if type(detail_text) is str:
            self.setDetailedText(detail_text)
        elif type(detail_text) is tuple:
            try:
                self.setDetailedText(''.join(traceback.format_exception(*detail_text)))
            except:
                err_win=DetailErr(
                    self,
                    'Error',
                    '타입 오류',
                    f'detail_text 타입 오류: {type(detail_text)}'
                )
        else:
            err_win=DetailErr(
                self,
                'Error',
                '타입 오류',
                f'detail_text 타입 오류: {type(detail_text)}'
            )

class Info(QMainWindow):
    def __init__(self,parent,title,info_text,display_qtinfo=False):
        self.__display_qtinfo=display_qtinfo

        super().__init__(parent)
        self.setupUI()

        self.retranslateUi(title,info_text)
        self.btnExit.clicked.connect(self.hide)

        if self.__display_qtinfo:
            self.btnQt.clicked.connect(lambda: QMessageBox.aboutQt(self))

    def setupUI(self):
        if not self.objectName():
            self.setObjectName(u"info")
        self.setFixedSize(400, 300)
        self.setWindowFlags(self.windowFlags()^Qt.WindowMinMaxButtonsHint)

        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName(u"centralwidget")

        self.glCentral = QGridLayout(self.centralwidget)
        self.glCentral.setObjectName(u"glCentral")

        self.pteInfo = QPlainTextEdit(self.centralwidget)
        self.pteInfo.setObjectName(u"pteInfo")
        self.pteInfo.setReadOnly(True)
        self.glCentral.addWidget(self.pteInfo, 0, 0, 1, 2)

        if self.__display_qtinfo:
            self.btnQt = QPushButton(self.centralwidget)
            self.btnQt.setObjectName(u"btnQt")
            self.glCentral.addWidget(self.btnQt, 1, 0, 1, 1, Qt.AlignLeft)

        self.btnExit = QPushButton(self.centralwidget)
        self.btnExit.setObjectName(u"btnExit")
        self.glCentral.addWidget(self.btnExit, 1, 1, 1, 1, Qt.AlignRight)

        self.setCentralWidget(self.centralwidget)

    def retranslateUi(self,title,info_text):
        self.setWindowTitle(QCoreApplication.translate("info", title, None))
        self.pteInfo.setPlainText(re.sub('\n +','\n',re.sub('\n{2,} *','\n\n',info_text)))
        self.btnExit.setText(QCoreApplication.translate("info", u"\ub2eb\uae30", None))
        if self.__display_qtinfo:
            self.btnQt.setText(QCoreApplication.translate("info", u"About Qt", None))

#Main Window class
class Main(QMainWindow,UI.Ui_Main):
    def __init__(self):
        def load_and_setexam(name=None):
            load(name)
            self.__set_exam(save=True)

        super().__init__()

        self.setupUI(self,COMBO_INIT_ORDER)
        load_and_setexam('save.json')

        #Define Variables
        self.__win=[]
        self.__opensource_notice_win = None
        self.__license_win           = None

        #Connect Slots (QComboBox)
        self.comboYear.currentIndexChanged.connect(self.__set_exam)
        self.comboSemester.currentIndexChanged.connect(self.__set_exam)
        self.comboExam.currentIndexChanged.connect(self.__set_exam)

        #Connect Slots (QButtons)
        self.btnSubject.clicked.connect(lambda: self.__show_window(Subject_In))
        self.btnGrading.clicked.connect(lambda: self.__win.append(Grading_Controller()))
        self.btnGradingRes.clicked.connect(lambda: self.__show_window(Grad_result))
        self.btnCalc.clicked.connect(lambda: self.__show_window(Grade_calc))
        self.btnInput.clicked.connect(lambda: self.__show_window(Exam_input))
        self.btnRes.clicked.connect(lambda: self.__show_window(Exam_result))
        #self.btnManage.clicked.connect()
        self.btnManage.setEnabled(False)

        #Connect Slots (QAction)
        self.save.triggered.connect(lambda: save(True))
        self.saveAs.triggered.connect(save)
        self.load.triggered.connect(load_and_setexam)
        self.exit.triggered.connect(self.close)
        self.cut_calc.triggered.connect(lambda: self.__show_window(Cut_calc))
        self.license.triggered.connect(self.__opensource_notice)
        self.about.triggered.connect(self.__license)

    def closeEvent(self,event):
        if ASK_ON_CLOSE and not saved:
            response=QMessageBox.question(
                self,
                'Info',
                '저장 안됨\n저장?',
                QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel
            )
            if response==QMessageBox.Save:
                save(True)
                event.accept()
                sys.exit(0)
            elif response==QMessageBox.Discard:
                event.accept()
                sys.exit(0)
            elif response==QMessageBox.Cancel:
                event.ignore()
        else:
            event.accept()
            sys.exit(0)

    #시험 설정
    def __set_exam(self,*,save=False):
        global current_num,current_wrong,current_result,\
               current_exam,current_subjects,current_subject_cnt

        def save_old(): #시험 결과 저장
            global result_semester,result_exam
            if current_exam[2]=='3':
                result_semester[current_exam[:2]]=[current_num,current_result]
            else:
                result_semester[current_exam[:2]][0]=current_num
                result_exam[current_exam]=[current_wrong,current_result]

        semester=''
        for wid in (self.comboYear,self.comboSemester,self.comboExam): #과목 불러오기
            semester+=str(wid.currentIndex()+1)

        #시험 결과 저장/불러오기
        if save:
            save_old()
        if semester[2]=='3': #학기말
            current_exam=semester
            current_num,current_result=result_semester[current_exam[:2]]
        else: #중간고사or기말고사
            current_exam=semester
            current_num=result_semester[current_exam[:2]][0]
            current_wrong,current_result=result_exam[current_exam]

        #버튼 상태 설정
        self.update_btn()
        #get current subject
        current_subjects=get_current_num(name=True)
        current_subject_cnt=len(current_subjects)

    #버튼 상태 업데이트
    def update_btn(self):
        if current_num:
            if current_exam[2]=='3':
                self.btnGrading.setEnabled(False)
                self.btnGradingRes.setEnabled(False)
            else:
                self.btnGrading.setEnabled(True)
                if current_wrong:
                    self.btnGradingRes.setEnabled(True)
                else:
                    self.btnGradingRes.setEnabled(False)
            self.btnCalc.setEnabled(True)
            self.btnInput.setEnabled(True)
            if current_result:
                self.btnRes.setEnabled(True)
            else:
                self.btnRes.setEnabled(False)
        else:
            self.btnGrading.setEnabled(False)
            self.btnGradingRes.setEnabled(False)
            self.btnCalc.setEnabled(False)
            self.btnInput.setEnabled(False)
            self.btnRes.setEnabled(False)

    def __show_window(self,window):
        self.__win.append(window(None))
        self.__win[-1].show()
        self.__win[-1].setGeometry(center_geometry(self.__win[-1].geometry()))

    def __opensource_notice(self):
        if not self.__opensource_notice_win:
            if os.path.isfile(PROGRAM_PATH+'NOTICE'):
                with open(PROGRAM_PATH+'NOTICE','r',encoding='utf-8') as file:
                    self.__opensource_notice_win=Info(None,'오픈 소스 라이선스',file.read(),True)
            else:
                self.__opensource_notice_win=Info(None,'오픈 소스 라이선스','Notice File is Missed',True)
        self.__opensource_notice_win.show()

    def __license(self):
        if not self.__license_win:
            if os.path.isfile(PROGRAM_PATH+'LICENSE'):
                with open(PROGRAM_PATH+'LICENSE','r',encoding='utf-8') as file:
                    self.__license_win=Info(None,'정보',file.read())
            else:
                self.__license_win=Info(None,'정보','License File is Missed')
        self.__license_win.show()

#과목 입력
class Subject_In(QMainWindow,UI.Ui_SubjectIn):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setupUI(self)

        self.__inbox=[]

        #기존 값 불러오기
        if current_num:
            for subject in current_num:
                self.__add_subject(subject,current_num[subject])

        self.btnAdd.clicked.connect(lambda: self.__add_subject())
        self.btnCancel.clicked.connect(self.deleteLater)
        self.btnSet.clicked.connect(self.__set_subject)

    def __add_subject(self,subject='',value=(0,True,True)):
        n=len(self.__inbox)
        wids=self.addWidgets(n,subject,value)
        wids[4].clicked.connect(lambda: self.__del_subject(n))
        self.__inbox.append(wids)
        QTimer.singleShot(100,lambda: resize_height(self,self.centralwidget,self.widCent))

    def __del_subject(self,n):
        for a in range(5):
            self.glMain.removeWidget(self.__inbox[n][a])
            self.__inbox[n][a].setParent(None)
        self.__inbox[n]=None

    def __set_subject(self):
        global current_num,saved,current_subjects,current_subject_cnt
        current_num={}; current_subjects=[]
        try:
            for k in range(len(self.__inbox)):
                if self.__inbox[k]:
                    current_num[self.__inbox[k][0].text()]=(
                        int(self.__inbox[k][1].text()),
                        self.__inbox[k][2].isChecked(),
                        self.__inbox[k][3].isChecked()
                    )
                    saved=False
        except ValueError:
            QMessageBox.critical(self,'ERROR','잘못된 값 입력')
        else:
            current_subjects=get_current_num(name=True)
            current_subject_cnt=len(current_subjects)
            self.deleteLater()
            main.update_btn()

#가채점 입력
class Grading_Controller:
    def __init__(self,parent=None):
        self.__parent=parent

        self.__grd1=Grading1(parent,current_subjects)
        self.__grd2=Grading2(parent)
        self.__grd31=Grading31(parent)
        self.__grd32=Grading32(parent)

        self.__grd1.stat_sig.connect(self.__sig_1)
        self.__grd2.stat_sig.connect(self.__sig_2)
        self.__grd31.stat_sig.connect(self.__sig_31)
        self.__grd32.stat_sig.connect(self.__sig_32)

        self.__grd1.show()

    def __sig_1(self,response):
        if response[0]==-1:
            self.__grd1.deleteLater()
            self.__grd2.deleteLater()
            self.__grd31.deleteLater()
            self.__grd32.deleteLater()
        elif response[0]==0:
            self.__subject=response[1]
            self.__grd2.show(response[1:])
        else:
            raise ValueError

    def __sig_2(self,response):
        if response[0]==-1:
            self.__grd1.show(response[1:])
        elif response[0]==0:
            self.__grd31.show(response[1:])
        elif response[0]==1:
            self.__grd32.show(response[1:])
        elif response[0]==2:
            self.__write_result(response[1:])
        else:
            raise ValueError

    def __sig_31(self,response):
        if response[0]==-1:
            self.__grd2.show(response[1:])
        elif response[0]==0:
            self.__grd32.show(response[1:])
        elif response[0]==1:
            self.__write_result(response[1:])
        else:
            raise ValueError

    def __sig_32(self,response):
        if response[0]==-2:
            self.__grd2.show(response[1:])
        elif response[0]==-1:
            self.__grd31.show(response[1:])
        elif response[0]==0:
            self.__write_result(response[1:])
        else:
            raise ValueError

    def __write_result(self,data):
        global current_wrong,saved
        def callback(btn):
            btnRole=msgbox.buttonRole(btn)
            if btnRole==QMessageBox.YesRole:
                detail_win=Grad_detail(None,data[0])
                detail_win.show()

        msgbox=QMessageBox(self.__parent)
        msgbox.setWindowTitle('Result')
        msgbox.addButton('닫기',QMessageBox.NoRole)
        msgbox.buttonClicked.connect(callback)

        if data[1] or data[2]:
            msgbox.addButton('상세보기',QMessageBox.YesRole)
            score=100
            for err1 in data[1].values():
                score=score-dec(err1[2])
            for err2 in data[2].values():
                score=score-dec(err2[1])+dec(err2[0])
            msgbox.setText(f'오답 수: {len(data[1])+len(data[2])}\n점수: {score}' )
        else:
            msgbox.setText('만점!\n점수: 100')

        current_wrong[self.__subject]=(data[1],data[2])
        saved=False

        msgbox.exec_()

class Grading1(QMainWindow,UI.Ui_Grading1):
    stat_sig=Signal(tuple)
    def __init__(self,parent=None,subjects=None):
        super().__init__(parent)
        self.setupUI(self)

        self.comboSubject.addItems(subjects)
        try:
            self.comboSubject.setCurrentIndex(0)
        except IndexError:
            QMessageBox.critical(self,title='ERROR',message='과목 입력 안됨')

        self.btnBack.clicked.connect(self.__priv)
        self.btnNext.clicked.connect(self.__next)

    def __priv(self):
        self.stat_sig.emit((-1,))

    def __next(self):
        self.hide()
        self.stat_sig.emit((0,self.comboSubject.currentText()))

class Grading2(QMainWindow,UI.Ui_Grading2):
    stat_sig=Signal(tuple)
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setupUI(self)

        self.__inputs=(
            (
                (self.lnAns11,self.lnAns12,self.lnAns13),
                (self.lnAns21,self.lnAns22,self.lnAns23)
            ),
            (
                (self.lnCor11,self.lnCor12,self.lnCor13),
                (self.lnCor21,self.lnCor22,self.lnCor23)
            )
        )

        self.btnBack.clicked.connect(self.__priv)
        self.btnRe.clicked.connect(self.__del_all)
        self.btnNext.clicked.connect(self.__next)

    def show(self,data):
        super().setVisible(True)

        self.__subject=data[0]

    def __del_all(self):
        for k in range(2):
            for j in range(2):
                for i in range(3):
                    self.__inputs[k][j][i].setText('')

    def __get_input(self): #입력값 불러오기, 오류 검사
        ans,cor=[],[]
        try:
            for j in range(2):
                for k in range(3):
                    #응답,정답 불러오기
                    a=self.__inputs[0][j][k].text().replace(' ','')
                    b=self.__inputs[1][j][k].text().replace(' ','')
                    #응답,정답 오류검사->저장
                    if len(a)!=len(b):
                        raise ValueError(f'Error with input({j},{k}):\nLength of answer({len(a)})\nis not equal length of correct({len(b)})')
                    if a and b:
                        a=list(a); b=list(b)
                        for x in range(len(a)):
                            a[x]=int(a[x])
                            b[x]=int(b[x])
                            if 0<a[x]<6:
                                if b[x]<1 or b[x]>5:
                                    raise ValueError(f'\nError with input({j},{k},{x}):\nanswer({a[x]}) and correct({b[x]})')
                            elif a[x]==6 or a[x]==7:
                                if b[x]!=0:
                                    raise ValueError(f'\nError with input({j},{k},{x}):\nanswer({a[x]}) and correct({b[x]})')
                            else:
                                raise ValueError(f'\nError with input({j},{k},{x}):\nanswer({a[x]}) and correct({b[x]})')
                        ans+=a; cor+=b
        except ValueError:
            msgbox=DetailErr(
                self,
                'ERROR',
                '값 입력 오류',
                sys.exc_info()
            )
            msgbox.exec_()
        else:
            if len(ans)==len(cor):
                return ans,cor
            else:
                QMessageBox.critical(self,'ERROR','개수 오류')

    def __priv(self): #이전 창으로 진행
        self.setVisible(False)
        self.stat_sig.emit((-1,parent))

    def __next(self): #다음 창으로 진행

        self.__err1={}
        self.__err2={}

        x=self.__get_input()
        if x:
            for k in range(len(x[0])):
                if x[0][k]<=5 and x[0][k]!=x[1][k]:
                    self.__err1[k+1]=(x[0][k],x[1][k])
                elif x[0][k]==7:
                    self.__err2[k+1]=None
            response=QMessageBox.question(
                self,
                "오답 확인",
                f"<오답 개수>\n선택형: {len(self.__err1)}\n서답형: {len(self.__err2)}\n채점 진행?"
            )
            if response==QMessageBox.Yes:
                self.setVisible(False)
                if self.__err1:
                    self.stat_sig.emit((0,self.__subject,self.__err1,self.__err2))
                elif self.__err2:
                    self.stat_sig.emit((1,self.__subject,self.__err1,self.__err2))
                else:
                    self.stat_sig.emit((2,self.__subject,self.__err1,self.__err2))

class Grading31(QMainWindow,UI.Ui_Grading31):
    stat_sig=Signal(tuple)
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setupUI(self)

        self.btnBack.clicked.connect(self.__priv)
        self.btnNext.clicked.connect(self.__next)

    def show(self,data):
        super().setVisible(True)
        self.__subject,self.__err1,self.__err2=data

        self.__err_num=tuple(self.__err1.keys())

        self.__score=[]
        for k,err in enumerate(self.__err1):
            self.__score.append(self.addWidgets(k,err))

        resize_height(self,self.centralwidget,self.widCent)

    def __priv(self):
        self.stat_sig.emit((-1,self.__subject))

    def __next(self):
        self.hide()
        try:
            for subject,widget in zip(self.__err_num,self.__score):
                self.__err1[subject]+=(widget.text(),)
            if self.__err2:
                self.stat_sig.emit((0,self.__subject,self.__err1,self.__err2))
            else:
                response=QMessageBox.question(self,'채점 완료?','채점 완료?')
                if response==QMessageBox.Yes:
                    self.setVisible(False)
                    self.stat_sig.emit((1,self.__subject,self.__err1,self.__err2))
        except ValueError:
            QMessageBox.critical(self,'Error','값 입력 오류')

class Grading32(QMainWindow,UI.Ui_Grading32):
    stat_sig=Signal(tuple)
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setupUI(self)

        self.btnBack.clicked.connect(self.__priv)
        self.btnNext.clicked.connect(self.__next)

    def show(self,data):
        super().setVisible(True)
        self.__subject,self.__err1,self.__err2=data

        self.__score=[]
        for k,err in enumerate(self.__err2):
            self.__score.append(self.addWidgets(k,err))

        resize_height(self,self.centralwidget,self.widCent)

    def __priv(self):
        self.setVisible(False)
        if self.__err1:
            self.stat_sig.emit((-2,self.__subject,self.__err1,self.__err2))
        else:
            self.stat_sig.emit((-1,self.__subject,self.__err1,self.__err2))

    def __next(self):
        try:
            for subject,wids in zip(self.__err2,self.__score):
                t1=wids[0].text()
                t2=wids[1].text()
                assert float(t1)<=float(t2)
                self.__err2[subject]=(t1,t2)
            response=QMessageBox.question(self,'채점 완료?','채점 완료?')
            if response==QMessageBox.Yes:
                self.setVisible(False)
                self.stat_sig.emit((0,self.__subject,self.__err1,self.__err2))
        except (ValueError, AssertionError):
            QMessageBox.critical(self,'Error','값 입력 오류')

class Grad_result(QMainWindow,UI.Ui_GradResult):
    def __init__(self,parent=None):
        def do_connect(signal,func,*args):
            signal.connect(lambda: func(*args))

        super().__init__(parent)
        self.setupUI(self)

        self.__parent=parent
        self.__subject_win=[]
        self.__chk=[]

        for k,subject in enumerate(current_subjects):
            if subject in current_wrong:
                score,err_cnt=get_score(subject)
                is_error=False
                if (score==100 and err_cnt!=0) or score>100 or score<0:
                    is_error=True
                    response=QMessageBox.warning(
                        self,
                        'ERROR',
                        'Data Error:\n점수=100, 오답수!=0',
                        QMessageBox.Ignore|QMessageBox.Cancel
                    )
                if not is_error or response==QMessageBox.Ignore:
                    chkDel,btn=self.addWidgets(k,subject,score,err_cnt)
                    if btn:
                        do_connect(btn.clicked,self.__show_detail,parent,subject)
                    self.__chk.append(chkDel)
            else:
                self.addWidgets(k,subject,None,None)
                self.__chk.append(None)

        resize_all(self,self.centralwidget,self.widCent)
        QShortcut(ESCAPE_SEQ,self).activated.connect(self.deleteLater)
        self.btnClose.clicked.connect(self.deleteLater)
        self.btnDel.clicked.connect(self.__del_res)

    def __del_res(self):
        global current_wrong,saved
        response=QMessageBox.question(self,'삭제?','복원 불가\n삭제?')
        if response==QMessageBox.Yes:
            saved=False
            for chk,subject in zip(self.__chk,current_subjects):
                if chk:
                    if chk.isChecked() and subject in current_wrong.keys():
                        del current_wrong[subject]
            self.deleteLater()
            if current_wrong:
                grad_win=Grad_result()
                grad_win.show()
            else:
                self.__parent.update_btn()

    def __show_detail(self,parent,subject):
        self.__subject_win.append(Grad_detail(None,subject))
        self.__subject_win[-1].show()

class Grad_detail(QMainWindow,UI.Ui_GradDetail):
    def __init__(self,parent,subject):
        if subject in current_subjects:
            super().__init__(parent)
            self.setupUI(self)
            wrongs=current_wrong[subject]
            try:
                if wrongs[0]:
                    self.addSel(wrongs[0])
                    self.widSel.resize(
                        self.widSel.width(),
                        self.widSel.sizeHint().height()
                    )
                if wrongs[1]:
                    self.addSupply(wrongs[1])
                    self.widSupply.resize(
                        self.widSupply.width(),
                        self.widSupply.sizeHint().height()
                    )
            except Exception as e:
                msgbox=DetailErr(
                    self,
                    'ERROR',
                    '오류 발생',
                    sys.exc_info(),
                    QMessageBox.Critical
                )
                msgbox.exec_()
                self.deleteLater()
            else:
                resize_height(self,self.centralwidget)

            QShortcut(ESCAPE_SEQ,self).activated.connect(self.deleteLater)
            self.btnClose.clicked.connect(self.deleteLater)
        else:
            QMessageBox.critical(self,'Error','없는 과목')
            self.deleteLater()

#(예상)등급계산
class Grade_calc(QMainWindow,UI.Ui_GradeCalc):
    def __init__(self,parent,grades=None):
        def cut_calc():
            self.__cut_calc.append(Cut_calc())
            self.__cut_calc[-1].show()

        super().__init__(parent)
        if current_subject_cnt==0:
            QMessageBox.critical(self,'Error','과목 입력 안됨')
        else:
            self.setupUI(self)

            self.__cut_calc=[]

            self.__input=[]
            for k,subject in enumerate(current_subjects):
                self.__input.append(self.addWidgets(k,subject,current_num[subject][0]))

            QShortcut(ESCAPE_SEQ,self).activated.connect(self.deleteLater)
            self.btnCutCalc.clicked.connect(cut_calc)
            self.btnClear.clicked.connect(self.__del_all)
            self.btnCalc.clicked.connect(self.__calc)
            self.btnClose.clicked.connect(self.deleteLater)

    def __calc(self):
        total_num   = 0
        best_grade  = 0
        worst_grade = 0
        try:
            for (grd_in1,grd_in2,chk),num in zip(self.__input,get_current_num()):
                if chk.isChecked():
                    grd1=int(grd_in1.text().replace(' ',''))
                    assert 0<grd1<9, f'grade must be 0<grade<9, but grade is {grd1}'
                    tmp=grd_in2.text().replace(' ','')
                    if tmp:
                        grd2=int(tmp)
                        assert 0<grd2<9, f'grade must be 0<grade<9, but grade is {grd2}'
                        best_grade +=min(grd1,grd2)*num
                        worst_grade+=max(grd1,grd2)*num
                    else:
                        best_grade +=grd1*num
                        worst_grade+=grd1*num
                    total_num+=num
            if not total_num:
                raise ValueError
        except (ValueError,AssertionError) as e:
            DetailErr(self,'Error','잘못된 입력',sys.exc_info())
        else:
            QMessageBox.information(
                self,
                '예상등급',
                f'최상: {(best_grade/total_num):.4}\n최하: {(worst_grade/total_num):.4}'
            )

    def __del_all(self):
        for grd_in1,grd_in2,_ in self.__input:
            grd_in1.setText('')
            grd_in2.setText('')

    #not used, but remain for future
    '''
    def __calc_full(self,grd1,grd2):
        #파싱
        if current_subject_cnt==len(grd1):
            res,tmp=[],[]
            res.append([grd1[0]])
            if grd2[0]:
                res.append([grd2[0]])
            for k in range(1,current_subject_cnt):
                if not grd1[k]:
                    break
                tmp=deepcopy(res)
                for j in range(len(res)):
                    res[j].append(grd1[k])
                if grd2[k]:
                    for j in range(len(tmp)):
                        tmp[j].append(grd2[k])
                    res+=tmp
            #등급 계산
            tmp=[]
            for a in res:
                x=0; k=0
                for subject in current_subjects:
                    x+=a[k]*current_num[subject][0]
                    k+=1
                tmp.append(round(x/sum(get_current_num()),4))
            return max(tmp),min(tmp)
        else:
            QMessageBox.critical(self,'Error','개수 오류')
            return None,None
    '''

#성적 입력
class Exam_input(QMainWindow,UI.Ui_ExamInput):
    def __init__(self,parent=None):
        global current_result
        def connect(k,btn):
            btn.clicked.connect(lambda: self.__del_subject(k))

        super().__init__(parent)
        self.__parent=parent
        if current_subjects:
            subjects=get_current_num(dictionary=True)
            self.setupUI(self,subjects.keys(),subjects.values())

            if current_result:
                for k,subject in enumerate(current_subjects):
                    if subject in current_result:
                        score,rank,person=current_result[subject]
                        self.ent[k][0].setText(score)
                        if rank and person:
                            self.ent[k][1].setText(str(rank))
                            self.ent[k][2].setText(str(person))

            for k,btn in enumerate(self.del_btn):
                connect(k,btn)
            self.btnClear.clicked.connect(lambda: self.__del_subject())
            self.btnCancel.clicked.connect(self.deleteLater)
            self.btnSet.clicked.connect(self.__get_input_and_close)

            #resize_height(self,self.centralwidget)
        else:
            QMessageBox.warning(self,'Error','과목 입력 안됨')

    def __del_subject(self,n=-1):
        assert type(n) is int, 'type of n must be int'
        assert -2<n<current_subject_cnt, f'must -1<n<{current_subject_cnt}, n={n}'
        if n<0:
            for lbScore,lbRank,lbPerson in self.ent:
                lbScore.setText('')
                lbRank.setText('')
                lbPerson.setText('')
        else:
            self.ent[n][0].setText('')
            self.ent[n][1].setText('')
            self.ent[n][2].setText('')

    def __get_input_and_close(self):
        global saved
        all_score_inputed = True
        all_inputed       = True
        results={}
        try:
            for ent,subject in zip(self.ent,current_subjects):
                score_in  = ent[0].text()
                rank_in   = ent[1].text()
                person_in = ent[2].text()

                if score_in:
                    if rank_in and person_in:
                        rank   = int(rank_in)
                        person = int(person_in)
                        results[subject] =(score_in,rank,person)
                    else:
                        all_inputed=False
                        results[subject]=(score_in,None,None)
                else:
                    all_score_inputed=False
        except ValueError:
            err_win=DetailErr(self,'Error','입력 값 오류',sys.exc_info())
            err_win.exec_()
        else:
            if not results:
                QMessageBox.warning(self,'진행?','모든 성적 미입력')
            else:
                if not all_score_inputed:
                    response=QMessageBox.question(self,'진행?','점수 미입력\n성적입력?')
                elif not all_inputed:
                    response=QMessageBox.question(self,'진행?','빈 칸 존재\n성적입력?')
                if all_inputed or response==QMessageBox.Yes:
                    global current_result
                    saved=False
                    current_result=results

                    self.deleteLater()
                    result_win=Exam_result()
                    result_win.show()

#시혐 결과
class Exam_result(QMainWindow,UI.Ui_ExamResult):
    def __init__(self,parent=None):
        super().__init__(parent)
        if current_result:
            inputed_subjects=list(current_result.keys())

            self.__grade_win = []
            self.__cut_win   = []

            nums     = []
            scores   = []
            ranks    = []
            rank_str = []
            grades   = []
            percents = []

            all_rank_inputed=True

            for k,subject in enumerate(current_subjects):
                if subject in inputed_subjects:
                    score,rank,person=current_result[subject]
                    nums.append(current_num[subject][0])
                    scores.append(score)
                    ranks.append(rank)
                    rank_str.append(f'{rank}/{person}')

                    if rank and person:
                        grade,percent=self.__get_grade(rank,person)
                        percents.append(round(percent,2))
                        grades.append(grade)
                    else:
                        all_rank_inputed=False
                        percents.append(None)
                        grades.append(None)
                else:
                    all_rank_inputed=False
                    nums.append(current_num[subject][0])
                    scores.append(None)
                    grades.append(None)
                    rank_str.append(None)
                    percents.append(None)

            if all_rank_inputed:
                total_grade=self.__get_total_grade(nums,grades)
            else:
                total_grade=0

            self.setupUI(self,current_subjects,nums,scores,grades,rank_str,percents,total_grade)

            QShortcut(ESCAPE_SEQ,self).activated.connect(self.deleteLater)
            self.btnCutCalc.clicked.connect(self.__cutcalc)
            self.btnGradeCalc.clicked.connect(self.__gradecalc)
            self.btnClose.clicked.connect(self.deleteLater)

        elif current_subjects:
            QMessageBox.critical(self,'Error','성적 입력 안됨')
        else:
            QMessageBox.critical(self,'Error','과목 입력 안됨')

    def __get_grade(self,rank,person):
        percent=rank/person*100
        cuts=(np.array(CUT_RATIO)*person*0.01).round()
        return np.searchsorted(cuts,rank)+1,percent

    def __get_total_grade(self,num,grd):
        array_num=np.array(num)
        array_grd=np.array(grd)
        result=(array_num*array_grd).sum()
        n=array_num.sum()
        return round(result/n,4)

    def __cutcalc(self):
        self.__cut_win.append(Cut_calc(None))
        self.__cut_win[-1].show()

    def __gradecalc(self):
        self.__grade_win.append(Grade_calc(None))
        self.__grade_win[-1].show()

#등급인원계산
class Cut_calc(QMainWindow,UI.Ui_CutCalc):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setupUI(self)

        for k,(ratio,lbGrade,lbRatio) in enumerate(zip(CUT_RATIO,self.lbGrades,self.lbRatios)):
            lbGrade.setText(str(k+1))
            lbRatio.setText(f'{ratio}%')

        QShortcut(ESCAPE_SEQ,self).activated.connect(self.deleteLater)
        QShortcut(QKeySequence.fromString('Enter'),self).activated.connect(self.__set_cuts)
        self.btnClose.clicked.connect(self.deleteLater)
        self.btnCalc.clicked.connect(self.__set_cuts)

    def __set_cuts(self):
        try:
            a=int(self.lnPersonCount.text())
        except ValueError:
            QMessageBox.critical(self,'Error','인원수 오류')
        else:
            for ratio,lbCut in zip(CUT_RATIO,self.lbCuts):
                lbCut.setText(str(round(a*ratio/100)))

if __name__=='__main__':
    import ctypes
    myappid = 'hys.grademanage2'
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

    app=QApplication()
    app.setWindowIcon(QIcon(PROGRAM_PATH+'icon.png'))

    main=Main()

    tmp=app.desktop().screenGeometry(main)
    screen_size=(tmp.width(),tmp.height())

    main.setGeometry(center_geometry(main))
    main.show()

    sys.exit(app.exec_())

ext: pyw