import os
import shutil
import sys
import urllib
import xbmc
import xbmcaddon
import xbmcgui,xbmcplugin
import xbmcvfs,logging
import uuid
KODI_VERSION = int(xbmc.getInfoLabel("System.BuildVersion").split('.', 1)[0])
if KODI_VERSION<=18:
    xbmc_tranlate_path=xbmc.translatePath
else:
    import xbmcvfs
    xbmc_tranlate_path=xbmcvfs.translatePath
__addon__ = xbmcaddon.Addon()
__author__     = __addon__.getAddonInfo('author')
__scriptid__   = __addon__.getAddonInfo('id')
__scriptname__ = __addon__.getAddonInfo('name')
__version__    = __addon__.getAddonInfo('version')
__language__   = __addon__.getLocalizedString

__cwd__        = xbmc_tranlate_path( __addon__.getAddonInfo('path') )
__profile__    = xbmc_tranlate_path( __addon__.getAddonInfo('profile') )
__resource__   = xbmc_tranlate_path( os.path.join( __cwd__, 'resources', 'lib' ) )
__temp__       = xbmc_tranlate_path( os.path.join( __profile__, 'temp', '') )

if xbmcvfs.exists(__temp__):
  try:
    shutil.rmtree(__temp__)
  except:
    pass
xbmcvfs.mkdirs(__temp__)

sys.path.append (__resource__)

from OSUtilities import OSDBServer, log, hashFile, normalizeString
try:
    import HTMLParser
    html_parser = HTMLParser.HTMLParser()
    from urllib import urlretrieve
    from urllib import  unquote_plus, unquote, urlopen, quote
except:
    import html
    from urllib.request import urlretrieve
    from urllib.parse import  unquote_plus, unquote,  quote
def Search_opensubtitle( item,imdb_id ,mode_subtitle,all_setting):
  search_data = []

  logging.warning(imdb_id)
  search_data = OSDBServer().searchsubtitles(item,imdb_id,all_setting)

  subtitle_list=[]

  if search_data != None:

    search_data.sort(key=lambda x: [not x['MatchedBy'] == 'moviehash',
                     not os.path.splitext(x['SubFileName'])[0] == os.path.splitext(os.path.basename(unquote(item['file_original_path'])))[0],
                     not normalizeString(xbmc.getInfoLabel("VideoPlayer.OriginalTitle")).lower() in x['SubFileName'].replace('.',' ').lower(),
                     not x['LanguageName'] == 'Undetermined'])
    x=1
    url_list=[]
    for item_data in search_data:

      ## hack to work around issue where Brazilian is not found as language in XBMC

      try:
       item['season']=int(item['season'])
       item['episode']=int(item['episode'])
       item_data['SeriesSeason']=int(item_data['SeriesSeason'])
       item_data['SeriesEpisode']=int(item_data['SeriesEpisode'])
      except:
         pass
      if ((item['season'] == item_data['SeriesSeason'] and
          item['episode'] == item_data['SeriesEpisode']) or
          (item['season'] == 0 and item['episode'] == 0) ## for file search, season and episode == ""
         ):

        try:
            listitem = xbmcgui.ListItem(label          = item_data["LanguageName"],
                                    label2         = str(x)+ '. '+item_data["SubFileName"]+' [B][COLOR green][OpenSubtitles][/COLOR][/B]',
                                    iconImage      = str(int(round(float(item_data["SubRating"])/2))),
                                    thumbnailImage = item_data["ISO639"]
                                    )
        except:
            listitem = xbmcgui.ListItem(label          = item_data["LanguageName"],
                                    label2         = str(x)+ '. '+item_data["SubFileName"]+' [B][COLOR green][OpenSubtitles][/COLOR][/B]',

                                    )
        listitem.setProperty( "sync", ("false", "true")[str(item_data["MatchedBy"]) == "moviehash"] )
        listitem.setProperty( "hearing_imp", ("false", "true")[int(item_data["SubHearingImpaired"]) != 0] )
        url = "plugin://%s/?action=download&link=%s&ID=%s&filename=%s&format=%s&source=%s&language=%s" % (__scriptid__,
                                                                          item_data["ZipDownloadLink"],
                                                                          item_data["IDSubtitleFile"],
                                                                          item_data["SubFileName"],
                                                                          item_data["SubFormat"],
                                                                          'opensubtitle',
                                                                          item_data["LanguageName"]
                                                                          )

        json_data={'url':url,
                 'label':item_data["LanguageName"],
                 'label2':str(x)+ '. '+item_data["SubFileName"]+' [B][COLOR green][OpenSubtitles][/COLOR][/B]',
                 'iconImage':str(int(round(float(item_data["SubRating"])/2))),
                 'thumbnailImage':item_data["ISO639"],
                 'hearing_imp':("false", "true")[int(item_data["SubHearingImpaired"]) != 0],
                 'sync': ("false", "true")[str(item_data["MatchedBy"]) == "moviehash"]}

        if mode_subtitle>1  :
          if url not in url_list:

            url_list.append(url)
            subtitle_list.append(json_data)
          #xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listitem,isFolder=False)
          x=x+1
        else:
          subtitle_list.append(json_data)
          return  Download_opensubtitle( item_data["IDSubtitleFile"],item_data["ZipDownloadLink"],item_data["SubFormat"],mode_subtitle),subtitle_list

          break
    return " ",subtitle_list
def Download_opensubtitle(id,url,format,mode_subtitle,stack=False):
  subtitle_list = []
  exts = [".srt", ".sub", ".txt", ".smi", ".ssa", ".ass" ]
  if stack:         ## we only want XMLRPC download if movie is not in stack,
                    ## you can only retreive multiple subs in zip
    result = False
  else:
    subtitle = os.path.join(__temp__, "%s.%s" %(str(uuid.uuid4()), format))
    try:
      result =OSDBServer().download(id, subtitle)
    except:
      log( __name__, "failed to connect to service for subtitle download")
      return subtitle_list
  if not result:
    log( __name__,"Download Using HTTP")
    zip = os.path.join( __temp__, "OpenSubtitles.zip")
    f = urllib.urlopen(url)
    logging.warning(url)
    with open(zip, "wb") as subFile:
      subFile.write(f.read())
    subFile.close()
    xbmc.sleep(500)
    xbmc.executebuiltin(('Extract("%s","%s")' % (zip,__temp__,)), True)
    for file in xbmcvfs.listdir(zip)[1]:
      file = os.path.join(__temp__, file)
      if (os.path.splitext( file )[1] in exts):
        subtitle_list.append(file)
  else:
    subtitle_list.append(subtitle)

  if xbmcvfs.exists(subtitle_list[0]):
    if mode_subtitle>1:
      return subtitle_list
    else:
      return subtitle_list[0]

      #xbmc.Player().setSubtitles(subtitle_list[0])


import pytest
from django.urls import reverse

# Create your tests here.

@pytest.mark.django_db
def test_cart_detail_unauthorized(client):
    """Тест отображения корзины для неавторизованного пользователя"""
    url = reverse('cart:cart_detail')
    response = client.get(url)
    assert response.status_code == 302

@pytest.mark.django_db
def test_cart_detail_as_admin(admin_client):
    """Тест отображения корзины для админа"""
    url = reverse('cart:cart_detail')
    response = admin_client.get(url)
    assert response.status_code == 200


class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        rn = Counter(ransomNote)
        mn = Counter(magazine)

        for k, v in rn.items():
            t = mn.get(k, 0)
            if t < v:
                return False
        return True


# -*- coding: utf-8 -*-
#harvest theses (with english titles) from Tokyo U.
#FS: 2020-06-22

import getopt
import sys
import os
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup
import re
import ejlmod3
import time
import json
import datetime

startdate = datetime.datetime.now() + datetime.timedelta(days=-90-30)
stampofstartdate = '%4d-%02d-%02d' % (startdate.year, startdate.month, startdate.day)

publisher = 'Tokyo U.'
verbose = True
years = 2
skipalreadyharvested = True

hdr = {'User-Agent' : 'Magic Browser'}
boring = ['journal article', 'conference paper', 'departmental bulletin paper', 'article',
          'other', 'book', 'conference object', 'dataset', 'master thesis', 'conference poster',
          'periodical', 'still image', 'technical report']
boringfacs = ['110', '111', '112', '113', '114',
              '116', '117', '118', '119', '120',
              '122', '123', '124', '131', '132', '133', '134', '135', '136',
              '138', '139', '140', '156', '166', '181', '182', '190', '191', '192', '200', '300']
boringfacs += ['1150910', '1150920', '1150620', '1150625', '1150920', '1151120', '1151420']
interesting = ['doctoral thesis', 'thesis']

if skipalreadyharvested:
    alreadyharvested = ejlmod3.getalreadyharvested('TOKYO_U')

#tocurl = 'https://repository.dl.itc.u-tokyo.ac.jp/oai?verb=ListIdentifiers&metadataPrefix=oai_dc&from=' + stampofstartdate
tocurl = 'https://repository.dl.itc.u-tokyo.ac.jp/oai?verb=ListRecords&from=' + stampofstartdate + '&until=' + ejlmod3.stampoftoday() + '&metadataPrefix=jpcoar_1.0'
prerecs = {}
notcomplete = True
cls = 0
i = 0
rec= {'identifier' : ''}
while notcomplete:
    i += 1
    ejlmod3.printprogress('=', [[i, (cls-1)//100 + 1], [tocurl]])
    try:
        tocpage = BeautifulSoup(urllib.request.build_opener(urllib.request.HTTPCookieProcessor).open(tocurl), features="lxml")
    except:
        print('   try again after 60 seconds')
        time.sleep(60)
        tocpage = BeautifulSoup(urllib.request.build_opener(urllib.request.HTTPCookieProcessor).open(tocurl), features="lxml")
    j = 0
    for record in tocpage.find_all('record'):
        j += 1
        rec = {'tc' : 'T', 'jnl' : 'BOOK', 'note' : [], 'autaff' : []}
        category = 'none'
        keepit = True
        #deleted?
        for header in record.find_all('header', attrs = {'status' : 'deleted'}):
            keepit = False
            if verbose: print('   deleted')
        #identifier
        for identifier in record.find_all('identifier'):
            rec['identifier'] = identifier.text
        if ejlmod3.checkinterestingDOI(rec['identifier']):
            keepit = False
            if verbose: print('   uninterestingDOI')
        ejlmod3.printprogress('-', [[100*(i-1)+j, cls], [rec['identifier']]])
        #type
        for dt in record.find_all('dc:type'):
            category = dt.text
            if category in boring:
                if verbose: print('   skip "%s"' % (category))
                keepit = False
                ejlmod3.adduninterestingDOI(rec['identifier'])
            elif category == 'thesis':
                for dn in record.find_all('dcndl:degreename'):
                    dnt = dn.text
                    if verbose: print('   '+dnt)
                    if dnt in ['理学博士', '博士(理学)']:
                        category = 'doctoral thesis'
                    elif dnt in ['修士(工学)', '修士(理学)']:
                        if verbose: print('   skip "%s"' % (dnt))
                        keepit = False
                        ejlmod3.adduninterestingDOI(rec['identifier'])
                    else:
                        category = 'thesis_' + dn.text
        if verbose: print('   category: '+category)
        #title
        for dt in record.find_all('dc:title'):
            rec['tit'] = dt.text
        for dt in record.find_all('dcterms:alternative'):
            rec['transtit'] = dt.text
        #language
        for dl in record.find_all('dc:language'):
            rec['language'] = dl.text
        #date
        for dd in record.find_all('datacite:date', attrs = {'datetype' : 'Issued'}):
            rec['date'] = dd.text
            if re.search('[12]\d\d\d', rec['date']):
                rec['year'] = re.sub('.*([12]\d\d\d).*', r'\1', rec['date'])
                if int(rec['year']) < ejlmod3.year(backwards=years):
                    keepit = False
                    if verbose: print('   skip '+ rec['date'])
        #PIDs
        for pid in record.find_all('jpcoar:identifier', attrs = {'identifiertype' : 'HDL'}):
            rec['hdl'] = pid.text
            if skipalreadyharvested and rec['hdl'] in alreadyharvested:
                keepit = False
                print('  already in backup')
        for pid in record.find_all('jpcoar:identifier', attrs = {'identifiertype' : 'DOI'}):
            rec['doi'] = pid.text
            if skipalreadyharvested and rec['doi'] in alreadyharvested:
                keepit = False
                print('  already in backup')
        for pid in record.find_all('jpcoar:identifier', attrs = {'identifiertype' : 'URI'}):
            rec['link'] = pid.text
        #Nippon Decimal Classification
        for ndc in record.find_all('jpcoar:subject', attrs = {'subjectscheme' : 'NDC'}):
            ndt = ndc.text[:2]
            if ndt == '41':
                rec['fc'] = 'm'
            elif ndt == '44':
                rec['fc'] = 'a'
            elif not ndt == '42':
                if verbose: print('   skip NDC='+ndc.text)
                keepit = False
                ejlmod3.adduninterestingDOI(rec['identifier'])
        #pdf
        for jf in record.find_all('jpcoar:file'):
            for jm in jf.find_all('jpcoar:mimetype'):
                if jm.text == 'application/pdf':
                    for ju in jf.find_all('jpcoar:uri'):
                        rec['pdf_url'] = ju.text
        #author
        for jcn in record.find_all('jpcoar:creatorname'):
            rec['autaff'].append([ jcn.text, publisher ])
        #article page
        if keepit and 'link' in rec:
            if verbose: print(rec['link'])
            time.sleep(2)
            try:
                artpage = BeautifulSoup(urllib.request.build_opener(urllib.request.HTTPCookieProcessor).open(rec['link']), features="lxml")
            except:
                print('   wait 60s')
                time.sleep(60)
                artpage = BeautifulSoup(urllib.request.build_opener(urllib.request.HTTPCookieProcessor).open(rec['link']), features="lxml")
            for pre in artpage.body.find_all('pre', attrs = {'class' : 'hide'}):
                metadata = json.loads(pre.text)
                #abstract
                if 'item_4_description_5' in metadata.keys():
                    abstract = metadata['item_4_description_5']['attribute_value_mlt'][0]['subitem_description']
                    if re.search('the', abstract):
                        rec['abs'] = abstract
                #degree
                if 'item_7_select_21' in metadata.keys():
                    degree = metadata['item_7_select_21']['attribute_value_mlt'][0]['subitem_select_item']
                    if degree in 'master':
                        if verbose: print('   skip degree "%s"' % (degree))
                        ejlmod3.adduninterestingDOI(rec['identifier'])
                        keepit = False
                    else:
                        rec['note'].append('DEGREE:'+degree)
            #faculty
            for ols in artpage.body.find_all('ol', attrs = {'class' : 'breadcrumb'}):
                for a in ols.find_all('a'):
                    fac = re.sub('\D', '', a.text.strip())
                    if fac in boringfacs:
                        if verbose: print( '  skip fac=%s' % (a.text.strip()))
                        ejlmod3.adduninterestingDOI(rec['identifier'])
                        keepit = False
        if keepit:
            rec['artlink'] = 'https://repository.dl.itc.u-tokyo.ac.jp/records/' + re.sub('.*:0*', '', rec['identifier'])
            if category in prerecs:
                prerecs[category].append(rec)
            else:
                prerecs[category] = [rec]
            ejlmod3.printrecsummary(rec)
    notcomplete = False
    for rt in tocpage.find_all('resumptiontoken'):
        tocurl = 'https://repository.dl.itc.u-tokyo.ac.jp/oai?verb=ListRecords&metadataPrefix=jpcoar_1.0&resumptionToken=' + rt.text.strip()
        cls = int(rt['completelistsize'])
        notcomplete = True
    lp = [len(prerecs[category]) for category in prerecs]
    ejlmod3.printprogress('+', [[sum(lp), 100*i, cls], list(prerecs.keys()), lp])
    if 100*i >= cls:
        notcomplete = False
    time.sleep(2)

if 'doctoral thesis' in prerecs:
    jnlfilename = 'THESES-TOKYO_U-%s' % (ejlmod3.stampoftoday())
    ejlmod3.writenewXML(prerecs['doctoral thesis'], publisher, jnlfilename, retfilename='retfiles_special')

for category in prerecs:
    if category != 'doctoral thesis':
        jnlfilename = '%s-TOKYO_U-%s' % (re.sub('\W', '', category.upper()), ejlmod3.stampoftoday())
        try:
            ejlmod3.writenewXML(prerecs[category], publisher, jnlfilename)
        except:
            ejlmod3.writenewXML([], publisher, jnlfilename)


import sys

class Celsius:
    def __init__(self, temp = 0):
        self.temperature = temp

    def to_fahrenheit(self):
        return (self.__dict__['temperature'] * 1.8) + 32

    @property
    def temperature(self):
        print("Getting value")
        return (self.__dict__['temperature'])

    @temperature.setter
    def temperature(self, value):
        if value < -273:
            raise ValueError("Temperature below -273 is not possible")
        print("Setting value")
        self.__dict__['temperature'] = value

def main ():
    c = Celsius()
    print ("c.temperature:", c.temperature)
    c.temperature = 37
    print ("c.temperature:", c.temperature)
    fnht = c.to_fahrenheit()
    print ("fnht:", fnht)
    try:
        c.temperature = -300
    except ValueError:
        print("Temperature below -237 is not possible")
    sys.exit (0)
main ()


from amber import run_amber_parallel
import subprocess

def stripper(n_ground_snaps, n_atoms, ppn):
    last_line = int(n_atoms / 2)
    snap_restarts=[]
    snap_trajectories=[]
    pmemd_available = False
    for i in range(n_ground_snaps):
        file_in = "ground_snap." + str(i+1)
        file_out = "ground_stripped." + str(i+1) + ".rst"
        file_string = ""
        cpptraj_string = "parm m1.prmtop\n"\
                       "trajin " + file_in + "\n"\
                       "strip :CL3\n"\
                       "trajout " + file_out + " restart\n"\
                       "run\n"\
                       "quit"
        open("stripper.traj", 'w').write(cpptraj_string)
        subprocess.run(['cpptraj','-i','stripper.traj'])
        snap_restarts.append("ground_stripped."+str(i+1)+".rst")
        snap_trajectories.append("nasqm_abs_" + str(i + 1))
    parm_stripper = "parm m1.prmtop\n"\
                    "parmstrip :CL3\n"\
                    "parmwrite out stripped.prmtop\n"\
                    "run\n"\
                    "quit\n"
    open("parm_stripper.traj", 'w').write(parm_stripper)
    subprocess.run(['cpptraj','-i','parm_stripper.traj'])
    subprocess.run(['cp','m1.prmtop','solvent.prmtop'])
    subprocess.run(['cp','stripped.prmtop','m1.prmtop'])
    run_amber_parallel(pmemd_available, snap_trajectories, snap_restarts, number_processors=ppn)
    subprocess.run(['cp','solvent.prmtop','m1.prmtop'])


from PyQt4.QtGui import *
from PyQt4.QtCore import *
from mipsmachine import MipsMachine, Instruction, MipsException
from regwidget import RegWidget
from codewidget import CodeWidget
from memwidget import MemWidget
from iowidget import IOWidget
from mipscoder import MipsCoder
from settings import Settings

class MainWindow(QMainWindow):
	def __init__(self, parent = None):
		QMainWindow.__init__(self, parent)
		self.settings = Settings()
		self.setWindowTitle("MIPS Simulator")
		self.mips = MipsMachine()
		self.codewidget = CodeWidget(self)
		self.memwidget = MemWidget(self)
		self.regwidget = RegWidget(self)
		self.iowidget = IOWidget(self)
		self.statusBar().showMessage("Ready")
		self.runtimer = QTimer(self)
		self.runtimer.timeout.connect(self.runtimerTick)
		self.resize(self.settings.mainwindowwidth(), self.settings.mainwindowheight())
		self.move(self.settings.mainwindowx(), self.settings.mainwindowy())

		self.sidewidget = QSplitter()
		self.sidewidget.addWidget(self.regwidget)
		self.sidewidget.addWidget(self.memwidget)
		self.sidewidget.setOrientation(Qt.Vertical)

		self.topsplit = QSplitter()
		self.mainsplit = QSplitter()
		self.mainsplit.setOrientation(Qt.Vertical)
		self.grid = QVBoxLayout()
		self.grid.setSpacing(2)
		self.widget = QWidget(self)
		self.widget.setLayout(self.grid)
		self.setCentralWidget(self.widget)

		self.topsplit.addWidget(self.codewidget)
		self.topsplit.addWidget(self.sidewidget)
		self.topsplit.setSizes([self.settings.codewidgetsize(), self.settings.sidewidgetsize()])
		self.mainsplit.addWidget(self.topsplit)
		self.mainsplit.addWidget(self.iowidget)
		self.mainsplit.setSizes([self.settings.topwidgetsize(), self.settings.iowidgetsize()])
		self.grid.addWidget(self.mainsplit)
		self.grid.setContentsMargins(0, 0, 0, 0)

		self.openfilename = None
		self.newfile = True

		#prefpolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
		#self.setSizePolicy(prefpolicy)

		self.createMenuBar()
		self.createToolBar()
		self.populateMenuBar()
		self.openFile('tests/test1.s')

	def createMenuBar(self):
		menubar = self.menuBar()
		self.filemenu = menubar.addMenu('&File')
		self.editmenu = menubar.addMenu('&Edit')
		self.debugmenu = menubar.addMenu('&Debug')

	def populateMenuBar(self):
		exitaction = QAction("E&xit", self)
		self.filemenu.addAction(exitaction)

	def createToolBar(self):
		newaction = QAction(QIcon("icons/document-new.png"), 'New File', self)
		openaction = QAction(QIcon("icons/document-open.png"), 'Open File', self)
		saveaction = QAction(QIcon("icons/document-save.png"), 'Save File', self)
		undoaction = QAction(QIcon("icons/undo.png"), 'Undo', self)
		redoaction = QAction(QIcon("icons/redo.png"), 'Redo', self)
		newaction.triggered.connect(self.toolbarNew)
		openaction.triggered.connect(self.toolbarOpen)
		saveaction.triggered.connect(self.toolbarSave)

		uploadaction = QAction(QIcon("icons/processor.png"), '&Upload to Simulator', self)
		stepaction = QAction(QIcon("icons/arrow-step.png"), '&Step', self)
		runaction = QAction(QIcon("icons/control-run.png"), 'Upload && &Run', self)
		fastrunaction = QAction(QIcon("icons/control-double.png"), 'Upload && Run &Fast', self)
		stopaction = QAction(QIcon("icons/control-stop.png"), 'S&top', self)
		resetaction = QAction(QIcon("icons/control-reset.png"), '&Reset', self)
		uploadaction.triggered.connect(self.toolbarMipsUpload)
		stepaction.triggered.connect(self.toolbarMipsStep)
		runaction.triggered.connect(self.toolbarMipsRun)
		fastrunaction.triggered.connect(self.toolbarMipsRunFast)
		stopaction.triggered.connect(self.toolbarMipsStop)
		resetaction.triggered.connect(self.toolbarMipsReset)

		newaction.setShortcut(QKeySequence.New)
		openaction.setShortcut(QKeySequence.Open)
		saveaction.setShortcut(QKeySequence.Save)

		self.fileToolbar = self.addToolBar('File')
		self.fileToolbar.addAction(newaction)
		self.fileToolbar.addAction(openaction)
		self.fileToolbar.addAction(saveaction)
		self.fileToolbar.addSeparator()
		self.fileToolbar.addAction(undoaction)
		self.fileToolbar.addAction(redoaction)
		self.fileToolbar.addSeparator()
		self.debugToolbar = self.addToolBar('Debugging')
		self.debugToolbar.addAction(uploadaction)
		self.debugToolbar.addAction(runaction)
		self.debugToolbar.addAction(fastrunaction)
		self.debugToolbar.addAction(stopaction)
		self.debugToolbar.addSeparator()
		self.debugToolbar.addAction(stepaction)
		self.debugToolbar.addAction(resetaction)
		self.fileToolbar.setMovable(False)
		self.fileToolbar.setFloatable(False)
		self.debugToolbar.setMovable(False)
		self.debugToolbar.setFloatable(False)

		self.filemenu.addAction(newaction)
		self.filemenu.addAction(openaction)
		self.filemenu.addAction(saveaction)
		self.editmenu.addAction(undoaction)
		self.editmenu.addAction(redoaction)
		self.debugmenu.addAction(uploadaction)
		self.debugmenu.addAction(runaction)
		self.debugmenu.addAction(fastrunaction)
		self.debugmenu.addAction(stopaction)
		self.debugmenu.addSeparator()
		self.debugmenu.addAction(stepaction)
		self.debugmenu.addAction(resetaction)

	def reload(self):
		self.iowidget.reload()
		self.memwidget.update()
		self.regwidget.update()

	def confirmSaveChanges(self):
		return QMessageBox.question(self, "Unsaved Changes", "You have unsaved changes, would you like to save?", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel);

	def saveFile(self, filename):
		with open(filename, 'w') as f:
			f.write(self.codewidget.toPlainText())
		self.openfilename = filename
		self.newfile = False
		self.codewidget.changed = False

	def openFile(self, filename):
		with open(filename, 'r') as f:
			self.codewidget.setText(f.read())
		self.openfilename = filename
		self.newfile = False
		self.codewidget.changed = False

	def saveAsDialog(self):
		filename = QFileDialog.getSaveFileName(self, "Save file", "", "*.s")
		if filename != '':
			self.saveFile(filename)

	def toolbarNew(self, e):
		if self.codewidget.changed:
			response = self.confirmSaveChanges()
			if response == QMessageBox.Yes:
				self.toolbarSave(e)
			elif response == QMessageBox.No:
				self.codewidget.setText("")
			else:
				return
		else:
			self.codewidget.setText("")
		self.openfilename = None
		self.newfile = True
		self.codewidget.changed = False

	def toolbarOpen(self, e):
		if self.codewidget.changed:
			response = self.confirmSaveChanges()
			if response == QMessageBox.Yes:
				self.toolbarSave(e)
			elif response == QMessageBox.Cancel:
				return
		filename = QFileDialog.getOpenFileName(self, "Open file", "", "*.s")
		if filename != '':
			self.openFile(filename)

	def toolbarSave(self, e):
		if self.codewidget.changed and self.openfilename == None:
			self.saveAsDialog()
		elif self.codewidget.changed and self.openfilename != None:
			self.saveFile(self.openfilename)

	def mipsStep(self):
		self.mips.step()
		self.regwidget.stepupdate()
		self.reload()

	def toolbarMipsUpload(self, e):
		self.iowidget.outputmessage("Uploading to MIPS Processor...")
		try:
			instr = Instruction.fromstring(str(self.codewidget.toPlainText()))
			self.mips.load(instr)
			self.iowidget.outputmessage("Upload Successful")
		except MipsException as e:
			self.iowidget.outputmessage("%d: %s" % (e.lineno, e.message))
		self.iowidget.outputmessage("")
		self.reload()

	def toolbarMipsStep(self, e):
		self.mipsStep()

	def toolbarMipsRun(self, e):
		self.runtimer.start(100)

	def toolbarMipsRunFast(self, e):
		self.runtimer.start(10)

	def toolbarMipsStop(self, e):
		self.runtimer.stop()

	def toolbarMipsReset(self, e):
		self.runtimer.stop()
		self.mips.reset()
		self.regwidget.clearhotregs()
		self.reload()

	def runtimerTick(self):
		self.mipsStep()

	def closeEvent(self, e):
		self.memwidget.saveSettings(self.settings)
		self.settings.mainwindowwidth(self.width())
		self.settings.mainwindowheight(self.height())
		self.settings.mainwindowx(self.x())
		self.settings.mainwindowy(self.y())
		self.settings.topwidgetsize(self.mainsplit.sizes()[0])
		self.settings.iowidgetsize(self.mainsplit.sizes()[1])
		self.settings.codewidgetsize(self.topsplit.sizes()[0])
		self.settings.sidewidgetsize(self.topsplit.sizes()[1])


#!/usr/bin/env python
"""
Further ananlysis on simulation data and Code for reproducing fig4.
"""
import h5py
import matplotlib as mpl
mpl.rcParams.update({'pdf.fonttype' : 42,
                     'ps.fonttype' : 42,
                     'legend.frameon' : False,
                     'axes.spines.right' :  False,
                     'axes.spines.top' : False})
import matplotlib.pyplot as plt
import numpy as np
import pyximport
pyximport.install()
import os
import scipy.io
from scipy.stats import wilcoxon, ttest_rel, ttest_ind
import sys
from time import time, sleep
from threading import Thread

try:
    if __IPYTHON__:
        # this is used for debugging purposes only. allows to reload classes
        # when changed
        get_ipython().magic('load_ext autoreload')
        get_ipython().magic('autoreload 2')
except NameError:
    pass
#
from fiola.fiolaparams import fiolaparams
from fiola.fiola import FIOLA
from paper_reproduction.Fig7.Fig7_utilities import barplot_annotate_brackets, barplot_pvalue
from fiola.utilities import normalize, normalize_piecewise, match_spikes_greedy, compute_F1, load, signal_filter, extract_spikes
#sys.path.append('/media/nel/storage/Code/NEL_LAB/fiola/use_cases')
#sys.path.append(os.path.abspath('/Users/agiovann/SOFTWARE/fiola'))
#from use_cases.test_run_fiola import run_fiola # must be in use_cases folder
from paper_reproduction.Fig4_5.test_run_fiola import run_fiola
from paper_reproduction.utilities import multiple_dfs

#%%
mode = ['overlapping', 'non_overlapping', 'positron'][0]
dropbox_folder = '/media/nel/storage/NEL-LAB Dropbox/'
#dropbox_folder = '/Users/agiovann/Dropbox/'

if mode == 'overlapping':
    ROOT_FOLDER = dropbox_folder+'NEL/Papers/VolPy_online/data/voltage_data/simulation/overlapping'
    SAVE_FOLDER = dropbox_folder+'NEL/Papers/VolPy_online/result/test_simulations/overlapping'
    names = [f'viola_sim3_{i}' for i in range(1, 19)]
    #names = [f'viola_sim3_{i}' for i in range(4, 7)]
    #names = [f'viola_sim6_{i}' for i in range(2, 20, 3)]

elif mode == 'non_overlapping':
    ROOT_FOLDER = dropbox_folder+'NEL/Papers/VolPy_online/data/voltage_data/simulation/non_overlapping'
    SAVE_FOLDER = dropbox_folder+'NEL/Papers/VolPy_online/result/test_simulations/non_overlapping'
    #names = [f'viola_sim5_{i}' for i in range(1, 8)]
    names = [f'viola_sim5_{i}' for i in range(2, 8, 2)]
    #names = [f'viola_sim7_{i}' for i in range(2, 9)]

elif mode == 'positron':
    ROOT_FOLDER = dropbox_folder+'NEL/Papers/VolPy_online/data/voltage_data/simulation/test/sim4_positron'
    SAVE_FOLDER = dropbox_folder+'NEL/Papers/VolPy_online/result/test_simulations/test/sim4_positron'
    names = [f'viola_sim4_{i}' for i in range(1, 13)]
#%%
# for num_layers in [1, 3, 5, 10, 30]:
#     for trace_with_neg in [True, False]:
distance = [f'dist_{i}' for i in [1, 3, 5, 7, 10, 15]]
#names = [f'viola_sim3_{i}' for i in range(1, 19)]
#names = [f'viola_sim5_{i}' for i in range(2, 8, 2)]
#names = [f'viola_sim6_{i}' for i in range(2, 20, 3)]
#names = [f'viola_sim7_{i}' for i in range(2, 9)]
test = []
t_range = [10000, 75000]
for idx, dist in enumerate(distance):
    #if idx == 1:
    vi_result_all = []
    time_all = []
    spnr_all = []
    if mode == 'overlapping':
        #select = np.arange(idx * 3, (idx + 1) * 3)
        select = np.array([idx])
    else:
        select = np.array(range(len(names)))
    for name in np.array(names)[select]:
        folder = os.path.join(ROOT_FOLDER, name)
        gt_files = [file for file in os.listdir(folder) if 'SimResults' in file]
        gt_file = gt_files[0]
        gt = scipy.io.loadmat(os.path.join(folder, gt_file))
        gt = gt['simOutput'][0][0]['gt']
        spikes = gt['ST'][0][0][0]

        vi_folder = os.path.join(folder, 'viola')
        vi_files = sorted([file for file in os.listdir(vi_folder) if 'v3.3' in file and 'layers_3_' in file ])
        #vi_files = sorted([file for file in os.listdir(vi_folder) if 'v3.0' in file and 'layers_1' in file])
        #vi_files = sorted([file for file in os.listdir(vi_folder) if 'v3.0' in file and f'layers_{num_layers}_' in file and f'trace_with_neg_{trace_with_neg}' in file])
        if len(vi_files) > 1:
            raise Exception('number of files greater than one')
        vi_file = vi_files[0]
        vi = np.load(os.path.join(vi_folder, vi_file), allow_pickle=True).item()

        time_all.append(np.mean(np.diff(vi.timing_online)))

        #vi_spatial = vi.H.copy()
        vi_temporal = vi.t_s.copy()
        vi_spikes = np.array([np.array(sorted(list(set(sp)-set([0])))) for sp in vi.index])[np.argsort(vi.seq)]

        n_cells = spikes.shape[0]
        vi_result = {'F1':[], 'precision':[], 'recall':[]}
        rr = {'F1':[], 'precision':[], 'recall':[]}
        for idx in range(n_cells):
            s1 = spikes[idx].flatten()
            s1 = s1 - 1     # matlab first element is 1
            s1 = s1[np.logical_and(s1>t_range[0], s1<t_range[1])]
            s2 = vi_spikes[idx]
            s2 = s2[np.logical_and(s2>t_range[0], s2<t_range[1])]
            idx1_greedy, idx2_greedy = match_spikes_greedy(s1, s2, max_dist=4)
            F1, precision, recall = compute_F1(s1, s2, idx1_greedy, idx2_greedy)
            rr['F1'].append(F1)
            rr['precision'].append(precision)
            rr['recall'].append(recall)
        vi_result_all.append(rr)

        spnr = []
        for idx in range(n_cells):
            s1 = spikes[idx].flatten()
            s1 = s1 - 1
            s1 = s1[np.logical_and(s1>t_range[0], s1<t_range[1])]
            s2 = vi_spikes[idx]
            s2 = s2[np.logical_and(s2>t_range[0], s2<t_range[1])]
            t2 = vi_temporal[idx]
            t2 = normalize_piecewise(t2, 5000)
            ss = np.mean(t2[s1])
            spnr.append(ss)
        spnr_all.append(spnr)

    folder_name = vi_file[:-4]
    try:
        os.makedirs(os.path.join(SAVE_FOLDER, folder_name))
        print('make folder')
    except:
        print('already exist')
    #np.save(os.path.join(SAVE_FOLDER, folder_name, f'viola_result_{t_range[0]}_{t_range[1]}'), vi_result_all)
    np.save(os.path.join(SAVE_FOLDER, folder_name, f'viola_result_{t_range[0]}_{t_range[1]}_timing'), time_all)

    # np.save(os.path.join(SAVE_FOLDER, folder_name, f'viola_result_spnr_{t_range[0]}_{t_range[1]}_v3.0'), spnr_all)
    # print(len(vi_result_all))
    #np.save(os.path.join(SAVE_FOLDER, folder_name,  f'viola_result_{t_range[0]}_{t_range[1]}_{dist}'), vi_result_all)
    ##np.save(os.path.join(SAVE_FOLDER, folder_name, f'viola_result_spnr_{t_range[0]}_{t_range[1]}_v2.0_without_shrinking'), spnr_all)
    test.append(np.mean(vi_result_all[0]['F1']))

#%%
#'t_' means tested algorithm
a_idx = 2 # algorithm idx
algo = ['volpy', 'suite2p', 'meanroi', 'meanroi_online'][a_idx]
thresh_list = np.arange(2.0, 4.1, 0.1)
best_thresh = [False, True, True, False][a_idx]

if mode == 'overlapping':
    t_range = [10000, 20000]
    names = [f'viola_sim3_{i}' for i in range(1, 19)]
    distance = [f'dist_{i}' for i in [1, 3, 5, 7, 10, 15]]
elif mode == 'non_overlapping':
    t_range = [10000, 75000]
    names = [f'viola_sim5_{i}' for i in range(1, 8)]
    distance = [0]

save_folder = os.path.join(SAVE_FOLDER, f'{algo}_result_v3.1')
try:
    os.makedirs(save_folder)
    print('make folder')
except:
    print('already exist')

for ii, dist in enumerate(distance):
    t_result_all = []
    spnr_all = []
    best_thresh_all = []

    if mode == 'overlapping':
        select = np.arange(ii * 3, (ii + 1) * 3)
    else:
        select = np.array(range(len(names)))

    for name in np.array(names)[select]:
        print(f'now processing {name}')
        folder = os.path.join(ROOT_FOLDER, name)
        gt_files = [file for file in os.listdir(folder) if 'SimResults' in file]
        gt_file = gt_files[0]
        gt = scipy.io.loadmat(os.path.join(folder, gt_file))
        gt = gt['simOutput'][0][0]['gt']
        spikes = gt['ST'][0][0][0]
        temporal = gt['trace'][0][0][:,:,0]
        path_ROIs = os.path.join(folder, 'viola', 'ROIs_gt.hdf5')
        with h5py.File(path_ROIs,'r') as h5:
            spatial = np.array(h5['mov'])
        plt.imshow(spatial.sum(0))

        #summary_file = os.listdir(os.path.join(folder, 'volpy'))
        #summary_file = [file for file in summary_file if 'summary' in file][0]
        #summary = cm.load(os.path.join(folder, 'volpy', summary_file))

        if algo == 'meanroi' or 'meanroi_online':
            movie_file = [file for file in os.listdir(folder) if 'hdf5' in file and 'flip' not in file][0]
            mov = load(os.path.join(folder, movie_file))
            mov = mov.reshape([mov.shape[0], -1], order='F')
            spatial_F = [np.where(sp.reshape(-1, order='F')>0) for sp in spatial]
            t_temporal = np.array([-mov[:, sp].mean((1,2)) for sp in spatial_F])
            t_spatial = spatial

        elif algo == 'volpy':
            t_folder = os.path.join(folder, 'volpy')
            t_files = sorted([file for file in os.listdir(t_folder) if 'adaptive_threshold' in file])
            t_file = t_files[0]
            t = np.load(os.path.join(t_folder, t_file), allow_pickle=True).item()
            t_spatial = t['weights'].copy()
            t_temporal = t['ts'].copy()
            t_ROIs = t['ROIs'].copy()
            t_ROIs = t_ROIs * 1.0
            t_templates = t['templates'].copy()
            t_spikes = t['spikes'].copy()

        if not best_thresh:
            print('not finding best thresh')
            n_cells = t_spatial.shape[0]
            t_result = {'F1':[], 'precision':[], 'recall':[]}
            rr = {'F1':[], 'precision':[], 'recall':[]}

            if algo == 'meanroi_online':
                from fiola.signal_analysis_online import SignalAnalysisOnlineZ
                saoz = SignalAnalysisOnlineZ(mode='voltage', window=10000, step=5000, detrend=True, flip=False,
                                             do_scale=False, template_window=2, robust_std=False, adaptive_threshold=True, fr=400, freq=15,
                                             minimal_thresh=3.0, online_filter_method = 'median_filter', filt_window = 15, do_plot=False)
                saoz.fit(t_temporal[:, :t_range[0]], num_frames=t_temporal.shape[1])
                for n in range(t_range[0], t_temporal.shape[1]):
                    saoz.fit_next(t_temporal[:, n: n+1], n)
                t_spikes = [np.unique(saoz.index[idxx]) for idxx in range(len(saoz.index))]

            for idx in range(n_cells):
                s1 = spikes[idx].flatten()
                s1 = s1 - 1
                s1 = s1[np.logical_and(s1>t_range[0], s1<t_range[1])]
                s2 = t_spikes[idx]
                s2 = s2[np.logical_and(s2>t_range[0], s2<t_range[1])]
                idx1_greedy, idx2_greedy = match_spikes_greedy(s1, s2, max_dist=4)
                F1, precision, recall= compute_F1(s1, s2, idx1_greedy, idx2_greedy)
                rr['F1'].append(F1)
                rr['precision'].append(precision)
                rr['recall'].append(recall)
            t_result_all.append(rr)
        else:
            print('find best thresh')
            from scipy.signal import medfilt
            n_cells = t_spatial.shape[0]
            t_result = {'F1':[], 'precision':[], 'recall':[]}
            t_result_0 = {'F1':[], 'precision':[], 'recall':[]}
            rr = {'F1':[], 'precision':[], 'recall':[]}
            for thresh in thresh_list:
                print(thresh)
                rr = {'F1':[], 'precision':[], 'recall':[]}
                t_temporal_p = np.array([tt - medfilt(tt, kernel_size=15) for tt in t_temporal])
                t_spikes = extract_spikes(t_temporal_p, threshold=thresh)

                for idx in range(n_cells):
                    s1 = spikes[idx].flatten()
                    s1 = s1 - 1
                    s1 = s1[np.logical_and(s1>t_range[0], s1<t_range[1])]
                    s2 = t_spikes[idx]
                    s2 = s2[np.logical_and(s2>t_range[0], s2<t_range[1])]
                    idx1_greedy, idx2_greedy = match_spikes_greedy(s1, s2, max_dist=4)
                    F1, precision, recall = compute_F1(s1, s2, idx1_greedy, idx2_greedy)
                    rr['F1'].append(F1)
                    rr['precision'].append(precision)
                    rr['recall'].append(recall)
                if not t_result['F1']:
                    t_result = rr
                    best_thresh = thresh
                    new_F1 = np.array(rr['F1']).sum()/n_cells
                    print(f'update: thresh:{thresh}, best_F1:{new_F1}, new_F1:{new_F1}')
                else:
                    best_F1 = np.array(t_result['F1']).sum()/n_cells
                    new_F1 = np.array(rr['F1']).sum()/n_cells
                    #print(new_F1)
                    if new_F1 > best_F1:
                        print(f'update: thresh:{thresh}, best_F1:{best_F1}, new_F1:{new_F1}')
                        t_result = rr
                        best_thresh = thresh

            t_result_all.append(t_result)

        if mode == 'non_overlapping':
            spnr = []
            for idx in range(n_cells):
                s1 = spikes[idx].flatten()
                s1 = s1 - 1
                s1 = s1[np.logical_and(s1>t_range[0], s1<t_range[1])]
                t2 = t_temporal_p[idx]
                t2 = normalize_piecewise(t2, 5000)
                ss = np.mean(t2[s1])
                spnr.append(ss)
            spnr_all.append(spnr)

    # if mode == 'non_overlapping':
    #     np.save(os.path.join(save_folder, f'{algo}_F1_{t_range[0]}_{t_range[1]}_v3.1'), t_result_all)
    #     np.save(os.path.join(save_folder, f'{algo}_spnr_{t_range[0]}_{t_range[1]}_v3.1'), spnr_all)
    # elif mode == 'overlapping':
    #     np.save(os.path.join(save_folder, f'{algo}_F1_{t_range[0]}_{t_range[1]}_{dist}'), t_result_all)

##################################################################################################################
##################################################################################################################


import pytest

from wattson.data import instance_data, region_data
from wattson.estimator import (
    calculate_scope_2,
    calculate_scope_3,
    compute_load_to_kwh,
    estimate_carbon_emissions,
)

SAMPLE_INSTANCE = instance_data["db.m4.10xlarge"]
SAMPLE_REGION = region_data["eu-north-1"]

def test_calculate_scope_2() -> None:
    with_PUE = 245.6 * 1.2
    expected_result = with_PUE / 1000 * 1 * 8
    result = calculate_scope_2(
        instance=SAMPLE_INSTANCE, region=SAMPLE_REGION, hours=1, load=50
    )
    assert result == expected_result
    # Cross-validate with a known example from Teads calculator
    assert round(result, 1) == 2.4

def test_calculate_scope_3() -> None:
    result = calculate_scope_3(instance=SAMPLE_INSTANCE, hours=1)
    # Cross-validate with a known example from Teads calculator
    assert result == 34.5

def test_lerp() -> None:
    assert round(compute_load_to_kwh(SAMPLE_INSTANCE.load_to_kwh, 25)) == 190

@pytest.mark.parametrize(
    ("region", "has_compensation", "expected_emissions"),
    [
        ("ca-central-1", True, 14.1),
        ("eu-north-1", False, 11.3),
        ("ap-northeast-1", False, 22.8),
    ],
)
def test_complete_estimate(
    region: str, has_compensation: bool, expected_emissions: float
) -> None:
    instance_type = "a1.large"
    hours = 3
    load = 50

    result = estimate_carbon_emissions(
        instance_type=instance_type,
        hours=hours,
        load_percentage=load,
        region=region,
    )

    # Cross-validate with a known example from Teads calculator
    assert round(result.scope_2_co2eq + result.scope_3_co2eq, 1) == expected_emissions
    assert (
        "has been compensated by renewable energy credits" in result.details
    ) == has_compensation
    assert result.compensated == has_compensation

# fmt: off
@pytest.mark.parametrize("value_set", [
    dict(region="xx-mars", instance_type="db.m4.10xlarge", hours=1),
    dict(region="eu-north-1", instance_type="deep.thought", hours=1),
    dict(region="eu-north-1", instance_type="db.m4.10xlarge", hours=-1),
    dict(region="eu-north-1", instance_type="db.m4.10xlarge", hours=1, load_percentage=1500),  # noqa: E501
])
# fmt: on
def test_nonsensical_values(value_set: dict) -> None:  # type: ignore[type-arg]
    with pytest.raises(ValueError):
        estimate_carbon_emissions(**value_set)


from simple_rl.tasks import GridWorldMDP

class GridWorldAttacker(GridWorldMDP):
    """
    Default values for GridWorldMDP:
    width=5, height=3, init_loc=(1, 1), rand_init=False, goal_locs=[()], lava_locs=[()],
    walls=[], is_goal_terminal=True, is_lava_terminal=False, gamma=0.99, slip_prob=0.0,
    step_cost=0.0, lava_cost=1.0, name="gridworld"):
    """
    def __init__(self, slip_prob=0.0):
        super().__init__(
            name="Attacker",
            width=5,
            height=5,
            init_loc=(1, 1),
            goal_locs=[(5, 5)],
            slip_prob=slip_prob)

    def __str__(self):
        return self.name


from operator import add, sub, mul

import ast
import unittest

TYPE_TO_FUNCTION = {
    ast.Add: add,
    ast.Sub: sub,
    ast.Mult: mul,
}

def evaluate(node):
    if type(node) == ast.Expression:
        return evaluate(node.body)
    elif type(node) == ast.BinOp:
        a = evaluate(node.left)
        b = evaluate(node.right)
        op_type = type(node.op)
        if op_type not in TYPE_TO_FUNCTION:
            raise ValueError(f'unsupported op type {node.op}')
        op_function = TYPE_TO_FUNCTION[op_type]
        return op_function(a, b)
    elif type(node) == ast.Constant:
        return node.value
    else:
        raise ValueError(f'unsupported node type {node}')

class TestEvaluate(unittest.TestCase):
    def test_evaluate(self):
        test_cases = [
            ('1 + 1', 2),
            ('1 + 2 * 3', 7),
            ('1 + 2 * (3 - 4)', -1),
            ('1 + (2 - 3) * 4 + 5', 2),
        ]
        for s, expected in test_cases:
            expr = ast.parse(s, mode='eval')
            # Uncomment to print AST for expression
            # print(ast.dump(expr, indent=2))
            actual = evaluate(expr)
            if actual != expected:
                raise AssertionError(f'"{s}": expected {expected}, got {actual}')

if __name__ == '__main__':
    unittest.main()


"""
17276 배열 돌리기
"""
import sys
input = sys.stdin.readline

def save_value(x: list):
    temp = [[] for _ in range(4)]
    for i in range(len(x)):
        for j in range(len(x)):
            if i == j:
                temp[0].append(x[i][j])  # 우하향대각선
            if i + j == len(x) - 1:
                temp[1].append(x[i][j])  # 좌하향대각선
    for i in range(len(x)):
        temp[2].append(x[len(x) // 2][i])  # 중앙 가로
        temp[3].append(x[i][len(x) // 2])  # 중앙 세로
    return temp

def clockWise_rotate(x: list):
    temp = save_value(x)
    for i in range(len(x)):
        for j in range(len(x)):
            if i + j == n - 1:
                x[i][j] = temp[3][i]
            if i == j:
                x[i][j] = temp[2][i]
    temp[1].reverse()
    for i in range(len(x)):
        x[len(x) // 2][i] = temp[1][i]
        x[i][len(x) // 2] = temp[0][i]
    return x

def counterClockWise_rotate(x: list):
    temp = save_value(x)
    temp[2].reverse()
    for i in range(n):
        for j in range(n):
            if i + j == n - 1:
                x[i][j] = temp[2][i]
            if i == j:
                x[i][j] = temp[3][i]

    for i in range(n):
        x[len(x) // 2][i] = temp[0][i]
        x[i][len(x) // 2] = temp[1][i]
    return x

t = int(input())
for _ in range(t):
    n, d = map(int, input().split())
    ls = [list(map(int, input().split())) for _ in range(n)]
    total = d // 45
    if total < 0:
        for i in range(abs(total)):
            ls = counterClockWise_rotate(ls)
    else:
        for i in range(total):
            ls = clockWise_rotate(ls)

    for i in ls:
        print(*i)


from aiogram.dispatcher.storage import BaseStorage, FSMContext

class MemoryStorage(BaseStorage):
    def __init__(self):
        self._data = {}

    async def close(self):
        self._data.clear()

    async def wait_closed(self):
        pass

    async def get_state(self, chat, user=None, default=None):
        key = self.__get_key(chat, user)
        return self._data.get(key, {}).get('state', default)

    async def set_state(self, state, chat, user=None):
        key = self.__get_key(chat, user)
        if key not in self._data:
            self._data[key] = {}
        self._data[key]['state'] = state

    async def update_data(self, chat, user, data):
        key = self.__get_key(chat, user)
        if key not in self._data:
            self._data[key] = {}
        self._data[key]['data'] = data

    async def get_data(self, chat, user=None, default=None):
        key = self.__get_key(chat, user)
        return self._data.get(key, {}).get('data', default) or {}

    async def reset_data(self, chat, user=None):
        key = self.__get_key(chat, user)
        if key in self._data:
            del self._data[key]

    @staticmethod
    def __get_key(chat, user=None):
        if user:
            return f"{chat}:{user}"
        return f"{chat}"


from app.models import *

def login_required(user):
    if user.is_anonymous():
        return False
    return True

def is_teacher(user):
    if Teacher.objects.filter(user_id=user.id).count() == 1:
        teacher = Teacher.objects.filter(user_id=user.id).first()
        times = Teach.objects.filter(teacher_id=teacher).count()
        students = Take.objects.filter(section_id__teach__teacher_id=teacher).count()
        levels = Level.objects.all().order_by('-level')
        for level in levels:
            if (students >= level.students) and (times >= level.times):
                teacher.level = level.level
                teacher.save()
                break
        return teacher
    return False


__author__ = 'kp14'

import subprocess

from atom.api import Atom, Unicode, List

from helpers import env

class CustomBlastSearch(Atom):
    """Represents parameters of a BLAST search.

    """
    fasta = Unicode()
    db = Unicode()
    available_dbs = List()

    def populate(self):
        try:
            paths = env.get_db_locations()
            result = ''
            for path in paths:
                print(path)
                if len(path) > 1:
                    dbs = subprocess.check_output(['blastdbcmd', '-list', path])
                    result += dbs
            raw_dbs = result.split('\n')
            # each line looks like: F:\path\to\db\db_name db_type -> db_name
            self.available_dbs = [os.path.basename(x.split()[0]) for x in raw_dbs[:-1]]
        except TypeError as e:  # paths is None
            # need to return this as a list so that it can populate the drop-down menu
            self.available_dbs = ['None, check variable: {}'.format(e.message)]

    def run(self):
        print(self.fasta)
        print(self.db)
        try:
            result = subprocess.check_output(['blastp',
                                              '-query', self.fasta,
                                              '-db', self.db,
                                              '-out', './tmp/blast.out'],
                                             stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            result = e.output
        print(unicode(result))


import os
from scipy.io import loadmat
from scipy.spatial.transform import Rotation as R
from PIL import Image
import numpy as np

import torch
from torch.utils.data import Dataset
from torchvision.transforms import functional as F
from torchvision.ops import roi_align

import matplotlib.pyplot as plt

from pytorch3d.io import load_objs_as_meshes, load_obj
from pytorch3d.transforms import quaternion_apply, Translate

class Stage2Dataset(Dataset):
	def __init__(self, image_list="./data/images.csv",
					   object_list="./data/objects.csv",
					   model_dir="./data",
					   transforms=None):
		self.N = 500
		self.N_mesh = 500
		self.image_list = [ln.strip().split(',') for ln in open(image_list,'r').readlines()]
		self.object_counts = np.cumsum(np.array([int(ln[-1]) for ln in self.image_list]))
		self.object_list = [ln.strip().split(',') for ln in open(object_list,'r').readlines()]

		self.object_lookup_id = {obj[1]: int(obj[0]) for obj in self.object_list}
		self.object_lookup_name = {int(obj[0]): obj[1] for obj in self.object_list}

		self.transforms = transforms

		self.intrinsic = np.array([[902.187744140625, 0.0, 662.3499755859375],
						  [0.0, 902.391357421875, 372.2278747558594],
						  [0.0, 0.0, 1.0]])

		self.obj_filenames = []
		for label_ in sorted(self.object_lookup_name.keys()):
			obj_filename = os.path.join(model_dir, "model", self.object_lookup_name[label_], self.object_lookup_name[label_]+".obj")
			self.obj_filenames.append(obj_filename)

		# Load obj file
		self.meshes = load_objs_as_meshes(self.obj_filenames, device=torch.device('cpu'))

		self.diameters = []
		for mesh in self.meshes:
			obj_cld = mesh.verts_padded()[0]
			obj_center = (obj_cld.min(0).values + obj_cld.max(0).values) / 2.0
			obj_cld = obj_cld - obj_center
			obj_diameter = torch.max(torch.linalg.norm(obj_cld, axis=1)) * 2
			self.diameters.append(obj_diameter)

	def __len__(self):
		return self.object_counts[-1]

	def __getitem__(self, idx):
		img_idx = np.argmax(idx<self.object_counts)
		_, scene_path, imgid, _ = self.image_list[img_idx]

		idx_offset = idx
		if img_idx>0:
			idx_offset = idx-self.object_counts[img_idx-1]

		color_path = os.path.join(scene_path, imgid+'-color.png')
		mask_path = os.path.join(scene_path, imgid+'-label.png')
		normal_path = os.path.join(scene_path, imgid+'-normal_true.png')
		plane_path = os.path.join(scene_path, imgid+'-tableplane_depth.png')
		depth_path = os.path.join(scene_path, imgid+'-depth_true.png')
		meta_path = os.path.join(scene_path, imgid+'-meta.mat')

		color = Image.open(color_path).convert("RGB")
		mask = Image.open(mask_path)
		normal = Image.open(normal_path)
		plane = np.array(Image.open(plane_path))
		depth = np.array(Image.open(depth_path))
		meta = loadmat(meta_path)

		depth = Image.fromarray(depth/meta['factor_depth'].item())
		plane = Image.fromarray(plane/meta['factor_depth'].item())

		mask = np.array(mask)
		obj_ids = meta['cls_indexes'].flatten()
		masks = (mask == obj_ids[:, None, None])[idx_offset]
		obj_ids = obj_ids[idx_offset]

		pos = np.where(masks)
		xmin = np.min(pos[1])
		xmax = np.max(pos[1])
		ymin = np.min(pos[0])
		ymax = np.max(pos[0])

		boxes = torch.as_tensor([[xmin, ymin, xmax, ymax]], dtype=torch.float32)
		labels = torch.as_tensor([obj_ids], dtype=torch.int64)
		masks = torch.as_tensor(masks, dtype=torch.uint8)

		area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])

		target = {}
		target["boxes"] = boxes
		target["labels"] = labels
		target["masks"] = masks
		target["image_id"] = torch.tensor([img_idx])
		target["area"] = area
		target["iscrowd"] = torch.zeros((1,), dtype=torch.int64)

		if self.transforms is not None:
			(color,normal,plane,depth), target = self.transforms((color,normal,plane,depth), target)

		H, W = depth.shape[1:]
		U, V = np.tile(np.arange(W), (H, 1)), np.tile(np.arange(H), (W, 1)).T
		fx, fy, cx, cy = self.intrinsic[0, 0], self.intrinsic[1, 1], self.intrinsic[0, 2], self.intrinsic[1, 2]
		X, Y = depth * (U - cx) / fx, depth * (V - cy) / fy

		X = F.convert_image_dtype(X)
		Y = F.convert_image_dtype(Y)
		Z = F.convert_image_dtype(depth)
		uvz = torch.stack([X,Y,Z]).squeeze(1)

		crops_color = roi_align(color.unsqueeze(0), [target['boxes']], (80,80), 1, 1)
		crops_normal = roi_align(normal.unsqueeze(0), [target['boxes']], (80,80), 1, 1)
		crops_plane = roi_align(plane.unsqueeze(0), [target['boxes']], (80,80), 1, 1)
		crops_uvz = roi_align(uvz.unsqueeze(0), [target['boxes']], (80,80), 1, 1)
		crops_masks = roi_align(masks.unsqueeze(0).unsqueeze(0).type(torch.float32), [target['boxes']], (80,80), 1, 1)
		crops_geometry_per_image = torch.cat([crops_normal, crops_plane, crops_uvz], dim=1)

		poses = np.transpose(meta['poses'], (2,0,1))
		target["poses"] = torch.from_numpy(poses)[idx_offset].unsqueeze(0)
		target["quats"] = torch.stack([torch.from_numpy(R.from_matrix(rmat[:, :3]).as_quat()) for rmat in poses])[:,[3,0,1,2]][idx_offset].unsqueeze(0)
		target["trans_gt"] = torch.stack([torch.from_numpy(rmat[:, 3]) for rmat in poses])[idx_offset].unsqueeze(0)
		target["trans"] = target["trans_gt"].view(1,3,1,1) - crops_uvz
		target["trans"] = target["trans"] / torch.linalg.vector_norm(target["trans"], dim=1, keepdim=True)

		mesh = self.meshes[list(target['labels']-1)].clone()
		mesh_pre_rot = mesh.verts_padded()
		mesh = mesh.update_padded(quaternion_apply(target['quats'].unsqueeze(1).type(torch.float32), mesh.verts_padded()))
		mesh_post_rot = mesh.verts_padded()

		mesh_samples = np.random.choice(range(mesh_pre_rot.shape[1]), size=self.N_mesh)

		target["mesh"] = mesh_pre_rot[:,mesh_samples]
		target["mesh_rot"] = mesh_post_rot[:,mesh_samples]
		target["diameter"] = self.diameters[obj_ids-1]

		return color, crops_color, crops_geometry_per_image, crops_masks, target


import pygame
import os
import random
import neat
import pickle
import pygame_menu

GAME_WIDTH = 1100
GAME_HEIGHT = 700
clock = pygame.time.Clock()
pygame.font.init()
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
GAME_WINDOW = pygame.display.set_mode((GAME_WIDTH, GAME_HEIGHT))
pygame.display.set_icon(pygame.image.load("res/icon.png"))
pygame.display.set_caption("AI CAR GAME")
STAT_FONT = pygame.font.SysFont("comicsans", 30)
GEN = 0
MODE = 0

class Background:
    BG_IMG = pygame.image.load('res/bg.png')
    bgImages = [BG_IMG, BG_IMG]

    def __init__(self, y1, x, y2, animationSpeed):
        self.y1 = y1
        self.x = x
        self.y2 = y2
        self.animationSpeed = animationSpeed

    def drawBackground(self, gameWindow):

        self.y1 += self.animationSpeed
        self.y2 += self.animationSpeed

        if self.BG_IMG.get_height() + self.y1 > 2 * GAME_HEIGHT:
            self.y1 = self.y2 - self.BG_IMG.get_height()
        if self.BG_IMG.get_height() + self.y2 > 2 * GAME_HEIGHT:
            self.y2 = self.y1 - self.BG_IMG.get_height()

        gameWindow.blit(self.bgImages[0], (0, self.y1))
        gameWindow.blit(self.bgImages[1], (0, self.y2))

class Wall:
    GAP = 150
    WALL_IMG = pygame.image.load('res/wall.png')

    def __init__(self, y, animationSpeed):
        self.y = y
        self.LEFT_WALL = self.WALL_IMG
        self.RIGHT_WALL = pygame.transform.flip(self.WALL_IMG, True, False)
        self.leftX = 0
        self.rightX = 0
        self.passed = False
        self.width = 0
        self.animationSpeed = animationSpeed
        self.set_width()

    def set_width(self):
        self.width = random.randrange(50, 950)
        self.leftX = self.width - self.WALL_IMG.get_width()
        self.rightX = self.leftX + self.WALL_IMG.get_width() + self.GAP

    def move(self):
        self.y += self.animationSpeed

    def drawWalls(self, gameWindow):
        gameWindow.blit(self.WALL_IMG, (self.leftX, self.y))
        gameWindow.blit(self.WALL_IMG, (self.rightX, self.y))

    def collide(self, playerCar):
        carMask = playerCar.getMask()
        leftWallMask = pygame.mask.from_surface(self.LEFT_WALL)
        rightWallMask = pygame.mask.from_surface(self.RIGHT_WALL)

        leftOffset = (self.leftX - playerCar.x, self.y - playerCar.y)
        rightOffset = (self.rightX - playerCar.x, self.y - playerCar.y)

        leftCollisionPoint = carMask.overlap(leftWallMask, leftOffset)
        rightCollisionPoint = carMask.overlap(rightWallMask, rightOffset)

        if leftCollisionPoint or rightCollisionPoint:
            return True
        else:
            return False

class Player:
    PLAYER_IMG = pygame.image.load('res/car.png')

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.xVel = 20

    def drawCar(self, gameWindow):
        gameWindow.blit(self.PLAYER_IMG, (self.x, self.y))

    def moveRight(self):
        if self.x + self.PLAYER_IMG.get_width() < GAME_WIDTH:
            self.x += self.xVel

    def moveLeft(self):
        if self.x > 0:
            self.x -= self.xVel

    def getMask(self):
        return pygame.mask.from_surface(self.PLAYER_IMG)

def drawGame(gameWindow, bg, walls, score, cars):
    bg.drawBackground(gameWindow)
    for wall in walls:
        wall.drawWalls(gameWindow)
    for car in cars:
        car.drawCar(gameWindow)
    scoreText = STAT_FONT.render("Score: " + str(score), 1, (255, 255, 0))
    quitText = STAT_FONT.render("ESC to stop", 1, (0, 0, 0))
    gameWindow.blit(scoreText, (GAME_WIDTH - 30 - scoreText.get_width(), 10))
    gameWindow.blit(quitText, (20, 10))
    pygame.display.update()

def drawGameAILearning(gameWindow, bg, cars, walls, score, gen):
    bg.drawBackground(gameWindow)
    for wall in walls:
        wall.drawWalls(gameWindow)
    for car in cars:
        car.drawCar(gameWindow)
        pygame.draw.line(gameWindow, (255, 255, 0), (int(car.x + car.PLAYER_IMG.get_width() // 2), car.y),
                         (walls[0].rightX, walls[0].y), 1)
        pygame.draw.line(gameWindow, (255, 255, 0), (int(car.x + car.PLAYER_IMG.get_width() // 2), car.y),
                         (walls[0].rightX - walls[0].GAP, walls[0].y), 1)
    scoreText = STAT_FONT.render("Score: " + str(score), 1, (0, 0, 0))
    quitText = STAT_FONT.render("ESC to stop", 1, (0, 0, 0))
    generation = STAT_FONT.render("Gen: " + str(gen), 1, (0, 0, 0))
    alive = STAT_FONT.render("Alive: " + str(len(cars)), 1, (0, 0, 0))
    gameWindow.blit(scoreText, (GAME_WIDTH - 30 - scoreText.get_width(), 10))
    gameWindow.blit(generation, (20, 10))
    gameWindow.blit(alive, (20, 35))
    gameWindow.blit(quitText, (20, 60))
    pygame.display.update()

def eval_genomes(genomes, config):
    global GEN
    GEN += 1
    cars = []
    nets = []
    ge = []
    animationSpeed = 10
    bg = Background(0, 0, GAME_HEIGHT, 10)
    walls = [Wall(0, animationSpeed)]
    score = 0

    for genome_id, genome in genomes:
        genome.fitness = 0
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        nets.append(net)
        cars.append(Player(GAME_WIDTH // 2, GAME_HEIGHT - 125))
        ge.append(genome)

    addWall = False
    running = True
    while running:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                quit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_ESCAPE]:
            ge[0].fitness = 2000
            cars.clear()
            GEN = 0

        wallInd = 0
        if len(cars) > 0:
            if len(walls) > 1 and cars[0].y > walls[0].y + walls[0].RIGHT_WALL.get_height():
                wallInd = 1
        else:
            break

        for x, car in enumerate(cars):
            ge[x].fitness += 0.1

            output = nets[x].activate((animationSpeed,
                                       (car.x + car.PLAYER_IMG.get_width() // 2)
                                       - (walls[wallInd].rightX - walls[wallInd].GAP),
                                       walls[wallInd].rightX - (car.x + car.PLAYER_IMG.get_width() // 2)))
            if output[0] > 0.8:
                car.moveRight()
            if output[1] > 0.8:
                car.moveLeft()

        rem = []
        for wall in walls:
            for x, car in enumerate(cars):
                if wall.collide(car):
                    ge[x].fitness -= 1
                    cars.pop(x)
                    nets.pop(x)
                    ge.pop(x)

                if not wall.passed and wall.y > car.y:
                    wall.passed = True
                    addWall = True

            if wall.y + wall.RIGHT_WALL.get_height() > GAME_HEIGHT:
                rem.append(wall)
            wall.move()

        if addWall:
            score += 1
            if animationSpeed < 30:
                if score % 5 == 0:
                    animationSpeed += 1
                    bg.animationSpeed += 1
                    for car in cars:
                        if car.xVel < 50:
                            car.xVel += 2
            for g in ge:
                g.fitness += 5
            walls.append(Wall(0, animationSpeed))
            addWall = False

        for r in rem:
            walls.remove(r)

        if score == 150:
            for car in cars:
                car.xVel = 0
        drawGameAILearning(GAME_WINDOW, bg, cars, walls, score, GEN)

def runAILearning():
    local_dir = os.path.dirname(__file__)
    config_file = os.path.join(local_dir, 'config.txt')
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                neat.DefaultSpeciesSet, neat.DefaultStagnation,
                                config_file)

    p = neat.Population(config)
    # p.add_reporter(neat.StdOutReporter(True))
    # stats = neat.StatisticsReporter()
    # p.add_reporter(stats)
    p.run(eval_genomes, 5)

def gameLoop():
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config.txt')
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                neat.DefaultSpeciesSet, neat.DefaultStagnation,
                                config_path)
    pickle_in = open("bestCar.pickle", "rb")
    bestCar = pickle.load(pickle_in)
    startGame(bestCar, config)

def startGame(genomes, config):
    cars = []
    animationSpeed = 10
    bg = Background(0, 0, GAME_HEIGHT, 10)
    walls = [Wall(0, animationSpeed)]
    score = 0
    net = neat.nn.FeedForwardNetwork.create(genomes, config)
    cars.append(Player(GAME_WIDTH // 2, GAME_HEIGHT - 125))

    if MODE == 1:
        cars.append(Player(GAME_WIDTH // 2, GAME_HEIGHT - 125))
        cars[1].PLAYER_IMG = pygame.image.load('res/car1.png')

    addWall = False
    running = True
    while running:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                quit()

        keys = pygame.key.get_pressed()

        if keys[pygame.K_ESCAPE]:
            break

        if len(cars) >= 2:
            if keys[pygame.K_LEFT]:
                cars[1].moveLeft()

            elif keys[pygame.K_RIGHT]:
                cars[1].moveRight()

        wallInd = 0
        if len(cars) > 0:
            if len(walls) > 1 and cars[0].y > walls[0].y + walls[0].RIGHT_WALL.get_height():
                wallInd = 1
        else:
            break

        output = net.activate((animationSpeed,
                               (cars[0].x + cars[0].PLAYER_IMG.get_width() // 2) - (
                                       walls[wallInd].rightX - walls[wallInd].GAP),
                               walls[wallInd].rightX - (cars[0].x + cars[0].PLAYER_IMG.get_width() // 2)))

        if output[0] > 0.8:
            cars[0].moveRight()
        if output[1] > 0.8:
            cars[0].moveLeft()

        rem = []
        for wall in walls:
            for x, car in enumerate(cars):
                if len(cars) > 2 and wall.collide(cars[1]):
                    running = False
                    break
                elif wall.collide(car):
                    cars.pop(x)

                if not wall.passed and wall.y > car.y:
                    wall.passed = True
                    addWall = True

            if wall.y + wall.RIGHT_WALL.get_height() > GAME_HEIGHT:
                rem.append(wall)
            wall.move()

            if (len(cars) >= 2) and wall.collide(cars[1]):
                cars.remove(cars[1])
                running = False
                break

        if addWall:
            score += 1
            if animationSpeed < 30:
                if score % 5 == 0:
                    animationSpeed += 1
                    bg.animationSpeed += 1
                    for car in cars:
                        if car.xVel < 50:
                            car.xVel += 2
            walls.append(Wall(0, animationSpeed))
            addWall = False

        for r in rem:
            walls.remove(r)

        if score == 500:
            for car in cars:
                car.xVel = 0

        if len(cars) < 2 and MODE == 1:
            break

        drawGame(GAME_WINDOW, bg, walls, score, cars)

def setMode(mode, value):
    global MODE
    MODE = value

def makeMenu(gameWindow, gameWidth, gameHeight):
    font = pygame_menu.font.FONT_NEVIS
    fontSize = 35
    fontColor = (0, 0, 0)
    shadowColor = (255, 255, 0)
    selectionColor = (0, 0, 255)

    menu = pygame_menu.Menu(gameHeight, gameWidth, 'Reinforcement learning Car Game', onclose=pygame_menu.events.EXIT,
                            theme=pygame_menu.themes.THEME_BLUE, mouse_visible=False, mouse_enabled=False)
    menu.add_label("Look how AI learns the game or just try yourself vs bot!", font_color=fontColor,
                   shadow=True, shadow_color=shadowColor, font_name=font,
                   font_size=fontSize)
    menu.add_vertical_margin(100)
    menu.add_selector('Mode: ', [('AI Game', 0), ('Self vs AI', 1)], onchange=setMode,
                      font_name=font, font_size=fontSize,
                      font_color=fontColor, shadow=True, shadow_color=shadowColor, selection_color=selectionColor)
    menu.add_button('Play!', gameLoop, font_name=font, font_size=fontSize,
                    font_color=fontColor, shadow=True, shadow_color=shadowColor, selection_color=selectionColor)
    menu.add_button('Look how AI learns the game!', runAILearning,  font_name=font, font_size=fontSize,
                    font_color=fontColor, shadow=True, shadow_color=shadowColor, selection_color=selectionColor)
    menu.add_button('Quit!', pygame_menu.events.EXIT,  font_name=font, font_size=fontSize,
                    font_color=fontColor, shadow=True, shadow_color=shadowColor, selection_color=selectionColor)
    menu.mainloop(gameWindow)

def main(gameWindow, gameWidth, gameHeight):
    makeMenu(gameWindow, gameWidth, gameHeight)

if __name__ == '__main__':
    main(GAME_WINDOW, GAME_WIDTH, GAME_HEIGHT)


import torch
import streamlit as st
from transformers import pipeline
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
import io
import os

summarizer = pipeline("summarization")

#Setting background image
# main_bg = "./images/background.png"
# main_bg_ext = "png"

# st.markdown(
#     f"""
#     <style>
#     .reportview-container {{
#         background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()})
#     }}
#     </style>
#     """,
#     unsafe_allow_html=True
# )

st.markdown(
"""
<style>
.reportview-container {
    background: url("https://github.com/AderemiF/Capstone_RLS/blob/main/images/background.png")
}
</style>
""",
unsafe_allow_html=True
)

st.markdown(' # **Research Literature Summarizer (RLS)**')
st.image('./images/papers.png')

#Positivus
expander = st.sidebar.beta_expander('Positivus API')
expander.markdown('[click here for demo](https://positivus.io/)')

expander = st.sidebar.beta_expander('Meet the developer')
expander.image('./images/Aderemi_net_9760.jpg')
expander.write('Aderemi Fayoyiwa')
expander.markdown('[Email](https://mail.google.com/mail/u/0/?fs=1&to=aderemifayoyiwa@gmail.com&su=SUBJECT&body=BODY&tf=cm)   [GitHub](https://github.com/AderemiF)   [Linkedin](https://www.linkedin.com/in/aderemi-fayoyiwa) [Portfolio](https://aderemi-fayoyiwa.herokuapp.com/)')

# left_column, right_column = st.beta_columns(2)
# pressed = left_column.button('RLS - summarizer')
#if pressed:
#	left_column.image('images\retina.jpg')
    #left_column.write('Welcome on board!')

st.markdown('# Introduction')
st.markdown(' ### Whether there would be a scientific publication or not, most research projects begin with literature review.')
st.markdown(' ##### \nAs the name implies, literature review involves going through related published research papers to see what has already been established in the research area. \nLiterature review answers some research questions and in some cases raises more research questions. \nsResearchers also use literature review to validate/corroborate their research findings.')
st.markdown(' ##### \nResearchers spend significant amount of time reviewing published research papers that are related to their current project or potential research publication. \nAbout half of the research papers reviewed end up not being relevant to the project or publication at hand. \nDepending on how we look at it, but it can be said that half of that time is wasted. \nIf the time lost was be somewhat saved, it can be put into more productive use, say expanding the scope of research or invested in writing research reports or paper.')

st.markdown(' # Problem Statement')
st.markdown('##### \n * A significant percentage of the total research time goes into literature review. \n * Often times, over half of these literatures do not contain any useful information pertaining to the research at hand.')

st.markdown('# Goal')
st.markdown(' ##### \nThe goal of this project is: \n * To create an application that can effectively summarize research papers. \n * To significantly reduce the amount of time researchers spend on literature review. \n * To summarize research papers in a way that keeps vital information intact.')

st.markdown('# Target')
st.markdown(' ##### \nThe target audience of the RLS are: \n * Researchers \n * University students \n * High school students')

st.markdown('# Summarizer')
expander = st.beta_expander('Model')
expander.write("NLP transformer: Huggingface's pretrained transformer")
expander.write('pipeline: summarization')

expander = st.beta_expander('Technologies Used')
expander.write('Python')
expander.write('Github/Git')
expander.write('Jupyter')
expander.write('Streamlit')
expander.write('Streamlitshare')

expander = st.beta_expander('What next?')
expander.write('Further optimization of the summarizer')
expander.write('Commercialize RLS')

st.markdown('# Try it out')
st.write('Summarize your research papers')
st.markdown(" * Upload your pdf file and you will get 2 txt files in your downloads \n * The first is 'file.txt', a txt version of your pdf file \n * The second is 'summary.txt', the summarized version of your pdf file")

st.markdown("#### Upload your pdf file below, it takes about 8 minutes to summarize")
file = st.file_uploader(label = 'RLS', type=['pdf'])

#Converting pdf to txt
if file is not None:
    # st.write('File successfully uploaded')
    st.markdown("#### File successfully uploaded 🎊")
    st.markdown("#### Why don't you get a cup of coffee while your paper is being converted and summarized...")

    def pdf2txt(inPDFfile, outTXTFile):
                inFile = open(inPDFfile, 'rb')
                resMgr = PDFResourceManager()
                retData = io.StringIO()
                TxtConverter = TextConverter(resMgr, retData, laparams=LAParams())
                interpreter = PDFPageInterpreter(resMgr, TxtConverter)

                #Process each page in pdf file
                for page in PDFPage.get_pages(inFile):
                    interpreter.process_page(page)

                txt = retData.getvalue()

                #save output data to a txt file
                return txt

    inPDFfile = file.name
    outTXTFile = 'file.txt'

    try:
        txt = pdf2txt(inPDFfile, outTXTFile)
        with open(outTXTFile, 'w') as f:
            f.write(txt)
    except Exception as e:
            print(e)

    st.markdown("#### File conversion completed, summarization is ongoing 📝 ")
    st.markdown("#### How about that cup of coffee? ☕")

    #Summarization
    #This is to make text readable after splitting
    def preprocessing(txt):
        #Combining the test into one long string
        #ARTICLE = ' '.join(text)
        ARTICLE = txt.replace('.', '.<eos>')
        ARTICLE = ARTICLE.replace('!', '!<eos>')
        ARTICLE = ARTICLE.replace('?', '?<eos>')

        #Splitting ARTICLE into individual sentences using <eos>
        sentences = ARTICLE.split('<eos>')
        return sentences

    with open('file.txt') as f:
        text = f.readlines()

    #Combining the test into one long string
    ARTICLE = ' '.join(text)

    sentences = preprocessing(ARTICLE)

    #Dividing the text (sentences) into smaller chunks to allow all the text be passed in using small chunks
    max_chunk = 300
    current_chunk = 0
    chunks = []

    for sentence in sentences:
        if len(chunks) == current_chunk + 1:
            if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:
                chunks[current_chunk].extend(sentence.split(' '))
            else:
                current_chunk += 1
                chunks.append(sentence.split(' '))
        else:
            print(current_chunk)
            chunks.append(sentence.split(' '))

    to_summarize = chunks[0:15]

    #Joining individual words together again to form sentences
    for chunk_id in range(len(to_summarize)):
        to_summarize[chunk_id] = ' '.join(to_summarize[chunk_id])

    #Passing the chunks to be summarized
    result = summarizer(to_summarize, max_length=150, min_length=30, do_sample=False)

    #Extracting the summary text and joining all chunk summaries in result together.
    #text = ' '.join([summ['summary_text'] for summ in result])

    #Saving summary into a txt file
    with open ('summary.txt', 'w') as f:
        #f.write(text)

        #text = ' '.join([summ['summary_text'] for summ in result])

        for v in result:
            f.write(v['summary_text'])
            f.write('\n')

    #Notice after summarization is complete
    st.markdown("#### Summarization complete! 🎊 🎊 🎊 ")
    st.markdown("#### Check your downloads for 'summary.txt' ")


#!/usr/bin/env python3

import subprocess

from lib.coordinatesManagement import CoordinatesManager
from lib.imageHandling import ImageHandler

coordinates=CoordinatesManager(calibration_filename="calib/calibration.json")

def main():

    # takeImage=subprocess.run("./server_scripts/moveAndTakePicture.sh")
    fetchImage=subprocess.run("./server_scripts/fetchPicturefromServer.sh")

    image=ImageHandler("data/colonies.jpg",coordinates)

    while True:
        image.showImage(circles=coordinates.getPoints())
        c=image.getPressedKey()

        if c==ord('q'):
            break

        if c==ord('s'):
            coordinates.writeCoordinatesFile(filename="data/coordinates.json")

            image.saveImage("data/colonies_processed.jpg")

            for coord in coordinates.coord_transformed:
                print(coord)

            fetchImage=subprocess.run("./server_scripts/pushColoniesToServer.sh")
            break

if __name__ == "__main__":
    main()


from django.db import models
from user.models import User

# Create your models here.

# 课程类
class Course(models.Model):
    course_name = models.CharField(max_length=16,verbose_name="课程名")
    course_tercher = models.IntegerField(verbose_name="课程老师ID")
    # 课程时间 "3-5" 周三 第五节课
    course_time = models.CharField(max_length=16,verbose_name="课程时间")
    course_student_amount = models.IntegerField("最多可选学生数",default=50)
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    updated_time = models.DateTimeField('更新时间', auto_now=True)
    course_desc = models.TextField("课程描述",default="")
    user = models.ManyToManyField(User)

class Score(models.Model):
    score = models.IntegerField("考试分数",default=0)
    # 分数对应课程
    course = models.OneToOneField(Course)
    # 分数对应学生
    user = models.OneToOneField(User)


question_list = ["1.How many continents are there?",
    "2.What is the capital of India?",
    "3.NG mei kaun se course padhaya jaata hai?"]
options_list = [["1.Four", "2.Nine", "3.Seven", "4.Eight"],
    ["1.Chandigarh", "2.Bhopal", "3.Chennai", "4.Delhi"],
    ["1.Software Engineering", "2.Counseling", "3.Tourism", "4.Agriculture"]]
solution_list = [3, 4, 1]
answer_list=[["(1)Four","(3)Seven",],
    ["(2)Bhopal","(4)Delhi"],
    ["(1)software engineering","(3)Tourism"]]
print("-:WELCOME TO KBC GAME:-")
print()
i=0
sum=0
count=0
while i<len(question_list):
    print(question_list[i])
    j=0
    a=i
    while j<len(options_list[i]):
        print(options_list[a][j])
        j+=1
    if count<1:
        num=input("do you want 5050 lifeline?")
        if num=="yes":
            print(answer_list[a])
            ans=int(input("enter your option number"))
            if ans==solution_list[i]:
                sum+=10000
                print("Congratualation, your answer is correct")
                print("you won Rs/",sum)
            else:
                print("your answer is wrong")
                print("Sorry, you can't play now")
                print("you won Rs/",sum)
                break
            count+=1
        else:
            print("you have alredy used")
            ans=int(input("enter your option number"))
            if ans==solution_list[i]:
                sum+=10000
                print("Congratualation, your answer is correct ")
                print("you won Rs/",sum)
            else:
                print("your answer is wrong")
                print("Sorry, you can't play now")
                print("you won Rs/",sum)
                break
    else:
        pass
        ans2=int(input("enter your answer number"))
        if ans2==solution_list[i]:
            sum+=10000
            print("Congratualation, your answer is correct ")
            print("you won Rs/",sum)
        else:
            print("your answer is wrong")
            print("Sorry, you can't play now")
            print("you won Rs/",sum)
            break
    i+=1
print("Thank you for playing game with me.")


import numpy as np
import os
import h5py
import matplotlib.pyplot as plt

LETTERS = set(['A','G','C','T'])
TRACE_PATH = '/Analyses/RawGenomeCorrected_000/BaseCalled_template/Events'

def create_motifs_set(mlen):
    motifs = LETTERS
    for i in range(mlen - 1):
        new_motifs = set()
        for motif in motifs:
            for l in LETTERS:
                new_motifs.add(motif + l)
        motifs = set(new_motifs)
    motifs_dir = {
        m:[] for m in motifs
    }
    return motifs_dir

def cohend(d1, d2):
    n1, n2 = len(d1), len(d2)
    s1, s2 = np.var(d1, ddof=1), np.var(d2, ddof=1)
    s = np.sqrt(((n1-1) * s1 + (n2-1) * s2) / (n1 + n2 - 2))
    u1, u2 = np.mean(d1), np.mean(d2)
    return (u1 - u2) / s

def extract_motifs(dirpath, mlen=6, max_reads=8000):

    sample_motifs = create_motifs_set(mlen)

    left_shift = int(np.floor(mlen/2))
    right_shift = int(np.ceil(mlen/2))

    reads_counter = 0

    fast5_files = os.listdir(dirpath)
    fast5_files = [f for f in fast5_files if '.fast5' in f]

    for j in range(len(fast5_files)):
        print('\tBatch', j, '...')

        with h5py.File('{}/{}'.format(dirpath, fast5_files[j]), 'r', rdcc_nbytes=1024**3) as f:
            for i in list(f.items()):
                print('\t\tRead {} from {}'.format(reads_counter, max_reads), end='')
                readname = i[0]
                try:
                    trace =f['/{}'.format(readname) + TRACE_PATH][:]
                except KeyError:
                    print('\r', end='')
                    continue

                seq = ''.join([t[4].decode() for t in trace])
                signal = [t[0] for t in trace]
                for mi in range(left_shift, len(seq) - right_shift):
                    sample_motifs[seq [mi-left_shift:mi+right_shift] ].append(signal[mi])

                reads_counter += 1
                print('\r', end='')
                if reads_counter >= max_reads:
                    break
            print()

        if reads_counter >= max_reads:
            break
    print()
    return sample_motifs

def compare_samples(
    motifs_1,
    motifs_2,
    name1='sample 1',
    name2='sample 2',
    save_path='Motifs',
    mmincount=100,
    min_effect_size=0.5,
    plot=True,
    ):

    summary_list = []

    try:
        os.mkdir(save_path)
    except FileExistsError:
        print('Save path exists! Files will be rewritten!')
    print('Processing...')
    motif_counter = 1
    for motif in motifs_1:
        print('\tMotif {} from {}'.format(motif_counter, len(motifs_1)), end='')
        motif_counter += 1
        if min(len(motifs_1[motif]), len(motifs_2[motif])) < 100:
            print('\r', end='')
            continue

        effect_size = cohend(motifs_1[motif], motifs_2[motif])
        summary_list.append((abs(effect_size), motif))

        if abs(effect_size) < min_effect_size:
            print('\r', end='')
            continue
        if plot:
            plt.hist(motifs_1[motif], label=name1, bins=100, alpha=0.5, density=True)
            plt.hist(motifs_2[motif], label=name2, bins=100, alpha=0.5, density=True)
            plt.title('{}: {}'.format(motif, round(effect_size, 4)))
            plt.legend()

            plt.tight_layout()
            plt.savefig('{}/{}.pdf'.format(save_path, motif), format='pdf')
            plt.close()
            print('\r', end='')

    summary_list.sort(reverse=True)
    print('Completed.')
    print('Writing summary..')

    f = open('{}/summary.csv'.format(save_path), 'w')
    f.write('MOTIF\tEffectSize\tplotted\n')

    for s in summary_list:
        if abs(s[0]) < min_effect_size:
            plotted = 'no'
        else:
            plotted = 'yes'
        f.write('{}\t{}\t{}\n'.format(s[1], s[0], plotted))
    f.close()

    f = open('{}/sequences.fasta'.format(save_path), 'w')
    for s in summary_list:
        if abs(s[0]) < min_effect_size:
            continue
        f.write('>{}; Effect_size {}\n'.format(s[1],s[0]))
        f.write(s[1] + '\n')
    f.close()

    print('Done!')