import torch
import torch.autograd as autograd
import torch.optim as optim

d = torch.load('fixed_point_test.pt')

nr_of_fixed_point_iterations = 100
learning_rate = 0.25
compute_simple_gradient_descent = False

inv_Kbar = d['inv_Kbar']
inv_Kbar_b = d['inv_Kbar_b']

nl = torch.tanh

def advect_q(q, theta1, bias1, theta2, bias2):
    """
    Forward equation which  is applied on the data. In principle similar to advect_q
    :param x:
    :param theta:
    :param bias:
    :return: \dot q_i = \theta \sigma(q_i) + b
    """
    temp_q_inner = torch.matmul(theta2, q) + bias2
    temp_q = nl(temp_q_inner)
    return torch.matmul(theta1, temp_q) + bias1

def compute_lagrangian(p, q, theta1, bias1, theta2, bias2):
    theta1_penalty = torch.mm(theta1.view(1, -1), torch.mm(inv_Kbar, theta1.view(-1, 1)))
    bias1_penality = torch.mm(bias1.t(), torch.mm(inv_Kbar_b, bias1))

    #theta2_penalty = torch.mm(theta2.view(1, -1), torch.mm(inv_Kbar, theta2.view(-1, 1)))

    #theta2_penalty = torch.norm((torch.mm(theta2, theta2.t()) - torch.eye(2)))

    theta2_penalty = ((torch.mm(theta2, theta2.t()) - torch.eye(2))**2).sum()

    #theta2_penalty = torch.mm((theta2-torch.eye(2)).view(1, -1), torch.mm(inv_Kbar, (theta2-torch.eye(2)).view(-1, 1)))

    bias2_penality = torch.mm(bias2.t(), torch.mm(inv_Kbar_b, bias2))

    kinetic_energy = 0.5 * (theta1_penalty + theta2_penalty + bias1_penality + bias2_penality)

    # this is really only how one propagates through the system given the parameterization
    potential_energy = torch.mean(p * advect_q(q, theta1, bias1, theta2, bias2))

    L = kinetic_energy - potential_energy

    return L

p = d['p']
q = d['q']
theta1 = d['theta1']
theta2 = d['theta2']
bias1 = d['bias1']
bias2 = d['bias2']

print(theta1)
print(theta2)
print(bias1)
print(bias2)

vars = [theta1, theta2, bias1, bias2]

#optimizer = optim.Adam(vars, lr=learning_rate)
optimizer = optim.SGD(vars, lr=learning_rate,momentum=0.9,nesterov=True)

for n in range(nr_of_fixed_point_iterations):

    optimizer.zero_grad()

    current_lagrangian = compute_lagrangian(p=p, q=q, theta1=theta1, bias1=bias1, theta2=theta2, bias2=bias2)

    print('{}: val = {}'.format(n,current_lagrangian.item()))

    theta_grad1, bias_grad1, theta_grad2, bias_grad2, = autograd.grad(current_lagrangian,
                                                                      (theta1, bias1, theta2, bias2),
                                                                      grad_outputs=current_lagrangian.data.new(
                                                                          current_lagrangian.shape).fill_(1),
                                                                      create_graph=True,
                                                                      retain_graph=True,
                                                                      allow_unused=True)

    if compute_simple_gradient_descent:

        theta1 = theta1 - learning_rate*theta_grad1
        bias1 = bias1 - learning_rate*bias_grad1

        theta2 = theta2 - learning_rate*theta_grad2
        bias2 = bias2 - learning_rate*bias_grad2

    else:

        theta1.grad = theta_grad1
        theta2.grad = theta_grad2
        bias1.grad = bias_grad1
        bias2.grad = bias_grad2

        optimizer.step()

    # print(theta)
    # print(bias)

print('theta1:')
print(theta1)
print('theta2:')
print(theta2)
print('bias1:')
print(bias1)
print('bias2:')
print(bias2)

# print('p:')
# print(p)
#
# print('q:')
# print(q)


import argparse
import enum
import json
import os
import sys
import traceback
import unittest
from types import TracebackType
from typing import Dict, List, Optional, Tuple, Type, TypeAlias, TypedDict

# Add the path to pythonFiles to sys.path to find testing_tools.socket_manager.
PYTHON_FILES = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, PYTHON_FILES)
# Add the lib path to sys.path to find the typing_extensions module.
sys.path.insert(0, os.path.join(PYTHON_FILES, "lib", "python"))
from testing_tools import socket_manager
from typing_extensions import NotRequired
from unittestadapter.utils import parse_unittest_args

DEFAULT_PORT = "45454"

def parse_execution_cli_args(args: List[str]) -> Tuple[int, str | None, List[str]]:
    """Parse command-line arguments that should be processed by the script.

    So far this includes the port number that it needs to connect to, the uuid passed by the TS side,
    and the list of test ids to report.
    The port is passed to the execution.py script when it is executed, and
    defaults to DEFAULT_PORT if it can't be parsed.
    The list of test ids is passed to the execution.py script when it is executed, and defaults to an empty list if it can't be parsed.
    The uuid should be passed to the execution.py script when it is executed, and defaults to None if it can't be parsed.
    If the arguments appear several times, the value returned by parse_cli_args will be the value of the last argument.
    """
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("--port", default=DEFAULT_PORT)
    arg_parser.add_argument("--uuid")
    arg_parser.add_argument("--testids", nargs="+")
    parsed_args, _ = arg_parser.parse_known_args(args)

    return (int(parsed_args.port), parsed_args.uuid, parsed_args.testids)

ErrorType = (
    Tuple[Type[BaseException], BaseException, TracebackType] | Tuple[None, None, None]
)

class TestOutcomeEnum(str, enum.Enum):
    error = "error"
    failure = "failure"
    success = "success"
    skipped = "skipped"
    expected_failure = "expected-failure"
    unexpected_success = "unexpected-success"
    subtest_success = "subtest-success"
    subtest_failure = "subtest-failure"

class UnittestTestResult(unittest.TextTestResult):
    formatted: Dict[str, Dict[str, str | None]] = dict()

    def startTest(self, test: unittest.TestCase):
        super(UnittestTestResult, self).startTest(test)

    def addError(
        self,
        test: unittest.TestCase,
        err: ErrorType,
    ):
        super(UnittestTestResult, self).addError(test, err)
        self.formatResult(test, TestOutcomeEnum.error, err)

    def addFailure(
        self,
        test: unittest.TestCase,
        err: ErrorType,
    ):
        super(UnittestTestResult, self).addFailure(test, err)
        self.formatResult(test, TestOutcomeEnum.failure, err)

    def addSuccess(self, test: unittest.TestCase):
        super(UnittestTestResult, self).addSuccess(test)
        self.formatResult(test, TestOutcomeEnum.success)

    def addSkip(self, test: unittest.TestCase, reason: str):
        super(UnittestTestResult, self).addSkip(test, reason)
        self.formatResult(test, TestOutcomeEnum.skipped)

    def addExpectedFailure(self, test: unittest.TestCase, err: ErrorType):
        super(UnittestTestResult, self).addExpectedFailure(test, err)
        self.formatResult(test, TestOutcomeEnum.expected_failure, err)

    def addUnexpectedSuccess(self, test: unittest.TestCase):
        super(UnittestTestResult, self).addUnexpectedSuccess(test)
        self.formatResult(test, TestOutcomeEnum.unexpected_success)

    def addSubTest(
        self, test: unittest.TestCase, subtest: unittest.TestCase, err: ErrorType | None
    ):
        super(UnittestTestResult, self).addSubTest(test, subtest, err)
        self.formatResult(
            test,
            TestOutcomeEnum.subtest_failure if err else TestOutcomeEnum.subtest_success,
            err,
            subtest,
        )

    def formatResult(
        self,
        test: unittest.TestCase,
        outcome: str,
        error: ErrorType | None = None,
        subtest: unittest.TestCase | None = None,
    ):
        tb = None
        if error and error[2] is not None:
            # Format traceback
            formatted = traceback.format_exception(*error)
            # Remove the 'Traceback (most recent call last)'
            formatted = formatted[1:]
            tb = "".join(formatted)

        test_id = test.id()

        result = {
            "test": test.id(),
            "outcome": outcome,
            "message": str(error),
            "traceback": tb,
            "subtest": subtest.id() if subtest else None,
        }

        self.formatted[test_id] = result

class TestExecutionStatus(str, enum.Enum):
    error = "error"
    success = "success"

TestResultTypeAlias: TypeAlias = Dict[str, Dict[str, str | None]]

class PayloadDict(TypedDict):
    cwd: str
    status: TestExecutionStatus
    result: NotRequired[TestResultTypeAlias]
    not_found: NotRequired[List[str]]
    error: NotRequired[str]

# Args: start_path path to a directory or a file, list of ids that may be empty.
# Edge cases:
# - if tests got deleted since the VS Code side last ran discovery and the current test run,
# return these test ids in the "not_found" entry, and the VS Code side can process them as "unknown";
# - if tests got added since the VS Code side last ran discovery and the current test run, ignore them.
def run_tests(
    start_dir: str,
    test_ids: List[str],
    pattern: str,
    top_level_dir: Optional[str],
    uuid: Optional[str],
) -> PayloadDict:
    cwd = os.path.abspath(start_dir)
    status = TestExecutionStatus.error
    error = None
    payload: PayloadDict = {"cwd": cwd, "status": status}

    try:
        # If it's a file, split path and file name.
        start_dir = cwd
        if cwd.endswith(".py"):
            start_dir = os.path.dirname(cwd)
            pattern = os.path.basename(cwd)

        # Discover tests at path with the file name as a pattern (if any).
        loader = unittest.TestLoader()

        args = {
            "start_dir": start_dir,
            "pattern": pattern,
            "top_level_dir": top_level_dir,
        }
        suite = loader.discover(start_dir, pattern, top_level_dir)

        # Run tests.
        runner = unittest.TextTestRunner(resultclass=UnittestTestResult)
        # lets try to tailer our own suite so we can figure out running only the ones we want
        loader = unittest.TestLoader()
        tailor: unittest.TestSuite = loader.loadTestsFromNames(test_ids)
        result: UnittestTestResult = runner.run(tailor)  # type: ignore

        payload["result"] = result.formatted

    except Exception:
        status = TestExecutionStatus.error
        error = traceback.format_exc()

    if error is not None:
        payload["error"] = error

    payload["status"] = status

    # print(f"payload: \n{json.dumps(payload, indent=4)}")

    return payload

if __name__ == "__main__":
    # Get unittest test execution arguments.
    argv = sys.argv[1:]
    index = argv.index("--udiscovery")

    start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])

    # Perform test execution.
    port, uuid, testids = parse_execution_cli_args(argv[:index])
    payload = run_tests(start_dir, testids, pattern, top_level_dir, uuid)

    # Build the request data (it has to be a POST request or the Node side will not process it), and send it.
    addr = ("localhost", port)
    with socket_manager.SocketManager(addr) as s:
        data = json.dumps(payload)
        request = f"""POST / HTTP/1.1
Host: localhost:{port}
Content-Length: {len(data)}
Content-Type: application/json
Request-uuid: {uuid}

{data}"""
        result = s.socket.sendall(request.encode("utf-8"))  # type: ignore


import requests
import pandas as pd

url = "https://hahow.in/group?filter=PUBLISHED"
url = "https://api.hahow.in/api/products/search?category=COURSE&filter=PUBLISHED&limit=24&page=0&sort=TRENDING"

headers = {"User-Agent":"Mozilla/5.0"}
r = requests.get(url, headers=headers)
datas_list = []
if r.status_code == 200:
    data = r.json()
    products = data["data"]["courseData"]["products"]
    for product in products:
        course_data = {
            "課程名稱":product["title"],
            "評價":product["averageRating"],
            "價格":product["price"],
            "購買人數":product["numSoldTickets"],
        }
        datas_list.append(course_data)
    df = pd.DataFrame(datas_list)
    df.to_excel("Hahow_course.xlsx", index=False, engine="openpyxl")
else:
    print("=== 網址錯誤 ===")


import usb1
import threading
import re

try:
	import queue
except ImportError:
	import Queue as queue

from octoprint.settings import settings
from octoprint.events import Events, eventManager

class FlashForgeError(Exception):
	def __init__(self, message, error=0):
		super(FlashForgeError, self).__init__(("{} ({})" if error else "{}").format(message, error))
		self.error = error

class FlashForge(object):
	BUFFER_SIZE = 512

	STATE_UNKNOWN = 0
	STATE_READY = 1
	STATE_BUILDING = 2
	STATE_SD_BUILDING = 3
	STATE_SD_PAUSED = 4
	STATE_HOMING = 5
	STATE_BUSY = 6
	STATE_WAIT_ON_TEMP = 7

	PRINTING_STATES = [STATE_BUILDING, STATE_SD_BUILDING, STATE_SD_PAUSED]

	regex_SDPrintProgress = re.compile(b"(?P<current>[0-9]+)/(?P<total>[0-9]+)")
	""" Regex matching SD print progress from M27. """
	regex_gcode = re.compile(b"^(N[0-9]+\s+)?(?P<gcode>[GM][0-9]+)(\s+(?P<payload>.+))?")
	""" Regex matching gcodes in write(). """
	regex_g1 = re.compile(
		b"G[01](?=.* X(?P<X>-?[0-9.]+))?(?=.* Y(?P<Y>-?[0-9.]+))?(?=.* Z(?P<Z>-?[0-9.]+))?(?=.* E(?P<E>-?[0-9.]+))?(?=.* F(?P<F>[0-9.]+))?")
	""" Regex matching move G1 commands for noG91 handling. """
	regex_M114position = re.compile(
		b"X:(?P<X>-?[0-9.]+) Y:(?P<Y>-?[0-9.]+) Z:(?P<Z>-?[0-9.]+) E0:(?P<E0>-?[0-9.]+)( E1:(?P<E1>-?[0-9.]+))?")
	""" Regex matching position values from M114 """

	def __init__(self, plugin, comm, usbcontext, portname, printer, read_timeout=10.0, write_timeout=10.0):
		import logging
		self._logger = logging.getLogger("octoprint.plugins.flashforge")
		self._logger.debug("__init__()")

		self._plugin = plugin
		self._comm = comm
		self._usbcontext = usbcontext
		self._handle = None
		self._portname = portname
		self._read_timeout = read_timeout
		self._write_timeout = write_timeout
		self._keep_alive_t = None
		self._keep_alive_enabled = False
		self._status_time = 0.0
		self._temp_time = 0.0
		self._temp_interval = 0.0
		self._autotemp_enabled = True
		self._is_autotemp = False
		self._incoming = queue.Queue()
		self._readlock = threading.Lock()
		self._writelock = threading.Lock()
		self._printerstate = self.STATE_UNKNOWN
		self._disconnect_event = False

		self._noG91 = False
		self._relative_pos = False
		self._pos = {"X": 0.0, "Y": 0.0, "Z": 0.0, "E0": 0.0, "E1": 0.0}
		self._extruder = "E0"

		self._usb_cmd_endpoint_in = 0
		self._usb_cmd_endpoint_out = 0
		self._usb_sd_endpoint_in = 0
		self._usb_sd_endpoint_out = 0

		for device in self._usbcontext.getDeviceIterator(skip_on_error=True):
			if printer["bus"] == device.getBusNumber() and printer["addr"] == device.getDeviceAddress():
				try:
					self._handle = device.open()
				except usb1.USBErrorAccess:
					eventManager().fire(Events.ERROR, {
						"error": "Found printer but there is a connection problem - check Terminal window for details.",
						"reason": "connection"})
					raise FlashForgeError("\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n\r\n"
										  "Unable to connect to FlashForge printer - permission error.\r\n\r\n"
										  "If you are using OctoPi/Linux add permission to access this device by editing file:\r\n /etc/udev/rules.d/99-octoprint.rules\r\n\r\n"
										  "and adding the line:\r\n"
										  "SUBSYSTEM==\"usb\", ATTR{{idVendor}}==\"{:04x}\", MODE=\"0666\"\r\n\r\n"
										  "You can do this as follows:\r\n"
										  "1) Connect to your OctoPi/Octoprint device using ssh\r\n"
										  "2) Type the following to open a text editor:\r\n"
										  "sudo nano /etc/udev/rules.d/99-octoprint.rules\r\n"
										  "3) Add the following line:\r\n"
										  "SUBSYSTEM==\"usb\", ATTR{{idVendor}}==\"{:04x}\", MODE=\"0666\"\r\n"
										  "4) Save the file and close the editor\r\n"
										  "5) Verify the file permissions are set to \"rw-r--r--\" by typing:\r\n"
										  "ls -al /etc/udev/rules.d/99-octoprint.rules\r\n"
										  "6) Reboot your system for the rule to take effect.\r\n\r\n"
										  "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n\r\n".format(printer["vid"], printer["vid"]))
				except usb1.USBError as usberror:
					raise FlashForgeError('Unable to connect to FlashForge printer - may already be in use', usberror)
				break

		if not self._handle:
			self._logger.debug("No FlashForge printer found")
			raise FlashForgeError('No FlashForge Printer found')

		try:
			self._handle.claimInterface(0)
		except usb1.USBError as usberror:
			raise FlashForgeError('Unable to connect to FlashForge printer - may already be in use', usberror)

		self._logger.debug("claimed USB interface")
		device = self._handle.getDevice()
		# look for an in and out endpoint pair:
		for configuration in device.iterConfigurations():
			for interface in configuration:
				for setting in interface:
					self._logger.debug(" setting number: 0x{:02x}, class: 0x{:02x}, subclass: 0x{:02x}, protocol: 0x{:02x}, #endpoints: {}".format(
						setting.getNumber(), setting.getClass(), setting.getSubClass(), setting.getProtocol(), setting.getNumEndpoints()))
					endpoint_in = 0
					endpoint_out = 0
					for endpoint in setting:
						self._logger.debug("  found endpoint type {} at address 0x{:02x}, max packet size {}".
							format(usb1.libusb1.libusb_transfer_type.get(endpoint.getAttributes()),
							endpoint.getAddress(),
							endpoint.getMaxPacketSize()))
						if usb1.libusb1.libusb_transfer_type.get(endpoint.getAttributes()) == 'LIBUSB_TRANSFER_TYPE_BULK':
							address = endpoint.getAddress()
							if address & usb1.ENDPOINT_IN:
								endpoint_in = address
							else:
								endpoint_out = address
							if endpoint_in and endpoint_out:
								# we have a pair of endpoints, assign them as needed
								# assume first pair is for commands, second for SD upload
								if not self._usb_cmd_endpoint_out:
									self._usb_cmd_endpoint_in = endpoint_in
									self._usb_cmd_endpoint_out = endpoint_out
									endpoint_in = endpoint_out = 0
								elif not self._usb_sd_endpoint_out:
									self._usb_sd_endpoint_in = endpoint_in
									self._usb_sd_endpoint_out = endpoint_out
									break

		# if we don't have endpoints for SD upload then use the regular ones
		if not self._usb_sd_endpoint_out:
			self._usb_sd_endpoint_in = self._usb_cmd_endpoint_in
			self._usb_sd_endpoint_out = self._usb_cmd_endpoint_out
		self._logger.debug(
			"  cmd_endpoint_out 0x{:02x}, cmd_endpoint_in 0x{:02x}".
			format(self._usb_cmd_endpoint_out, self._usb_cmd_endpoint_in))
		self._logger.debug(
			"  sd_endpoint_out 0x{:02x}, sd_endpoint_in 0x{:02x}".
			format(self._usb_sd_endpoint_out, self._usb_sd_endpoint_in))
		if not (self._usb_cmd_endpoint_in and self._usb_cmd_endpoint_out):
			self.close()
			raise FlashForgeError('Unable to find USB endpoints - turn on debug output and check octoprint.log')

		self._keep_alive_t = threading.Thread(target=self.keep_alive, name="FlashForge.Keep_Alive")
		self._keep_alive_t.daemon = True
		self.enable_keep_alive(True)
		self._keep_alive_t.start()
		self._plugin.on_connect(self)

	@property
	def timeout(self):
		"""Return timeout for reads. OctoPrint Serial Factory property"""

		self._logger.debug("timeout()")
		return self._read_timeout

	@timeout.setter
	def timeout(self, value):
		"""Set timeout for reads. OctoPrint Serial Factory property"""

		self._logger.debug("Setting read timeout to {}s".format(value))
		self._read_timeout = value

	@property
	def write_timeout(self):
		"""Return timeout for writes. OctoPrint Serial Factory property"""

		self._logger.debug("write_timeout()")
		return self._write_timeout

	@write_timeout.setter
	def write_timeout(self, value):
		"""Set timeout for writes. OctoPrint Serial Factory property"""

		self._logger.debug("Setting write timeout to {}s".format(value))
		self._write_timeout = value

	@property
	def port(self):
		"""port name. OctoPrint Serial Factory property"""
		return self._portname

	def _valid_command(self, command):
		""" Check if command is valid for FF (allow line numbers N for emergency shutdown M112)

		M117 seems to cause issues during SD printing, but has to get this far to allow DisplayLayerPlugin to work
		"""
		gcode = command.split(b' ', 1)[0]
		return (gcode[0] in b"GMN") and gcode not in [b"M117"]

	def keep_alive(self):
		"""Keep printer connection alive

		Some printers drop the connection if they don't receive something at least every 4s, so we will send M119 to
		get status every few seconds.
		Also use for auto-reporting temperature so we can pass temp to Octoprint when the print queue is blocked by
		printer waiting for heatup, etc otherwise OctoPrint will think the printer is not responding...
		"""
		exit_flag = threading.Event()
		self._status_time = 0.0
		self._temp_time = 0.0
		keep_alive = 0.5
		self._logger.debug("keep_alive() set to:{}".format(keep_alive))
		# do not queue commands if the connection is going away
		while self._handle and not self._disconnect_event:
			if self._keep_alive_enabled:
				# even though we have blocking on the write() routine we do not want to try to write while uploading to
				# SD etc since it can generate confusion when the upload completes and a keep alive goes through at the
				# same time
				self._temp_time += keep_alive
				if self._temp_interval and self._temp_time >= self._temp_interval:
					# do the fake auto reporting of temp OctoPrint
					self._is_autotemp = True
					self.write(b"M105")
					self._temp_time = 0.0
				self._status_time += keep_alive
				if self._status_time >= 2.0:
					# get status every 2s so printer gets something during long ops
					# Dremel 3D20 seems to require something at least every 2s - other FF printers seem to be able to wait up to 3.5s
					# may want to make this a setting
					self.write(b"M119")
					self._status_time = 0.0
			if exit_flag.wait(timeout=keep_alive):
				exit()
		self._logger.debug("keep_alive() exiting")

	def enable_keep_alive(self, enable):
		"""Disable keep alive if we are streaming to the printer - eg file upload
		Even though there is blocking on the read/write this helps prevent issues/simplifies
		debugging during file upload and triggering a print
		"""
		self._logger.debug("enable_keep_alive({})".format(enable))
		self._keep_alive_enabled = enable
		self._temp_time = 0.0
		self._status_time = 0.0

	def is_ready(self):
		"""Return true if the printer is idle"""

		self._logger.debug("is_ready()")
		return self._printerstate == self.STATE_READY

	def is_printing(self):
		"""Return true if the printer is in any printing state"""

		self._logger.debug("is_printing()")
		return self._printerstate in self.PRINTING_STATES

	def is_sd_printing(self):
		"""Return true if the printer is in any printing state"""

		self._logger.debug("is_sd_printing()")
		return self._printerstate in [self.STATE_SD_PAUSED, self.STATE_SD_BUILDING]

	def disable_autotemp(self):
		"""Disable simulated auto temp reporting if printer claims to do it"""
		self._autotemp_enabled = False

	def disable_G91(self, disable):
		"""Disable G91 support if prefs indicate printer does not support relative positioning"""
		self._noG91 = disable

	def write(self, data):
		"""Write commands to printer. OctoPrint Serial Factory method

		Formats the commands sent by OctoPrint to make them FlashForge friendly.
		"""

		self._logger.debug("write() called by thread {}".format(threading.currentThread().getName()))
		if not self._handle:
			# do not queue commands if the connection is going away
			return

		self._writelock.acquire()

		# save the length for return on success
		data_len = len(data)

		# strip carriage return, etc so we can terminate lines the FlashForge way
		data = data.strip(b" \r\n")

		# try to filter out garbage commands (we need to replace with something harmless)
		# do this here instead of octoprint.comm.protocol.gcode.sending hook so DisplayLayerProgress plugin will work
		if len(data) and not self._valid_command(data):
			self._logger.debug("filtering command {0}".format(data.decode()))
			data = b"M105"
		else:
			cmd = data.split(b' ', 1)
			payload = b"" if len(cmd) == 1 else cmd[1]
			gcode = cmd[0]

			# special handling for relative positioning support
			if gcode in [b"G0", b"G1"] and self._noG91 and self._relative_pos:
				# try to convert relative positioning to absolute
				self._logger.debug("G0/G1 with rel pos")
				match = FlashForge.regex_g1.search(data)
				if match:
					self._logger.debug("G1 {}".format(match.groupdict()))
					data = b"G1"
					for k, v in match.groupdict().items():
						if v != None:
							if k in ['X', 'Y', 'Z']:
								v = self._pos[k] + float(v)
								data += b" %s%06.4f" % (k.encode(), v)
							elif k == 'E':
								self._logger.debug("extruder {}, {}".format(self._extruder, self._pos[self._extruder]))
								v = self._pos[self._extruder] + float(v)
								data += b" %s%06.4f" % (k.encode(), v)
							else:
								v = int(v)
								data += b" %s%d" % (k.encode(), v)
			elif gcode == b"G90":
				self._relative_pos = False
			elif gcode == b"G91":
				self._relative_pos = True
				if self._noG91:
					data = b"G90"
			elif gcode == b"M23":
				# we started an SD print - make sure to set printer state
				self._status_time = 0.0
				data += b"\r\n~M119"
			elif gcode == b"M27":
				# make sure we have the current printer status before getting SD card progress, because SD card
				# progress reports printing when cancelled or finished...
				self._status_time = 0.0
				data = b"M119\r\n~M27"
			elif gcode == b"M105":
				self._temp_time = 0.0
			elif gcode == b"M108":
				self._extruder = "E1" if b"T1" in payload else "E0"
				self._logger.debug("select extruder {0}".format(self._extruder))
			elif gcode == b"M601":
				# make sure we have the current printer status as soon as we connect
				self._status_time = 0.0
				data += b"\r\n~M119"

		try:
			self._logger.debug("write() {0}".format(data.decode()))
			self._handle.bulkWrite(self._usb_cmd_endpoint_out, b"~%s\r\n" % data, int(self._write_timeout * 1000.0))
			self._writelock.release()
			return data_len
		except usb1.USBError as usberror:
			self._writelock.release()
			raise FlashForgeError('USB Error write()', usberror)

	def writeraw(self, data, command = True):
		"""Write raw data to printer.

		data: bytearray to send
		command: True to send g-code, False to send to upload SD card
		"""

		self._logger.debug("writeraw() called by thread {}".format(threading.currentThread().getName()))

		try:
			self._handle.bulkWrite(self._usb_cmd_endpoint_out if command else self._usb_sd_endpoint_out, data)
			return len(data)
		except usb1.USBError as usberror:
			raise FlashForgeError('USB Error writeraw()', usberror)

	def readline(self):
		"""Read line worth of response from printer. OctoPrint Serial Factory method

		Read response from the printer and store as a series of \r\n terminated lines
		OctoPrint reads response line by line..

		Returns:
			List of lines returned from the printer
		"""

		self._logger.debug("readline() called by thread {}".format(threading.currentThread().getName()))

		self._readlock.acquire()

		# return any line we have buffered
		if not self._incoming.empty():
			self._readlock.release()
			return self._incoming.get_nowait()

		# fetch some data
		data = self.readraw()
		# parse and buffer it
		data = self._parse_response(data)
		if b"CMD M601 " in data:
			# should also be getting a status response
			status = self.readraw()
			# parse and buffer it
			data += self._parse_response(status)

		self._readlock.release()
		# return the buffer
		return self._incoming.get_nowait()

	def _parse_response(self, data):
		"""Parse raw data from printer into lines and buffer them

		Manipulates printer response if necessary into something OctoPrint understands, breaks into lines and stores
		in a buffer for readline() method.
		"""
		if len(data):
			if b"CMD M27 " in data:
				# need to filter out bogus SD print progress from cancelled or paused prints
				if b"printing byte" in data:
					match = FlashForge.regex_SDPrintProgress.search(data)
					if match:
						try:
							current = int(match.group("current"))
							total = int(match.group("total"))
						except:
							pass
						else:
							# Note: there is an issue with .gx files indicating the current byte size is greater than the
							# total when the print is started
							if self._printerstate == self.STATE_READY and current >= total:
								# Ultra 3D: after completing print it still indicates SD card progress
								data = b"CMD M27 Received.\r\nDone printing file\r\nok\r\n"
							elif self._printerstate in [self.STATE_SD_PAUSED, self.STATE_SD_BUILDING] and \
								not self._comm.isSdFileSelected():
								# user manually started a print or we connected while one was running
								data = b"File opened: SD_printing.gcode Size: %d\r\nok\r\n" % total
							elif self._printerstate == self.STATE_SD_PAUSED:
								# when paused still printer indicates printing so change the response
								# TODO: there may be a proper way to signal this using "action"?
								data = b"CMD M27 Received.\r\nPrinting paused\r\nok\r\n"
								if self._comm.isSdPrinting():
									# this is for when we connect and the printer is printing but paused or the user
									# manually paused the print using the printer screen. doesn't seem to be a way to
									# tell OctoPrint the correct state so we do it the dirty way
									self._comm._changeState(self._comm.STATE_PAUSED)
							elif self._printerstate != self.STATE_SD_BUILDING:
								# after print is cancelled M27 always looks like its printing from sd card
								data = b"CMD M27 Received.\r\nNot SD printing\r\nok\r\n"

				elif not data.strip().endswith(b"ok"):
					# for Dremel 3D20 not responding correctly when not printing from SD card:
					if self._printerstate == self.STATE_READY:
						data = b"CMD M27 Received.\r\nDone printing file\r\nok\r\n"
					else:
						data += b"ok\r\n"

			elif b"CMD M105 " in data:
				if self._is_autotemp:
					# this was generated as an auto temp report by our keep alive so filter out the CMD and OK
					# so as not to confuse the OctoPrint buffer counter
					data = data.replace(b"CMD M105 Received.\r\n", b"")
					# TODO: add " W:?" to the string to indicate that the printer is waiting to get to temp if state
					#  indicates waiting on tool or bed. This should prevent OctoPrint from triggering timeouts?
					if not (b"CMD " in data and self._printerstate in [self.STATE_SD_BUILDING, self.STATE_SD_PAUSED]):
						# do not drop the "ok" if there is the response to another command in here and we are printing from SD?
						data = data.replace(b"\r\nok", b"")
				self._is_autotemp = False

			elif b"CMD M114 " in data:
				# looks like get current position returns A: and B: for extruders?
				data = data.replace(b" A:", b" E0:").replace(b" B:", b" E1:")
				match = FlashForge.regex_M114position.search(data)
				if match:
					for k, v in match.groupdict().items():
						if v != None:
							self._pos[k] = float(v)
					self._logger.debug("pos: {}".format(self._pos))

			elif b"CMD M115 " in data:
				# Try to make the firmware response more readable by OctoPrint
				data = data.replace(b"Firmware:", b"FIRMWARE_NAME: FlashForge VER:")

			elif b"CMD M119 " in data:
				# this was generated by us so do not return anything to OctoPrint
				oldstate = self._printerstate
				if b"MachineStatus: READY" in data:
					if b"MoveMode: READY" in data:
						self._printerstate = self.STATE_READY
					elif b"MoveMode: WAIT_ON_TOOL" in data or b"MoveMode: WAIT_ON_PLATFORM" in data:
						# printing directly and printer waiting for bed or extruder to heat up
						self._printerstate = self.STATE_WAIT_ON_TEMP
					elif b"MoveMode: HOMING" in data:
						# printing directly and printer waiting for bed or extruder to heat up
						self._printerstate = self.STATE_HOMING
					else:
						# moving or homing
						self._printerstate = self.STATE_BUSY
				elif b"MachineStatus: BUILDING_FROM_SD" in data:
					if b"MoveMode: PAUSED" in data:
						self._printerstate = self.STATE_SD_PAUSED
					else:
						self._printerstate = self.STATE_SD_BUILDING
				else:
					self._printerstate = self.STATE_BUSY
				# Remove M119 response (assuming it is the last part of the response, other command may be at the front)
				# Typically if something is prepended, it will be a move related command.
				data = data.split(b"CMD M119 ")[0]
				if len(data) and (self._printerstate == self.STATE_READY or self._printerstate == self.STATE_SD_PAUSED):
					# If the printer is still moving it will send the ok associated with the command later. If it has
					# completed the movement a separate ok is never sent so we add it here
					data += b"ok\r\n"

				if oldstate != self._printerstate:
					self._logger.debug("state changed from {} to {}".format(oldstate, self._printerstate))
					# force temp reporting if busy while direct printing and waiting for extruder/bed to heat up
					# (unless printer reports autotemp) so OctoPrint sees something.
					# TODO: use OctoPrint state instead to decide when to do the temp check - ie if printing and temp wait
					if self._printerstate == self.STATE_WAIT_ON_TEMP and self._autotemp_enabled:
						self._temp_interval = settings().getFloat(["serial", "timeout", "temperatureAutoreport"])
					else:
						self._temp_interval = 0.0
					# TODO: if we just connected and the printer is printing from SD then trigger an M27 to get
					#		OctoPrint to detect SD printing

			if len(data):
				# turn data into list of lines
				datalines = data.splitlines()
				for i, line in enumerate(datalines):
					self._logger.debug("buffering: {}".format(line))
					self._incoming.put(line)
			else:
				self._incoming.put(data)

		else:
			self._incoming.put(data)

		return data

	def readraw(self, timeout=-1):
		"""
		Read everything available from the from the printer

		Parameters:
			timeout : max time to wait in ms

		Returns:
			String containing response from the printer
		"""

		data = b""
		if timeout == -1:
			timeout = int(self._read_timeout * 1000.0)
		self._logger.debug("readraw() called by thread: {}, timeout: {}".format(threading.currentThread().getName(), timeout))

		try:
			# read data from USB until ok signals end or timeout
			while not data.strip().endswith(b"ok"):
				data += self._handle.bulkRead(self._usb_cmd_endpoint_in, self.BUFFER_SIZE, timeout)
		except usb1.USBErrorTimeout:
			self._logger.debug("readraw() TIMEOUT")
		except usb1.USBError as usberror:
			raise FlashForgeError("USB Error readraw()", usberror)

		self._logger.debug("readraw() returns: {}".format(data.decode().replace("\r\n", " | ")))
		return data

	def sendcommand(self, cmd, timeout=1000, readresponse=True):
		"""
		Send g-code to printer and wait for a response

		Parameters:
			cmd : FF formatted g-code command
			timeout : max time to wait in ms
			readresponse : true to return printer response

		Returns:
			True if printer responded with ok, indicating command was accepted
			Optional : string containing response from the printer
		"""

		self._logger.debug("sendcommand() {}".format(cmd.decode()))

		self.writeraw(b"~%s\r\n" % cmd)
		if not readresponse:
			return True, None

		# read response, make sure we are getting the command we sent
		gcode = b"CMD %s " % cmd.split(b" ", 1)[0]
		response = b""
		while True:
			response = self.readraw(timeout)
			# TODO: if response is multiline and contains response to a previous command as well as this one then
			#  we lose the previous command. We might need to parse each line and save old responses into the buffer
			if not response or gcode in response:
				break
			# we got the response from some previous OctoPrint command so parse it into the buffer used for
			# OctoPrint listener so it will be read later
			self._parse_response(response)
		# note that sometimes the ok response is not terminated with \r\n eg M104 on Dreamer
		if b"\r\nok" in response:
			self._logger.debug("sendcommand() got an ok")
			return True, response
		return False, response

	def makeexclusive(self, exclusive):
		"""	Obtain exclusive use of the connection for the current thread"""

		if exclusive:
			self._readlock.acquire()
			self._writelock.acquire()
		else:
			self._readlock.release()
			self._writelock.release()

	def close(self):
		"""	Close USB connection and cleanup. OctoPrint Serial Factory method"""

		self._logger.debug("close()")

		self._disconnect_event = True
		if self._keep_alive_t:
			self._keep_alive_t.join()

		# cleanup
		if self._handle:
			self._logger.debug("closing handle...")
			if not self._readlock.locked():
				# TODO: try to fetch any pending replies from the printer.
				#  This doesn't really work properly right now as sometimes the printer stops returning responses...
				#  Unfortunately if we close the port without reading all pending data it seems to break subsequent
				#  connections to the printer, requiring a printer reboot
				self._readlock.acquire()
				try:
					while True:
						data = self._handle.bulkRead(self._usb_cmd_endpoint_in, self.BUFFER_SIZE, 3000)
						self._logger.debug("bulkRead() {}".format(data.decode().replace("\r\n", " | ")))
				except usb1.USBError as usberror:
					self._logger.debug("bulkRead() error {}".format(usberror))
					pass
				self._readlock.release()
			try:
				self._handle.releaseInterface(0)
			except usb1.USBError as usberror:
				self._logger.debug("Error releasing handle {}".format(usberror))
				pass
			try:
				self._handle.close()
			except usb1.USBError as usberror:
				raise FlashForgeError("Error closing USB handle", usberror)
			self._handle = None

		self._incoming = None

		self._plugin.on_disconnect()


"""
settingsFileBuilder.py
Marcus Viscardi,    March 27, 2023

Simple goal of this script is to provide a fast and foolproof way
to build the settings files that I use for my nanopore pipeline.

Currently, even with my knowledge of what goes into this, it is an
annoying process of copying and pasting data paths, output paths,
and other details.

This script will provide a GUI based method to build the file(s).
"""
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
from functools import partial

# ChatGPT provided the framework I used to write the below class! Crazy.
class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.checkbox_dict = None
        self.submit_button = None
        self.cancel_button = None
        self.files_and_dirs_dict = None
        self.steps_dict = None
        self.checkbox_header = None
        self.lbl_header = None
        self.master = master
        self.pack(side="top", fill="both", expand=True)
        self.create_widgets()

    def create_widgets(self):
        current_row = 0

        # Add header
        self.lbl_header = tk.Label(self, text="Nanopore Pipeline Settings File Builder", font=("Arial", 16, "bold underline"))
        self.lbl_header.grid(row=0, column=0, columnspan=4, pady=10)
        current_row += 1

        # Add checkbox section
        self.checkbox_header = tk.Label(self, text="Select Steps to Run:", font=("Arial", 12, "bold"))
        self.checkbox_header.grid(row=current_row, column=0, columnspan=4)
        current_row += 1

        self.steps_dict = {"G": ["Guppy Basecalling", True],
                           "A": ["Filtering Alt. Genomes (currently pretty rough and slow)", False],
                           "T": ["Trimming TERA-Seq Adapters", False],
                           "M": ["Minimap2 and SamTools", True],
                           "C": ["Concatenate Files", True],
                           "N": ["Nanopolish Index and polyA Calling", True],
                           "F": ["FeatureCounts", True],
                           "P": ["Merging Results w/ Pandas", True],
                           "S": ["Mapping Old Standards (still experimental!!)", False],
                           "L": ["Calling Transcripts w/ Flair", True],
                           "X": ["Running random eXtra steps", False],
                           }
        self.checkbox_dict = {}

        for i, (step, (description, default_state)) in enumerate(self.steps_dict.items()):
            var = tk.BooleanVar(value=default_state)
            checkbox = tk.Checkbutton(self, text=f"{step}: {description}", variable=var)
            self.checkbox_dict[step] = var
            checkbox.grid(row=i + current_row, column=0, columnspan=4, sticky="w", padx=(20, 0), pady=(0, 5))
        current_row += len(self.checkbox_dict)

        sep = ttk.Separator(self, orient='horizontal')
        sep.grid(row=current_row, column=0, columnspan=4, sticky="ew", padx=(20, 0), pady=(0, 5))
        current_row += 1

        # Per path selection:
        files_and_dirs_to_select_dict = {'settingsFile': '',
                                         'dataDir': '',
                                         'genomeDir': '/data16/marcus/genomes/plus_cerENO2_elegansRelease100',
                                         'outputDir': ''}
        self.files_and_dirs_dict = {}  # each of the above items will get a label, entry box, and browse button
        for file_or_dir_target, default_path in files_and_dirs_to_select_dict.items():
            lbl = tk.Label(self, text=f"Enter path for {file_or_dir_target}:")
            lbl.grid(row=current_row, column=0, sticky="w", padx=(20, 0), pady=(10, 5))
            ent = tk.Entry(self)
            ent.grid(row=current_row, column=1, columnspan=2, sticky="ew", pady=(10, 5))
            if default_path:
                ent.insert(0, default_path)
            file_or_dir_specific_func = partial(self.browse_file, file_or_dir_target)
            btn = tk.Button(self, text="Browse", command=file_or_dir_specific_func)
            btn.grid(row=current_row, column=3, padx=(0, 20), pady=(10, 5))
            self.files_and_dirs_dict[file_or_dir_target] = [lbl, ent, btn, file_or_dir_specific_func]
            current_row += 1

        self.cancel_button = tk.Button(self, text="Cancel", command=self.cancel)
        self.cancel_button.grid(row=current_row, column=1, pady=(0, 20))
        self.submit_button = tk.Button(self, text="Submit", command=self.submit)
        self.submit_button.grid(row=current_row, column=2, pady=(0, 20))
        current_row += 1

        # Configure the root window to expand
        self.master.columnconfigure(0, weight=1)
        self.master.rowconfigure(0, weight=1)

        # Set weight for rows and columns to make the frame scale with the window
        for i in range(current_row):
            self.grid_rowconfigure(i, weight=1)
        for i in range(4):
            self.grid_columnconfigure(i, weight=1)

    def browse_file(self, file_or_dir_trigger: str):
        if file_or_dir_trigger.endswith("Dir"):
            file_path = filedialog.askdirectory()
        elif file_or_dir_trigger.endswith("File"):
            file_path = filedialog.askopenfilename()
        else:
            file_path = None
        self.files_and_dirs_dict[file_or_dir_trigger][1].delete(0, tk.END)
        self.files_and_dirs_dict[file_or_dir_trigger][1].insert(tk.END, file_path)

    def submit(self):
        # print(f"Steps To Run:")
        stepsToRun = ''
        for stepToRun, stepVar in self.checkbox_dict.items():
            if stepVar.get():
                stepsToRun += stepToRun
        print(f"stepsToRun|{stepsToRun}")

        for file_or_dir, (_, ent, _, _) in self.files_and_dirs_dict.items():
            enter_box_content = ent.get()
            if enter_box_content:
                print(f"{file_or_dir}|{enter_box_content}")
        self.master.destroy()

    def cancel(self):
        self.master.destroy()

if __name__ == '__main__':
    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()


r"""
Downloads the relevant archives, verifies they are all the same version, and
prepares to upload them per the release playbook.

Use bazel to build and use the tool:

  bazel build //tools/release_engineering:download_release_candidate   # build
  bazel-bin/tools/release_engineering/download_release_candidate       # run

Here's an example of how to download the archives:

  bazel build //tools/release_engineering:download_release_candidate
  bazel-bin/tools/release_engineering/download_release_candidate \
    --version v0.27.0 --timestamp 20210216
"""

import argparse
import os
import shlex
import subprocess
import sys
import tarfile

class UserError(RuntimeError):
    pass

def run(argv, check=True, shell=False, **kwargs):
    if shell:
        assert isinstance(argv, str)
        cmd = argv
    else:
        assert isinstance(argv, list)
        cmd = shlex_join(argv)
    print(f"+ {cmd}", file=sys.stderr)
    return subprocess.run(argv, check=check, shell=shell, **kwargs)

def shlex_join(argv):
    # TODO(eric.cousineau): Replace this with `shlex.join` when we exclusively
    # use Python>=3.8.
    return " ".join(map(shlex.quote, argv))

def get_commit_from_version(tar_file, timestamp):
    print(f"Extract version information from: {tar_file}...")
    with tarfile.open(tar_file, "r") as tar:
        version_member = tar.getmember("drake/share/doc/drake/VERSION.TXT")
        with tar.extractfile(version_member) as f:
            assert f is not None, tar_file
            text = f.read().decode("utf8")
    upload_timestamp, commit = text.split()
    assert upload_timestamp.startswith(timestamp), (
        tar_file, upload_timestamp, commit, timestamp,
    )
    return commit

def main():
    parser = argparse.ArgumentParser(
        prog="download_release_candidate", description=__doc__,
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument(
        "--version", type=str, required=True,
        help="Release version in vX.Y.Z format.")
    parser.add_argument(
        "--timestamp", type=str, required=True,
        help="Drake archive timestamp in YYYYMMDD format.")
    args = parser.parse_args()

    version = args.version
    timestamp = args.timestamp

    release_dir = f"/tmp/drake-release/{version}"
    if os.path.isdir(release_dir):
        raise UserError(
            f"Directory must not already exist: {release_dir}\n"
            f"If you are re-running this script, please remove the "
            f"directory.")

    os.makedirs(release_dir)
    print(f"+ cd {release_dir}", file=sys.stderr)
    os.chdir(release_dir)

    tar_file_list = [
        f"drake-{timestamp}-bionic.tar.gz",
        f"drake-{timestamp}-focal.tar.gz",
        f"drake-{timestamp}-mac.tar.gz",
    ]

    # Download.
    base_url = "https://drake-packages.csail.mit.edu/drake/nightly"
    for tar_file in tar_file_list:
        run(["wget", f"{base_url}/{tar_file}"])
        run(["wget", f"{base_url}/{tar_file}.sha512"])

    # Verify that each archive uses the same version.
    commit_list = [
        get_commit_from_version(tar_file, timestamp)
        for tar_file in tar_file_list
    ]
    commit_expected = commit_list[0]
    version_errors = []
    for tar_file, commit in zip(tar_file_list, commit_list):
        if commit != commit_expected:
            version_errors.append(
                f"For '{tar_file}': Commit '{commit}' is not "
                f"the expected value '{commit_expected}'")
    if len(version_errors) > 0:
        raise UserError("\n".join(version_errors))
    print(f"All versions have the same commit: {commit_expected}")

    # Checksums.
    # - Verify downloaded sha512.
    run("sha512sum -c *.sha512", shell=True)
    # - Generate sha256.
    for tar_file in tar_file_list:
        run(f"sha256sum {tar_file} > {tar_file}.sha256", shell=True)
    # - Verify.
    run("sha256sum -c *.sha256", shell=True)

    print()
    print(
        f"Ready! Please continue with the release playbook. The files "
        f"to uploaded are located in the following folder (Ctrl+Click "
        f"in the terminal to open in your file explorer):\n"
        f"\n"
        f"  file://{release_dir}\n")

if __name__ == "__main__":
    try:
        main()
    except UserError as e:
        print(e, file=sys.stderr)
        sys.exit(1)


import os
import time
import json
import re

sourcepath = ""   #change as needed
outputpath = ""   #change as needed

outputpath += "/out.json"   #output filename

def natural_sort(l):
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
    return sorted(l, key = alphanum_key)

paths = []
dict = {}
for files in os.listdir(sourcepath):
	filepath = os.path.join(sourcepath, files)
	paths.append(filepath)

paths = natural_sort(paths)

for path in paths:
	with open(path, 'r') as f:
		JSON = json.load(f)
	dict.update(JSON)

with open(outputpath, 'w') as f:
	json.dump(dict, f, indent = 2)


"""
the dataset for pascal voc
Reference:
    https://github.com/jfzhang95/pytorch-deeplab-xception
"""
import os
from torch.utils.data import Dataset
from PIL import Image
from utils.utils import encode_segmap

class VOCDataset(Dataset):
    """Pacal VOC Dateset"""

    def __init__(self, base_dir, split='train', transform=None):
        """
        Args:
             base_dir(str): Directory with all the images ande mask
             split(str): one of train and val or trainval
             transform (callable): transform to be applied on a sample.
        """
        self.image_dir = os.path.join(base_dir, "JPEGImages")
        self.mark_dir = os.path.join(base_dir, "SegmentationClass")
        self.split = split
        assert os.path.exists(self.image_dir) and os.path.exists(self.mark_dir)

        self.image_list = []
        with open(os.path.join(base_dir, self.split + ".txt"), "r") as f:
            lines = f.read().splitlines()
        for line in lines:
            self.image_list.append(line)

        self.transform = transform

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

    def __getitem__(self, idx):
        image_path = os.path.join(self.image_dir, "{}.jpg".format(self.image_list[idx]))
        image = Image.open(image_path)
        mask_path = os.path.join(self.mark_dir, "{}.png".format(self.image_list[idx]))
        mask = Image.open(mask_path).convert("RGB")
        mask = encode_segmap(mask) # encode segmentation label images as pascal classes
        mask = Image.fromarray(mask) # convert numpy array to Image array
        sample = {"image": image, "mask": mask}
        if self.transform:
            sample = self.transform(sample)
        return sample

if __name__ == '__main__':
    """
    test VOCDataset
    """
    import numpy as np
    import matplotlib.pyplot as plt
    from transform import Normalize, RandomScaleCrop, RandomGaussianBlur, RandomHorizontalFlip, ToTensor
    from torchvision import transforms
    from torch.utils.data import DataLoader
    from utils import utils

    trainset = VOCDataset("data/train",
            transform=transforms.Compose(
                [
                    RandomScaleCrop(550, 512),
                    RandomHorizontalFlip(),
                    Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
                    ToTensor()
                ]
                )
        )
    dataloader = DataLoader(trainset, batch_size=2, shuffle=True, num_workers=0)

    for i, sample in enumerate(dataloader):
        for j in range(sample["image"].size()[0]):
            image = sample["image"][j].numpy()
            mask = sample["mask"][j].numpy()
            image = image.transpose([1, 2, 0])
            image *= (0.229, 0.224, 0.225)
            image += (0.485, 0.456, 0.406)
            image = image * 255
            image = image.astype(np.uint8)
            mask = mask.astype(np.uint8)

            mask = utils.decode_segmap(mask)

            plt.figure()
            plt.subplot(2, 2, 1)
            plt.imshow(image)
            plt.subplot(2, 2, 2)
            plt.imshow(mask)
        if i == 0:
            break
    plt.show()


from selenium.webdriver.common.by import By

BASE_URL = 'https://petfriends.skillfactory.ru/'

def test_show_my_pets(selenium_driver):
    driver = selenium_driver

    driver.find_element(By.CSS_SELECTOR, 'a.nav-link[href="/my_pets"]').click()
    assert driver.current_url == BASE_URL + 'my_pets'

    pets_number = driver.find_element(By.XPATH, '//div[@class=".col-sm-4 left"]').text.split('\n')[1].split(': ')[1]
    pets_count = driver.find_elements(By.XPATH, '//table[@class="table table-hover"]/tbody/tr')
    assert int(pets_number) == len(pets_count)


from django import forms
from .models import Product
from .models import Historic
from markets.models import Market

class HistoricForm (forms.ModelForm):
    price = forms.CharField(
        error_messages={'required': 'Este campo é obrigatório! Preencha este campo com o preço do produto.'},
        widget=forms.TextInput(
            attrs={
                'class' : 'form-control',
                'placeholder' : 'Preço',
            }
        )
    )
    cod_commerce = forms.ChoiceField()

    def __init__(self, user, *args, **kwargs):
        emptyField = [('', '----------')]
        commerces = emptyField + [(cod_commerce.pk, cod_commerce.name) for cod_commerce in Market.objects.all()]
        super(HistoricForm, self).__init__(*args, **kwargs)
        self.fields['cod_commerce'] = forms.ChoiceField(
            choices= commerces
        )
        self.fields['cod_commerce'].widget.attrs['class'] = 'form-control'
        self.fields['cod_commerce'].error_messages['required'] = 'Este campo é obrigatório! Preencha este campo com o mercado do produto.'

    class Meta:
        model = Historic
        fields = ('price', 'cod_commerce')

class ProductForm(forms.ModelForm):
    name = forms.CharField(
        error_messages={'required': 'Este campo é obrigatório! Preencha este campo com o nome do produto.'},
        widget=forms.TextInput(
            attrs={
                'class' : 'form-control',
                'placeholder' : 'Nome',
            }
        )
    )
    brand = forms.CharField(
        error_messages={'required': 'Este campo é obrigatório! Preencha este campo com a marca do produto.'},
        widget=forms.TextInput(
            attrs={
                'class' : 'form-control',
                'placeholder' : 'Marca',
            }
        )
    )
    description = forms.CharField(
        error_messages={'required': 'Este campo é obrigatório! Preencha este campo com a descrição do produto.'},
        widget=forms.TextInput(
            attrs={
                'class' : 'form-control',
                'placeholder' : 'Descrição',
            }
        )
    )
    category = forms.CharField(
        error_messages={'required': 'Este campo é obrigatório! Preencha este campo com a categoria do produto.'},
        widget=forms.TextInput(
            attrs={
                'class' : 'form-control',
                'placeholder' : 'Categoria',
            }
        )
    )
    photo = forms.CharField(
        error_messages={'required': 'Este campo é obrigatório! Preencha este campo com a url da imagem do produto.'},
        widget=forms.TextInput(
            attrs={
                'class' : 'form-control',
                'placeholder' : 'Url da foto',
            }
        )
    )
    class Meta:
        model = Product
        fields = ['name', 'brand', 'description', 'category', 'photo']


from collections import defaultdict
import numpy as np
import torchvision

class IMBALANCECIFAR100(torchvision.datasets.CIFAR100):
    cls_num = 100

    def __init__(self, root, imb_factor=None, rand_number=0, train=True,
                 transform=None, target_transform=None, download=True):
        super().__init__(root, train, transform, target_transform, download)

        if train and imb_factor is not None:
            np.random.seed(rand_number)
            img_num_list = self.get_img_num_per_cls(self.cls_num, imb_factor)
            self.gen_imbalanced_data(img_num_list)

        self.classnames = self.classes
        self.labels = self.targets
        self.cls_num_list = self.get_cls_num_list()
        self.num_classes = len(self.cls_num_list)

    def get_img_num_per_cls(self, cls_num, imb_factor):
        img_max = len(self.data) / cls_num
        img_num_per_cls = []
        for cls_idx in range(cls_num):
            num = img_max * (imb_factor**(cls_idx / (cls_num - 1.0)))
            img_num_per_cls.append(int(num))
        return img_num_per_cls

    def gen_imbalanced_data(self, img_num_per_cls):
        new_data = []
        new_targets = []
        targets_np = np.array(self.targets, dtype=np.int64)
        classes = np.unique(targets_np)
        # np.random.shuffle(classes)
        self.num_per_cls_dict = dict()
        for the_class, the_img_num in zip(classes, img_num_per_cls):
            self.num_per_cls_dict[the_class] = the_img_num
            idx = np.where(targets_np == the_class)[0]
            np.random.shuffle(idx)
            selec_idx = idx[:the_img_num]
            new_data.append(self.data[selec_idx, ...])
            new_targets.extend([the_class, ] * the_img_num)
        new_data = np.vstack(new_data)
        self.data = new_data
        self.targets = new_targets

    def get_cls_num_list(self):
        counter = defaultdict(int)
        for label in self.labels:
            counter[label] += 1
        labels = list(counter.keys())
        labels.sort()
        cls_num_list = [counter[label] for label in labels]
        return cls_num_list

class CIFAR100(IMBALANCECIFAR100):
    def __init__(self, root, train=True, transform=None):
        super().__init__(root, imb_factor=None, train=train, transform=transform)

class CIFAR100_IR10(IMBALANCECIFAR100):
    def __init__(self, root, train=True, transform=None):
        super().__init__(root, imb_factor=0.1, train=train, transform=transform)

class CIFAR100_IR50(IMBALANCECIFAR100):
    def __init__(self, root, train=True, transform=None):
        super().__init__(root, imb_factor=0.02, train=train, transform=transform)

class CIFAR100_IR100(IMBALANCECIFAR100):
    def __init__(self, root, train=True, transform=None):
        super().__init__(root, imb_factor=0.01, train=train, transform=transform)


from setuptools import setup, find_packages

with open("requirements.txt") as f:
	install_requires = f.read().strip().split("\n")

# get version from __version__ variable in eventcheckin/__init__.py
from eventcheckin import __version__ as version

setup(
	name="eventcheckin",
	version=version,
	description="This app allows to do checkin on Events",
	author="Aadhil",
	author_email="aadhilpm.com",
	packages=find_packages(),
	zip_safe=False,
	include_package_data=True,
	install_requires=install_requires
)


import os,sys
import cv2
import dlib
from imutils import face_utils
from scipy.spatial import distance
import datetime
import csv

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('../haarcascade_frontalface_alt2.xml')
face_parts_detector = dlib.shape_predictor('../shape_predictor_68_face_landmarks.dat')
blind_count = 0
prev_blink = False

# 目のアスペクト比を求める
def calc_ear(eye):
    A = distance.euclidean(eye[1], eye[5])
    B = distance.euclidean(eye[2], eye[4])
    beside = distance.euclidean(eye[0], eye[3])
    vertical = (A + B) / 2
    eye_ear = vertical / beside
    return round(eye_ear, 3)

while True:
    # カメラから１フレーム読み込む
    ret, img = cap.read()

    #画像をグレースケール化
    img_gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # facesには顔の位置情報が格納されている
    faces = face_cascade.detectMultiScale(
            img_gry, scaleFactor=1.11, minNeighbors=3, minSize=(100, 100))

    if len(faces) == 1:
        x, y, w, h = faces[0, :]
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

        face = dlib.rectangle(x, y, x + w, y + h)
        face_parts = face_parts_detector(img_gry, face)
        face_parts = face_utils.shape_to_np(face_parts)

        # 右目のアスペクト比を求める
        right_eye = face_parts[42:48]
        right_eye_ear = calc_ear(right_eye)

        # 左目のアスペクト比を求める
        left_eye = face_parts[36:42]
        left_eye_ear = calc_ear(left_eye)

        if (right_eye_ear + left_eye_ear) < 0.4:
            curEyeOpen = 0
        else:
            curEyeOpen = 1

        if curEyeOpen == 0 and lastEyeOpen == 1:
            blind_count += 1

            with open('data.csv', 'a', newline='') as csvfile:
                writer = csv.writer(csvfile)

                # csvファイルが空である場合はヘッダー行を書き込む
                if csvfile.tell() == 0:
                    writer.writerow([ 'Timestamp', 'Blink' ,'Blink_Count'])

                writer.writerow([str(datetime.datetime.now()), 'blink!!', blind_count])

        lastEyeOpen = curEyeOpen

    cv2.putText(img, str(blind_count), (10, 100), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA)
    cv2.imshow('frame', img)

    if cv2.waitKey(1) == 27:
        break  # esc to quit

cap.release()
cv2.destroyAllWindows()


# Rose diagrams
# Script to make rose diagram of the basin orientation and fault strike.
# FJC 30/06/20

# import packages
import numpy as np
import matplotlib.pyplot as plt

# shapefiles
import geopandas as gpd

# read in the basin shapefiles
data_dir = '/home/bjdd72/san_andreas/'
gdf = gpd.read_file(data_dir+'SAF_only_basins.shp')

print(gdf)

# bin the orientations into 10 degree bins.
bin_width = 5
bin_edges = np.arange(0, 360+bin_width, bin_width)
basins_hist, bin_edges = np.histogram(gdf['azimuth'], bins=bin_edges)

# read in the fault azimuth shapefile
pts = gpd.read_file(data_dir+'SanAndreasPoints.shp')
# double the strikes with directions at 180 degrees to get a mirrored rose diagram
strikes = pts['azimuth'].values
strikes_mirrored = strikes + 180
all_strikes = np.concatenate([strikes, strikes_mirrored])
strike_hist, bin_edges = np.histogram(all_strikes, bins=bin_edges)

# set up the figure
fig = plt.figure(figsize=(14,8))

# set up the fault strike subplot
ax = fig.add_subplot(121, projection='polar')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_title('Fault strike orientation', y=1.1, fontsize=16)
ax.bar(np.deg2rad(bin_edges[0:-1]), strike_hist, width=np.deg2rad(bin_width), align='edge', edgecolor='0.2', color='0.5', zorder=2, alpha=0.5)
ax.tick_params(labelsize=12)

# set up the basin orientation subplot
ax = fig.add_subplot(122, projection='polar')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_title('Drainage basin orientation', y=1.1, fontsize=16)
ax.tick_params(labelsize=12)

# plot the histogram as bars. The bin edges are used as the left hand coordinate for the bars, minus the last edge. The histogram counts are the bar heights.
ax.bar(np.deg2rad(bin_edges[0:-1]), basins_hist, width=np.deg2rad(bin_width), align='edge', edgecolor='0.2', color='0.5', zorder=2, alpha=0.5)
#plt.subplots_adjust(wspace=0.5)
plt.savefig('SAF_rose_diagrams.png', dpi=300)


import numpy as np
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import streamlit as st
from tensorflow import keras
from keras.models import model_from_json
from keras.preprocessing.image import img_to_array
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, RTCConfiguration, VideoProcessorBase, WebRtcMode

classifier = keras.models.load_model('Final_model_Custome_CNN.h5')

#load face
try:
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    emotion_labels = ['Angry','Disgust','Fear','Happy','Neutral', 'Sad', 'Surprise']  # Emotion that will be predicted

except Exception:
    st.write("Error loading cascade classifiers")

RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})

class Faceemotion(VideoTransformerBase):
    def transform(self, frame):
      label = []
      img = frame.to_ndarray(format="bgr24")
      face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
      emotion_labels = ['Angry','Disgust','Fear','Happy','Neutral', 'Sad', 'Surprise']

      #image gray
      img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      faces = face_cascade.detectMultiScale(
          image=img_gray, scaleFactor=1.3, minNeighbors=1)

      for (x, y, w, h) in faces:
          a=cv2.rectangle(img=img, pt1=(x, y), pt2=(
              x + w, y + h), color=(255, 0, 0), thickness=2)
          roi_gray = img_gray[y:y + h, x:x + w]
          roi_gray = cv2.resize(roi_gray, (48, 48), interpolation=cv2.INTER_AREA)
          roi = roi_gray.astype('float')/255.0
          roi = img_to_array(roi)
          roi = np.expand_dims(roi,axis=0) ## reshaping the cropped face image for prediction
          prediction = classifier.predict(roi)[0]   #Prediction
          label=emotion_labels[prediction.argmax()]
          label_position = (x,y)
          b=cv2.putText(a,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
          label_position = (x, y)

      return b

def main():
    # Face Analysis Application #
    st.markdown("
Real Time Face Emotion Detection Application
", unsafe_allow_html=True)

    #add space
    st.markdown("
", unsafe_allow_html=True)

    #add paragraph text and position it in the center
    st.markdown("
Click on start to use webcam and detect your face emotion
", unsafe_allow_html=True)

    #add space
    st.markdown("
", unsafe_allow_html=True)

    webrtc_streamer(key="example", mode=WebRtcMode.SENDRECV, rtc_configuration=RTC_CONFIGURATION,
                        video_processor_factory=Faceemotion)
    #add space
    st.markdown("
", unsafe_allow_html=True)

    #add space
    st.markdown("
", unsafe_allow_html=True)

    html_temp4 = """

This Application is developed by Harish using Streamlit Framework, Opencv, Tensorflow and Keras library for demonstration purpose.

Thanks for Visiting

"""

    st.markdown(html_temp4, unsafe_allow_html=True)

if __name__ == "__main__":
    main()


from flask import Flask, render_template, redirect, url_for, request, send_file
from api.login import Session
from api.login import AuthenticationFailedError
from datetime import datetime
import pandas as pd
import numpy as np
import time
import  requests
app = Flask(__name__)

# TODO: aggiungere hoover info profilo
# TODO: aggiungere estensione voti
    # TODO: voto minimo per avere 6

# TODO: risolvere bug data prossimi eventi
# TODO: possibilmente nel grafico tenere solo 2 cifre decimali
@app.route('/', methods=['GET', 'POST'])
def login():

    user_session = Session()
    error = None

    if request.method == 'POST':
        try:
            user_session.login(request.form['username'], request.form['password'])

            # prende tutti i dati necessari per la dashboard
            lessons = user_session.lessons()
            grades = user_session.grades()
            periods = user_session.periods()
            absences = user_session.absences()
            noticeboard = user_session.noticeboard()
            didactics = user_session.didactics()
            cards = user_session.cards()

            return render_template("dashboard.html",

            lessons=lessons,

            #eventi
            events=get_events(user_session),

            parse_event_date=parse_event_date,

            #grafici
            #media
            graph_media=graph_media(grades),

            # media durante i periodi (Q1: 7.50, Q2 = 8.00)
            media_periodi = get_medie_periods(periods, grades),

            # voti durante anno
            gradesData=graph_grades(grades),

            # ultimi voti
            grades=parse_grades(grades),

            # assenze per la dashboard, review
            absences_review = get_totale_assenze(absences),

            absences_data = get_months_absences(absences),

            log_out = log_out(),
            noticeboard_titles = get_noticeboard_titles(noticeboard),

            noticeboard_dates = get_noticeboard_dates(noticeboard),

            didactics = didactics,

            cards = cards['cards']
            )
        except AuthenticationFailedError as e:
            error = "Username o password errati"
            return render_template('login.html', error=error)

    return render_template('login.html', error=error)

#dashboard
@app.route("/dashboard")
def dashboard():
    return redirect(url_for('login'))

@app.route("/logout")
def log_out():
    return render_template('login.html')

# box prossimi eventi
# elaborazione data (2018-10-22) --> 22-10-2018
def parse_event_date(event_date):
    parts = event_date.split("-")
    return parts[2][:2] + "/" + parts[1]

#prende gli eventi dal giorno 'today' fino alla fine della scuola

def get_events(user_session):
    begin = datetime(2019,3,22)
    end = datetime(2019,6,9)
    return user_session.agenda(begin,end)

# ultimi 5 voti --> grades=parse_grades(grades)
def parse_grades(grades):
    # trasforma i voti in un dataframe
    df = pd.DataFrame.from_dict(grades['grades'])
    # converte la colonna evt date in time
    df['evtDate'] = pd.to_datetime(df['evtDate'], format='%Y-%m-%d')
    # ordina le varie date in ordine cronologico decrescente (utlimo --> primo)
    df = df.sort_values('evtId', ascending=False)
    # prende i primi 5 voti
    gradeslist = df.iloc[0:5].values.tolist()

    return gradeslist

# gradesData=graph_grades(grades) --> elabora dati per grafico dei voti durante l'anno
def graph_grades(grades):

    # ordina in base alla data (crescente)
    results = sorted(grades['grades'],key=lambda x : time.strptime(x['evtDate'],"%Y-%m-%d"))
    output=""
    # per ogni data prende i valori che non sono None e crea un output ( {x: 'data', y: 'voto'}) che andrá nel grafico
    for result in results:
        if str(result['decimalValue']) == 'None':
            pass
        else:
            output += "{ x: '" + result['evtDate'] + "', y: " + str(result['decimalValue']) +  "},"
    #toglie la virgola iniziale
    return output[:-1]

# graph_media=graph_media(grades) fa un grafico della media dei voti
def graph_media(grades):
    media = 0
    output=""
    # ordina in base alla data (crescente)
    results = sorted(grades['grades'],key=lambda x : time.strptime(x['evtDate'],"%Y-%m-%d"))
    i = 1
    for result in results:
        if str(result['decimalValue']) == 'None':
            pass
        else:
            # calcola la media in quel giorno
            media += result['decimalValue']
            output += "{ x: '" + result['evtDate'] + "', y: " + str(media/i) +  "},"
            i+=1
    return output

#    media_voti = get_media(grades), tipo list
def get_media(grades):
    media = 0
    div = 1
    for grade in grades['grades']:
        if str(grade['decimalValue']) == 'None' :
            pass
        else:
            media += grade['decimalValue']
            div += 1
    return media/div;
    # TODO: finire funzione media, sbagliata

def get_medie_periods(periods, grades):
    periods_medie = []
    periods_pos = []
    df = pd.DataFrame.from_dict(grades['grades'])
    for period in periods['periods']:
        periods_pos.append(period['periodPos'])

    output = ""
    for period in periods_pos:
    #    output +
        periods_medie.append(df['decimalValue'][df['periodPos'] == period].mean())
    return periods_medie

def get_totale_assenze(absences):

    df = pd.DataFrame.from_dict(absences['events'])

    assenze = df[df['evtCode'] == 'ABA0'].values.tolist()
    uscite = df[df['evtCode'] == 'ABU0'].values.tolist()
    ritardi = df[df['evtCode'] == 'ABR0'].values.tolist()
    ritardibrevi =df[df['evtCode'] == 'ABR1'].values.tolist()
    not_justified = df[df['isJustified'] == False].values.tolist()

    output = [len(assenze), len(uscite), len(ritardi), len(ritardibrevi), len(not_justified)]
    return output

def get_months_absences(absences):

    output = []

    assenze = [0,0,0,0,0,0,0,0,0,0,0,0]
    uscite = [0,0,0,0,0,0,0,0,0,0,0,0]
    ritardi = [0,0,0,0,0,0,0,0,0,0,0,0]

    for event in absences['events']:
        if event['evtCode'] == 'ABA0':
            assenze[int(event['evtDate'].split('-')[1])-1] += 1
        elif event['evtCode'] == 'ABU0':
            uscite[int(event['evtDate'].split('-')[1])-1] += 1
        else:
            ritardi[int(event['evtDate'].split('-')[1])-1] += 1

    output.append(assenze[-4:] + assenze[:6])
    output.append(uscite[-4:] + uscite[:6])
    output.append(ritardi[-4:] + ritardi[:6])

    return output

def get_noticeboard_titles(noticeboard):
    output = []
    url = 'https://web.spaggiari.eu/sif/app/default/bacheca_utente.php?action=file_download&com_id='

    for i in range(0, 5):
        if noticeboard['items'][i]['cntHasAttach'] == True:
            output.append([noticeboard['items'][i]['cntTitle'], elabora_data(noticeboard['items'][i]['pubDT']), url+str(noticeboard['items'][i]['cntId'])])
        else:
            output.append([noticeboard['items'][i]['cntTitle'], elabora_data(noticeboard['items'][i]['pubDT']), ''])

    return output

def get_noticeboard_dates(noticeboard):
    return 0
    #output = []
    #for i in range(0, 5):
    #    output.append(elabora_data(noticeboard['items'][i]['pubDT']))

def elabora_data(dt):
    '''
    input: 2019-03-21T15:11:18+01:00
    output: 21-03-2019
    '''
    parts = dt.split('-')

    return parts[2][:2] + "-" + parts[1] + "-" + parts[0]

# def get_didactics(didactics):
#     output = []
#
#     for materials in didactics['didacticts']
#         materials_content = []
#         for material in materials:
#             materials_content.append(material['folderName'], material['lastShareDT'])
#         output.append([materials['teacherName'].lower().capitalize(), materials_content])

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


# Dataset
# synthetic dataset for simple regression
from matplotlib import pyplot as plt
from sklearn.datasets import make_regression

# plt.figure()
# plt.title('Sample regression problem with one input variable')
X_R1, y_R1 = make_regression(n_samples=100, n_features=1,
                            n_informative=1, bias=150.0,
                            noise=30, random_state=0)
# plt.scatter(X_R1, y_R1, marker='o', s=50)
# plt.show()

from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
import numpy as np

fig, subaxes = plt.subplots(2, 3, figsize=(11,8), dpi=70)

X_predict_input = np.linspace(-3, 3, 50).reshape(-1, 1)  # make up a test set

X_train, X_test, y_train, y_test = train_test_split(X_R1[0::5], y_R1[0::5], random_state = 0)

for thisaxisrow, thisactivation in zip(subaxes, ['tanh', 'relu']):
    for thisalpha, thisaxis in zip([0.0001, 1.0, 100], thisaxisrow):
        mlpreg = MLPRegressor(hidden_layer_sizes = [100,100],
                             activation = thisactivation,
                             alpha = thisalpha,
                             solver = 'lbfgs').fit(X_train, y_train)
        y_predict_output = mlpreg.predict(X_predict_input)
        thisaxis.set_xlim([-2.5, 0.75])
        thisaxis.plot(X_predict_input, y_predict_output,
                     '^', markersize = 10)
        thisaxis.plot(X_train, y_train, 'o')
        thisaxis.set_xlabel('Input feature')
        thisaxis.set_ylabel('Target value')
        thisaxis.set_title('MLP regression\nalpha={}, activation={})'
                          .format(thisalpha, thisactivation))
        plt.tight_layout()

plt.show()

Python Project/Neural networks: Regression.py


import paramiko
import time

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect('40.40.40.1', port=22, username='admin', password='')

print('Anda Berhasil Login\n')

stdin,stdout,stderr = ssh_client.exec_command("interface wireless enable wlan1") #enable hotspot
stdin,stdout,stderr = ssh_client.exec_command("ip dhcp-client add interface=ether1 disabled=no")
stdin,stdout,stderr = ssh_client.exec_command("interface wireless set mode=ap-bridge numbers=wlan1") #Menjadikan wirelles menjadi mode apbridge
stdin,stdout,stderr = ssh_client.exec_command("interface wireless set wlan1 ssid=Bandi-Wifi") #Membuat ssid untuk hotspot
stdin,stdout,stderr = ssh_client.exec_command("ip firewall nat add chain=srcnat out-interface=ether1 action=masquerade") #agar router mendapat internet
stdin,stdout,stderr = ssh_client.exec_command("ip address add address=20.20.20.1/24 interface=ether2") #seting ip address interface untuk hotspot
time.sleep(1)
stdin,stdout,stderr = ssh_client.exec_command("ip pool add name=dhcp_hotspot ranges=20.20.20.2-20.20.20.254") #bikin pool untuk dhcp server
stdin,stdout,stderr = ssh_client.exec_command("ip dhcp-server add address-pool=dhcp_hotspot disabled=no interface=ether2 name=hotspot") #bikin dhcp-server pake pool yang sebelumnya dibuat
stdin,stdout,stderr = ssh_client.exec_command("ip dhcp-server network add address=20.20.20.0/24 dns-server=8.8.8.8 gateway=20.20.20.1") #masukin network dhcp-server kita , jangan sampai kelewat
stdin,stdout,stderr = ssh_client.exec_command("ip hotspot profile add dns-name=bandi.net hotspot-address=20.20.20.1 name=hotspot1") # buat profile buat hotspot
stdin,stdout,stderr = ssh_client.exec_command("ip hotspot add interface=ether2 name=hotspot2 address-pool=dhcp_hotspot profile=hotspot1 addresses-per-mac=2 disabled=no")
stdin,stdout,stderr = ssh_client.exec_command("ip hotspot add address-pool=dhcp_hotspot disabled=no interface=wlan1 name=hotspot1 profile=hotspot1 html-directory=hotspot") # buat hotspot

time.sleep(1)
stdin, stdout, stderr = ssh_client.exec_command('ip address print')

stdin, stdout, stderr = ssh_client.exec_command('log print')

output = stdout.readlines()

print('\n'.join(output))

ssh_client.close


# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from news.models import Column
from news.models import Article
from django.shortcuts import redirect
from django.http import JsonResponse
from django.template import RequestContext

def index(request):
    columns = Column.objects.all()
    # return redirect("http://www.ziqiangxuetang.com/django/django-cms-develop3.html")
    # return render(request, 'index.html', {'columns': columns})
    return render(request, 'index.html')

def column_detail(request, column_slug):
    # return HttpResponse('colmun slug: ' + column_slug)
    column = Column.objects.get(slug=column_slug)
    return render(request, 'news/column.html', {'column': column})

def article_detail(request, pk, article_slug):
    article = Article.objects.get(pk=pk)
    if article_slug != article.slug:
        return redirect("http://www.ziqiangxuetang.com/django/django-cms-develop3.html")
    return render(request, 'news/article.html', {'article': article})

def template_index_reports(request):
    reports_tmp_return = 'kkkkk'
    return JsonResponse(reports_tmp_return, safe=False)

def file_upload(request):
    if request.method == "POST":  # 请求方法为POST时，进行处理
        myFile = request.FILES.get("uploadBox", None)  # 获取上传的文件，如果没有文件，则默认为None
        if not myFile:
            return render(request, 'index.html', {'uploadResult':"no files for upload!"})
        import os
        destination = open(os.path.join("D:\\TDDOWNLOAD", myFile.name), 'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
            destination.close()
            # return render(request, 'index.html', {'uploadResult':"glyphicon glyphicon-ok-circle"})
        return render(request, '', {'uploadResult': "file"})

def file_upload_json(request):
    if request.method == "POST":  # 请求方法为POST时，进行处理
        myFile = request.FILES.get("uploadBox", None)  # 获取上传的文件，如果没有文件，则默认为None
        if not myFile:
            result = 'none'
            return JsonResponse(result, safe=False)
        import os
        destination = open(os.path.join("D:\\TDDOWNLOAD", myFile.name), 'wb+')  # 打开特定的文件进行二进制的写操作
        result = 'success'
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
            destination.close()
            # return render(request, 'index.html', {'uploadResult':"glyphicon glyphicon-ok-circle"})
        #return JsonResponse(result, safe=False)
        import json
        return HttpResponse(json.dumps(result))

def file_upload_fileinput(request):
    result = {"data": []}
    try:
        ret = -1
        myfile = request.FILES.get("file_data", None)
        if myfile:
            import os
            destination = open(os.path.join("D:\\TDDOWNLOAD", myfile.name), 'wb+')  # 打开特定的文件进行二进制的写操作
            if not myfile:
                ret = 1
            ret = 0
        return JsonResponse(result)
    except Exception:
        return JsonResponse(result)


# coding=utf-8
from flask import g
from flask_restplus import Namespace, abort, fields
from sqlalchemy import and_, or_

from disquotes.model import Permission
from disquotes.views.api.base import ResourceBase

ns = Namespace("permissions", "Permissions storage.")

permission_model = ns.model("Permission", {
    "permission": fields.String
})

@ns.route("/")
@ns.param("server", "Discord Server ID", type=int, required=True)
@ns.param("channel", "Discord Channel ID", type=int)
class PermissionResource(ResourceBase):
    def get(self):
        server, channel = self.get_server_channel()

        if channel:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                or_(
                    Permission.channel_id == channel.id,
                    Permission.channel_id == None
                )
            ))
        else:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == None
            ))

        return {
            "permissions": [x[0] for x in query.all()]
        }

    @ns.param("perm", "Permission node", required=True)
    def put(self):
        server, channel = self.get_server_channel(create=True)

        if channel:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == channel.id
            ))
        else:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == None
            ))

        if query.filter(Permission.permission == self.get_field("perm")).scalar():
            abort(400, "Permission already exists for this channel/server.")

        new_perm = Permission(
            server_id=server.id,
            channel_id=channel.id if channel else None,
            permission=self.get_field("perm")
        )

        g.db.add(new_perm)

    @ns.param("perm", "Permission node", required=True)
    def delete(self):
        server, channel = self.get_server_channel()

        if channel:
            deleted_perm = g.db.query(Permission).filter(Permission.channel_id == channel.id)
        else:
            deleted_perm = g.db.query(Permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == None
            ))

        deleted_perm = deleted_perm.filter(Permission.permission == self.get_field("perm")).delete()


import os
import cv2
#import BlurDetection
from BlurDetection import BlurDetection

facefile = "/home/gubo/1/1/buqingchu/2-buqingchu/"
successfile = "/home/gubo/8/buqingchu/"
successfile1 = "/home/gubo/8/qingchu/"
successfile2 = "/home/gubo/8/0.33/"
successfile3 = "/home/gubo/8/0/"
def blur_detction(testImg):
    tmp = BlurDetection('')
    score = tmp._Tenengrad(testImg)
    return float(score)
piclist = os.listdir(facefile)
successlist = os.listdir(successfile)
# i = 0

piclist = os.listdir(facefile)
successlist = os.listdir(successfile)
for okname in piclist:
    if okname.endswith('jpg'):

        img = cv2.imread(facefile + okname)

        sp = img.shape
        y = sp[0] * 0.15
        #print(y)
        h = sp[0] * 0.70
        #print(h)
        x = sp[1] * 0.15

        w = sp[1] * 0.7
        testImg = img[int(y):int(y + h), int(x):int(x + w)]
        testImg = cv2.resize(testImg,(80,80),interpolation=cv2.INTER_LINEAR)
        cv2.resize()
        sp = testImg.shape
        #print(sp)
        #print(sp[0])
            #cal_score = Cal(testImg)
            #print(cal_score)
        score = blur_detction(testImg)
        score = round(score, 2)

        # print(str(score) +'-' + okname)
        #cv2.imwrite(successfile + str(score) + '-' + okname, img)
            #print(res_int)
        # os.system(
        #     'mv -f ' + ' ' + facefile + okname + ' ' + successfile + okname +'_' + str(score))


import logging

from openpyxl import load_workbook

from .bulk_upload import process_product_row, is_sheet_valid

logger = logging.getLogger(__name__)

def bulk_product_upload(file, user):
    """
    Uploads products specified in file.

    :type file: readable file stream
    :param file: the file that contains products information

    :type user: models.User
    :param user: the user that is requesting the upload.

    :rtype: dict
    :return: a dict with products divided by sheet.
    """
    if file is None:
        return {}

    wb = load_workbook(file, read_only=True)

    products_by_sheet = {}
    for sheet in wb.get_sheet_names():
        current_sheet = []
        if not is_sheet_valid(sheet):
            continue
        ws = wb.get_sheet_by_name(sheet)

        is_first_row = True
        for row in ws.rows:

            if is_first_row:
                is_first_row = False
                continue
            try:
                product, errors = process_product_row(row, sheet)
            except Exception as e:
                continue
            product['errors'] = errors
            current_sheet.append(product)

        products_by_sheet[sheet] = current_sheet
    return products_by_sheet


# Создайте программу для игры в ""Крестики-нолики"".
from tabulate import tabulate

# a = [[1, 2, 3],
#      [4, 5, 6],
#      [7, 8, 9]]
# X = "X"
# O = "O"

b = [["." for i in range(3)] for j in range(3)]
b[1] = "123"

print(tabulate(b, tablefmt="heavy_grid"))

Task_5/3_Крестики нолики.py

3_Крестики нолики.py


from gensim.models import LdaModel
from util import topic_match
from gensim import corpora

model_file = '.\\model\\lda_model'
lda = LdaModel.load(model_file)

corpus_file = '.\\data\\corpus_cut.txt'
sentences = []
for line in open(corpus_file, 'r', encoding='utf8'):
    line = line.strip()
    sentences.append(line.split(' '))

    #对文本进行处理，得到文本集合中的词表
dictionary = corpora.Dictionary(sentences)

comment_file = '.\\data\\comment.txt'
answer_file = '.\\data\\answer.txt'
result_file = 'result.txt'
topic_match(comment_file, answer_file, result_file, dictionary, lda)


import embera
import networkx as nx
import matplotlib.pyplot as plt

bench_graph_list = embera.benchmark.topologies.embera_bench()

nrows = 7
ncols = len(bench_graph_list)//nrows + bool(len(bench_graph_list)%nrows)
fig, axs = plt.subplots(nrows, ncols, subplot_kw={'aspect':'equal'})
fig.set_size_inches(ncols , nrows)
for i, ax in enumerate(axs.flat):
    if i>=len(bench_graph_list): fig.delaxes(ax); continue
    Sg = bench_graph_list[i]
    pos = Sg.graph.setdefault('pos', nx.spring_layout(Sg))
    nx.draw(Sg, pos=pos, ax=axs.flat[i], node_size=1, width=0.2, edge_color='grey')

plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)

plt.show()