import sys

from PyQt5.QtCore import QFile, QFileInfo, Qt, QTextCodec
from PyQt5.QtGui import (QFont, QFontDatabase, QFontInfo, QIcon, QKeySequence,
        QPixmap, QTextBlockFormat, QTextCharFormat, QTextCursor,
        QTextDocumentWriter, QTextListFormat)
from PyQt5.QtWidgets import (QAction, QActionGroup, QApplication, QColorDialog,
        QComboBox, QFileDialog, QFontComboBox, QMainWindow, QMenu, QMessageBox,
        QTextEdit, QToolBar)
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog

import textedit_rc

if sys.platform.startswith('darwin'):
    rsrcPath = ":/images/mac"
else:
    rsrcPath = ":/images/win"

class TextEdit(QMainWindow):
    def __init__(self, fileName=None, parent=None):
        super(TextEdit, self).__init__(parent)

        self.setWindowIcon(QIcon(':/images/logo.png'))
        self.setToolButtonStyle(Qt.ToolButtonFollowStyle)
        self.setupFileActions()
        self.setupEditActions()
        self.setupTextActions()

        helpMenu = QMenu("Help", self)
        self.menuBar().addMenu(helpMenu)
        helpMenu.addAction("About", self.about)
        helpMenu.addAction("About &Qt", QApplication.instance().aboutQt)

        self.textEdit = QTextEdit(self)
        self.textEdit.currentCharFormatChanged.connect(
                self.currentCharFormatChanged)
        self.textEdit.cursorPositionChanged.connect(self.cursorPositionChanged)
        self.setCentralWidget(self.textEdit)
        self.textEdit.setFocus()
        self.setCurrentFileName()
        self.fontChanged(self.textEdit.font())
        self.colorChanged(self.textEdit.textColor())
        self.alignmentChanged(self.textEdit.alignment())
        self.textEdit.document().modificationChanged.connect(
                self.actionSave.setEnabled)
        self.textEdit.document().modificationChanged.connect(
                self.setWindowModified)
        self.textEdit.document().undoAvailable.connect(
                self.actionUndo.setEnabled)
        self.textEdit.document().redoAvailable.connect(
                self.actionRedo.setEnabled)
        self.setWindowModified(self.textEdit.document().isModified())
        self.actionSave.setEnabled(self.textEdit.document().isModified())
        self.actionUndo.setEnabled(self.textEdit.document().isUndoAvailable())
        self.actionRedo.setEnabled(self.textEdit.document().isRedoAvailable())
        self.actionUndo.triggered.connect(self.textEdit.undo)
        self.actionRedo.triggered.connect(self.textEdit.redo)
        self.actionCut.setEnabled(False)
        self.actionCopy.setEnabled(False)
        self.actionCut.triggered.connect(self.textEdit.cut)
        self.actionCopy.triggered.connect(self.textEdit.copy)
        self.actionPaste.triggered.connect(self.textEdit.paste)
        self.textEdit.copyAvailable.connect(self.actionCut.setEnabled)
        self.textEdit.copyAvailable.connect(self.actionCopy.setEnabled)
        QApplication.clipboard().dataChanged.connect(self.clipboardDataChanged)

        if fileName is None:
            fileName = ':/example.html'

        if not self.load(fileName):
            self.fileNew()

    def closeEvent(self, e):
        if self.maybeSave():
            e.accept()
        else:
            e.ignore()

    def setupFileActions(self):
        tb = QToolBar(self)
        tb.setWindowTitle("File Actions")
        self.addToolBar(tb)

        menu = QMenu("&File", self)
        self.menuBar().addMenu(menu)

        self.actionNew = QAction(
                QIcon.fromTheme('document-new',
                        QIcon(rsrcPath + '/filenew.png')),
                "&New", self, priority=QAction.LowPriority,
                shortcut=QKeySequence.New, triggered=self.fileNew)
        tb.addAction(self.actionNew)
        menu.addAction(self.actionNew)

        self.actionOpen = QAction(
                QIcon.fromTheme('document-open',
                        QIcon(rsrcPath + '/fileopen.png')),
                "&Open...", self, shortcut=QKeySequence.Open,
                triggered=self.fileOpen)
        tb.addAction(self.actionOpen)
        menu.addAction(self.actionOpen)
        menu.addSeparator()

        self.actionSave = QAction(
                QIcon.fromTheme('document-save',
                        QIcon(rsrcPath + '/filesave.png')),
                "&Save", self, shortcut=QKeySequence.Save,
                triggered=self.fileSave, enabled=False)
        tb.addAction(self.actionSave)
        menu.addAction(self.actionSave)

        self.actionSaveAs = QAction("Save &As...", self,
                priority=QAction.LowPriority,
                shortcut=Qt.CTRL + Qt.SHIFT + Qt.Key_S,
                triggered=self.fileSaveAs)
        menu.addAction(self.actionSaveAs)
        menu.addSeparator()

        self.actionPrint = QAction(
                QIcon.fromTheme('document-print',
                        QIcon(rsrcPath + '/fileprint.png')),
                "&Print...", self, priority=QAction.LowPriority,
                shortcut=QKeySequence.Print, triggered=self.filePrint)
        tb.addAction(self.actionPrint)
        menu.addAction(self.actionPrint)

        self.actionPrintPreview = QAction(
                QIcon.fromTheme('fileprint',
                        QIcon(rsrcPath + '/fileprint.png')),
                "Print Preview...", self,
                shortcut=Qt.CTRL + Qt.SHIFT + Qt.Key_P,
                triggered=self.filePrintPreview)
        menu.addAction(self.actionPrintPreview)

        self.actionPrintPdf = QAction(
                QIcon.fromTheme('exportpdf',
                        QIcon(rsrcPath + '/exportpdf.png')),
                "&Export PDF...", self, priority=QAction.LowPriority,
                shortcut=Qt.CTRL + Qt.Key_D,
                triggered=self.filePrintPdf)
        tb.addAction(self.actionPrintPdf)
        menu.addAction(self.actionPrintPdf)
        menu.addSeparator()

        self.actionQuit = QAction("&Quit", self, shortcut=QKeySequence.Quit,
                triggered=self.close)
        menu.addAction(self.actionQuit)

    def setupEditActions(self):
        tb = QToolBar(self)
        tb.setWindowTitle("Edit Actions")
        self.addToolBar(tb)

        menu = QMenu("&Edit", self)
        self.menuBar().addMenu(menu)

        self.actionUndo = QAction(
                QIcon.fromTheme('edit-undo',
                        QIcon(rsrcPath + '/editundo.png')),
                "&Undo", self, shortcut=QKeySequence.Undo)
        tb.addAction(self.actionUndo)
        menu.addAction(self.actionUndo)

        self.actionRedo = QAction(
                QIcon.fromTheme('edit-redo',
                        QIcon(rsrcPath + '/editredo.png')),
                "&Redo", self, priority=QAction.LowPriority,
                shortcut=QKeySequence.Redo)
        tb.addAction(self.actionRedo)
        menu.addAction(self.actionRedo)
        menu.addSeparator()

        self.actionCut = QAction(
                QIcon.fromTheme('edit-cut', QIcon(rsrcPath + '/editcut.png')),
                "Cu&t", self, priority=QAction.LowPriority,
                shortcut=QKeySequence.Cut)
        tb.addAction(self.actionCut)
        menu.addAction(self.actionCut)

        self.actionCopy = QAction(
                QIcon.fromTheme('edit-copy',
                        QIcon(rsrcPath + '/editcopy.png')),
                "&Copy", self, priority=QAction.LowPriority,
                shortcut=QKeySequence.Copy)
        tb.addAction(self.actionCopy)
        menu.addAction(self.actionCopy)

        self.actionPaste = QAction(
                QIcon.fromTheme('edit-paste',
                        QIcon(rsrcPath + '/editpaste.png')),
                "&Paste", self, priority=QAction.LowPriority,
                shortcut=QKeySequence.Paste,
                enabled=(len(QApplication.clipboard().text()) != 0))
        tb.addAction(self.actionPaste)
        menu.addAction(self.actionPaste)

    def setupTextActions(self):
        tb = QToolBar(self)
        tb.setWindowTitle("Format Actions")
        self.addToolBar(tb)

        menu = QMenu("F&ormat", self)
        self.menuBar().addMenu(menu)

        self.actionTextBold = QAction(
                QIcon.fromTheme('format-text-bold',
                        QIcon(rsrcPath + '/textbold.png')),
                "&Bold", self, priority=QAction.LowPriority,
                shortcut=Qt.CTRL + Qt.Key_B, triggered=self.textBold,
                checkable=True)
        bold = QFont()
        bold.setBold(True)
        self.actionTextBold.setFont(bold)
        tb.addAction(self.actionTextBold)
        menu.addAction(self.actionTextBold)

        self.actionTextItalic = QAction(
                QIcon.fromTheme('format-text-italic',
                        QIcon(rsrcPath + '/textitalic.png')),
                "&Italic", self, priority=QAction.LowPriority,
                shortcut=Qt.CTRL + Qt.Key_I, triggered=self.textItalic,
                checkable=True)
        italic = QFont()
        italic.setItalic(True)
        self.actionTextItalic.setFont(italic)
        tb.addAction(self.actionTextItalic)
        menu.addAction(self.actionTextItalic)

        self.actionTextUnderline = QAction(
                QIcon.fromTheme('format-text-underline',
                        QIcon(rsrcPath + '/textunder.png')),
                "&Underline", self, priority=QAction.LowPriority,
                shortcut=Qt.CTRL + Qt.Key_U, triggered=self.textUnderline,
                checkable=True)
        underline = QFont()
        underline.setUnderline(True)
        self.actionTextUnderline.setFont(underline)
        tb.addAction(self.actionTextUnderline)
        menu.addAction(self.actionTextUnderline)

        menu.addSeparator()

        grp = QActionGroup(self, triggered=self.textAlign)

        # Make sure the alignLeft is always left of the alignRight.
        if QApplication.isLeftToRight():
            self.actionAlignLeft = QAction(
                    QIcon.fromTheme('format-justify-left',
                            QIcon(rsrcPath + '/textleft.png')),
                    "&Left", grp)
            self.actionAlignCenter = QAction(
                    QIcon.fromTheme('format-justify-center',
                            QIcon(rsrcPath + '/textcenter.png')),
                    "C&enter", grp)
            self.actionAlignRight = QAction(
                    QIcon.fromTheme('format-justify-right',
                            QIcon(rsrcPath + '/textright.png')),
                    "&Right", grp)
        else:
            self.actionAlignRight = QAction(
                    QIcon.fromTheme('format-justify-right',
                            QIcon(rsrcPath + '/textright.png')),
                    "&Right", grp)
            self.actionAlignCenter = QAction(
                    QIcon.fromTheme('format-justify-center',
                            QIcon(rsrcPath + '/textcenter.png')),
                    "C&enter", grp)
            self.actionAlignLeft = QAction(
                    QIcon.fromTheme('format-justify-left',
                            QIcon(rsrcPath + '/textleft.png')),
                    "&Left", grp)

        self.actionAlignJustify = QAction(
                QIcon.fromTheme('format-justify-fill',
                        QIcon(rsrcPath + '/textjustify.png')),
                "&Justify", grp)

        self.actionAlignLeft.setShortcut(Qt.CTRL + Qt.Key_L)
        self.actionAlignLeft.setCheckable(True)
        self.actionAlignLeft.setPriority(QAction.LowPriority)

        self.actionAlignCenter.setShortcut(Qt.CTRL + Qt.Key_E)
        self.actionAlignCenter.setCheckable(True)
        self.actionAlignCenter.setPriority(QAction.LowPriority)

        self.actionAlignRight.setShortcut(Qt.CTRL + Qt.Key_R)
        self.actionAlignRight.setCheckable(True)
        self.actionAlignRight.setPriority(QAction.LowPriority)

        self.actionAlignJustify.setShortcut(Qt.CTRL + Qt.Key_J)
        self.actionAlignJustify.setCheckable(True)
        self.actionAlignJustify.setPriority(QAction.LowPriority)

        tb.addActions(grp.actions())
        menu.addActions(grp.actions())
        menu.addSeparator()

        pix = QPixmap(16, 16)
        pix.fill(Qt.black)
        self.actionTextColor = QAction(QIcon(pix), "&Color...", self,
                triggered=self.textColor)
        tb.addAction(self.actionTextColor)
        menu.addAction(self.actionTextColor)

        tb = QToolBar(self)
        tb.setAllowedAreas(Qt.TopToolBarArea | Qt.BottomToolBarArea)
        tb.setWindowTitle("Format Actions")
        self.addToolBarBreak(Qt.TopToolBarArea)
        self.addToolBar(tb)

        comboStyle = QComboBox(tb)
        tb.addWidget(comboStyle)
        comboStyle.addItem("Standard")
        comboStyle.addItem("Bullet List (Disc)")
        comboStyle.addItem("Bullet List (Circle)")
        comboStyle.addItem("Bullet List (Square)")
        comboStyle.addItem("Ordered List (Decimal)")
        comboStyle.addItem("Ordered List (Alpha lower)")
        comboStyle.addItem("Ordered List (Alpha upper)")
        comboStyle.addItem("Ordered List (Roman lower)")
        comboStyle.addItem("Ordered List (Roman upper)")
        comboStyle.activated.connect(self.textStyle)

        self.comboFont = QFontComboBox(tb)
        tb.addWidget(self.comboFont)
        self.comboFont.activated[str].connect(self.textFamily)

        self.comboSize = QComboBox(tb)
        self.comboSize.setObjectName("comboSize")
        tb.addWidget(self.comboSize)
        self.comboSize.setEditable(True)

        db = QFontDatabase()
        for size in db.standardSizes():
            self.comboSize.addItem("%s" % (size))

        self.comboSize.activated[str].connect(self.textSize)
        self.comboSize.setCurrentIndex(
                self.comboSize.findText(
                        "%s" % (QApplication.font().pointSize())))

    def load(self, f):
        if not QFile.exists(f):
            return False

        fh = QFile(f)
        if not fh.open(QFile.ReadOnly):
            return False

        data = fh.readAll()
        codec = QTextCodec.codecForHtml(data)
        unistr = codec.toUnicode(data)

        if Qt.mightBeRichText(unistr):
            self.textEdit.setHtml(unistr)
        else:
            self.textEdit.setPlainText(unistr)

        self.setCurrentFileName(f)
        return True

    def maybeSave(self):
        if not self.textEdit.document().isModified():
            return True

        if self.fileName.startswith(':/'):
            return True

        ret = QMessageBox.warning(self, "Application",
                "The document has been modified.\n"
                "Do you want to save your changes?",
                QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)

        if ret == QMessageBox.Save:
            return self.fileSave()

        if ret == QMessageBox.Cancel:
            return False

        return True

    def setCurrentFileName(self, fileName=''):
        self.fileName = fileName
        self.textEdit.document().setModified(False)

        if not fileName:
            shownName = 'untitled.txt'
        else:
            shownName = QFileInfo(fileName).fileName()

        self.setWindowTitle(self.tr("%s[*] - %s" % (shownName, "Rich Text")))
        self.setWindowModified(False)

    def fileNew(self):
        if self.maybeSave():
            self.textEdit.clear()
            self.setCurrentFileName()

    def fileOpen(self):
        fn, _ = QFileDialog.getOpenFileName(self, "Open File...", None,
                "HTML-Files (*.htm *.html);;All Files (*)")

        if fn:
            self.load(fn)

    def fileSave(self):
        if not self.fileName:
            return self.fileSaveAs()

        writer = QTextDocumentWriter(self.fileName)
        success = writer.write(self.textEdit.document())
        if success:
            self.textEdit.document().setModified(False)

        return success

    def fileSaveAs(self):
        fn, _ = QFileDialog.getSaveFileName(self, "Save as...", None,
                "ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)")

        if not fn:
            return False

        lfn = fn.lower()
        if not lfn.endswith(('.odt', '.htm', '.html')):
            # The default.
            fn += '.odt'

        self.setCurrentFileName(fn)
        return self.fileSave()

    def filePrint(self):
        printer = QPrinter(QPrinter.HighResolution)
        dlg = QPrintDialog(printer, self)

        if self.textEdit.textCursor().hasSelection():
            dlg.addEnabledOption(QPrintDialog.PrintSelection)

        dlg.setWindowTitle("Print Document")

        if dlg.exec_() == QPrintDialog.Accepted:
            self.textEdit.print_(printer)

        del dlg

    def filePrintPreview(self):
        printer = QPrinter(QPrinter.HighResolution)
        preview = QPrintPreviewDialog(printer, self)
        preview.paintRequested.connect(self.printPreview)
        preview.exec_()

    def printPreview(self, printer):
        self.textEdit.print_(printer)

    def filePrintPdf(self):
        fn, _ = QFileDialog.getSaveFileName(self, "Export PDF", None,
                "PDF files (*.pdf);;All Files (*)")

        if fn:
            if QFileInfo(fn).suffix().isEmpty():
                fn += '.pdf'

            printer = QPrinter(QPrinter.HighResolution)
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName(fn)
            self.textEdit.document().print_(printer)

    def textBold(self):
        fmt = QTextCharFormat()
        fmt.setFontWeight(self.actionTextBold.isChecked() and QFont.Bold or QFont.Normal)
        self.mergeFormatOnWordOrSelection(fmt)

    def textUnderline(self):
        fmt = QTextCharFormat()
        fmt.setFontUnderline(self.actionTextUnderline.isChecked())
        self.mergeFormatOnWordOrSelection(fmt)

    def textItalic(self):
        fmt = QTextCharFormat()
        fmt.setFontItalic(self.actionTextItalic.isChecked())
        self.mergeFormatOnWordOrSelection(fmt)

    def textFamily(self, family):
        fmt = QTextCharFormat()
        fmt.setFontFamily(family)
        self.mergeFormatOnWordOrSelection(fmt)

    def textSize(self, pointSize):
        pointSize = float(pointSize)
        if pointSize > 0:
            fmt = QTextCharFormat()
            fmt.setFontPointSize(pointSize)
            self.mergeFormatOnWordOrSelection(fmt)

    def textStyle(self, styleIndex):
        cursor = self.textEdit.textCursor()
        if styleIndex:
            styleDict = {
                1: QTextListFormat.ListDisc,
                2: QTextListFormat.ListCircle,
                3: QTextListFormat.ListSquare,
                4: QTextListFormat.ListDecimal,
                5: QTextListFormat.ListLowerAlpha,
                6: QTextListFormat.ListUpperAlpha,
                7: QTextListFormat.ListLowerRoman,
                8: QTextListFormat.ListUpperRoman,
            }

            style = styleDict.get(styleIndex, QTextListFormat.ListDisc)
            cursor.beginEditBlock()
            blockFmt = cursor.blockFormat()
            listFmt = QTextListFormat()

            if cursor.currentList():
                listFmt = cursor.currentList().format()
            else:
                listFmt.setIndent(blockFmt.indent() + 1)
                blockFmt.setIndent(0)
                cursor.setBlockFormat(blockFmt)

            listFmt.setStyle(style)
            cursor.createList(listFmt)
            cursor.endEditBlock()
        else:
            bfmt = QTextBlockFormat()
            bfmt.setObjectIndex(-1)
            cursor.mergeBlockFormat(bfmt)

    def textColor(self):
        col = QColorDialog.getColor(self.textEdit.textColor(), self)
        if not col.isValid():
            return

        fmt = QTextCharFormat()
        fmt.setForeground(col)
        self.mergeFormatOnWordOrSelection(fmt)
        self.colorChanged(col)

    def textAlign(self, action):
        if action == self.actionAlignLeft:
            self.textEdit.setAlignment(Qt.AlignLeft | Qt.AlignAbsolute)
        elif action == self.actionAlignCenter:
            self.textEdit.setAlignment(Qt.AlignHCenter)
        elif action == self.actionAlignRight:
            self.textEdit.setAlignment(Qt.AlignRight | Qt.AlignAbsolute)
        elif action == self.actionAlignJustify:
            self.textEdit.setAlignment(Qt.AlignJustify)

    def currentCharFormatChanged(self, format):
        self.fontChanged(format.font())
        self.colorChanged(format.foreground().color())

    def cursorPositionChanged(self):
        self.alignmentChanged(self.textEdit.alignment())

    def clipboardDataChanged(self):
        self.actionPaste.setEnabled(len(QApplication.clipboard().text()) != 0)

    def about(self):
        QMessageBox.about(self, "About",
                "This example demonstrates Qt's rich text editing facilities "
                "in action, providing an example document for you to "
                "experiment with.")

    def mergeFormatOnWordOrSelection(self, format):
        cursor = self.textEdit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)

        cursor.mergeCharFormat(format)
        self.textEdit.mergeCurrentCharFormat(format)

    def fontChanged(self, font):
        self.comboFont.setCurrentIndex(
                self.comboFont.findText(QFontInfo(font).family()))
        self.comboSize.setCurrentIndex(
                self.comboSize.findText("%s" % font.pointSize()))
        self.actionTextBold.setChecked(font.bold())
        self.actionTextItalic.setChecked(font.italic())
        self.actionTextUnderline.setChecked(font.underline())

    def colorChanged(self, color):
        pix = QPixmap(16, 16)
        pix.fill(color)
        self.actionTextColor.setIcon(QIcon(pix))

    def alignmentChanged(self, alignment):
        if alignment & Qt.AlignLeft:
            self.actionAlignLeft.setChecked(True)
        elif alignment & Qt.AlignHCenter:
            self.actionAlignCenter.setChecked(True)
        elif alignment & Qt.AlignRight:
            self.actionAlignRight.setChecked(True)
        elif alignment & Qt.AlignJustify:
            self.actionAlignJustify.setChecked(True)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    mainWindows = []
    for fn in sys.argv[1:] or [None]:
        textEdit = TextEdit(fn)
        textEdit.resize(700, 800)
        textEdit.show()
        mainWindows.append(textEdit)

    sys.exit(app.exec_())


import sys, random, re, logging, collections, os, io
from numpy import random

class PSM(object):
    """
    This class represents the data pertained on a particular (peptideID, charge state) tuple
    """

    __slots__ = ['id', 'seq', 'weights', 'ions', 'scores', 'y_fractions']

    def __init__(self, key, seq):
        self.id = key
        self.seq = seq
        self.weights = {'b': list(), 'y': list()}
        self.ions = {'b': list(), 'y': list()}
        self.scores = list()
        self.y_fractions = list()

    def addSpectra(self, ions_n_ints, score, y_frac):
        self.scores.append(score)
        self.y_fractions.append(y_frac)
        for ion_type, val_tuple in ions_n_ints.items():
            self.ions[ion_type].append(val_tuple[1])
            self.weights[ion_type].append(val_tuple[2])

    def addSpectra2(self, ions_n_ints, score, y_frac):
        """
        Stores the ion series as a list of ints rather than a list of strings
        this is useful for decreasing memory consumption
        """
        self.scores.append(score)
        self.y_fractions.append(y_frac)
        for ion_type, val_tuple in ions_n_ints.items():
            key, ion_series, ion_ints = val_tuple
            ion_series = [int(x[1:]) for x in ion_series]
            self.ions[ion_type].append(ion_series)
            self.weights[ion_type].append(ion_ints)

    def getSize(self):
        from common_utils import get_size
        total = sys.getsizeof(self.id)
        total += sys.getsizeof(self.seq)
        total += get_size(self.weights)
        total += get_size(self.ions)
        total += get_size(self.scores)
        total += get_size(self.y_fractions)
        return total

    def listSize(self):
        from common_utils import get_size
        print(f"Size listing of PSMs for {(self.seq, self.id[1])}, # of spectra={len(self.scores)}")
        print("-"*60)
        print(f"{'Id/key':8s}:{sys.getsizeof(self.id):>6d}")
        print(f"{'Sequence':8s}:{sys.getsizeof(self.seq):>6d}")
        print(f"{'Weights':8s}:{get_size(self.weights):>6d}")
        print(f"{'Ions':8s}:{get_size(self.ions):>6d}")
        print(f"{'Scores':8s}:{get_size(self.scores):>6d}")
        print(f"{'y_fracs':8s}:{get_size(self.y_fractions):>6d}")
        print("=" * 60)

    def listIons(self):
        from itertools import islice
        for i in islice(range(len(self.ions['y'])), 7):
            y = self.ions['y'][i]
            b = self.ions['b'][i]
            print(', '.join(y))
            print(', '.join(b))
            print()

class MQParser(object):
    __slots__ = ['psms', 'pos', 'neg', 'fdr', 'fdr_threshold', 'ion_threshold', 'mc', 'debug']

    def __init__(self, fdr_threshold=0.01, ion_threshold=0.001, mc=1, dodebug=False):
        self.psms = {}
        self.pos = 0.0
        self.neg = 0.0
        self.fdr = 0.0
        self.fdr_threshold = fdr_threshold
        self.ion_threshold = ion_threshold
        self.mc = mc
        self.debug = dodebug

    def newPSM(self, key, seq):
        psm = PSM(key, seq)
        self.psms[key] = psm

    def updateInfo(self, key, ions_ints, s, yfrac):
        self.psms[key].addSpectra2(ions_ints, s, yfrac)

    def getDataAsTuple(self, key):
        if key not in self.psms:
            raise KeyError('Specified key not found')

        hit = self.psms[key]

        # Assertions:
        if len(hit.scores) != len(hit.y_fractions):
            raise AssertionError("Values of uneven length for {}".format(key))
        if len(hit.ions['b']) != len(hit.weights['b']):
            raise AssertionError("Values of uneven length for {}".format(key))
        if len(hit.ions['y']) != len(hit.weights['y']):
            raise AssertionError("Values of uneven length for {}".format(key))

        _getionseries = lambda itype, iseries: [[itype+str(ion) for ion in s] for s in iseries]

        return zip(hit.scores,
                   _getionseries('y', hit.ions['y']), hit.weights['y'],
                   _getionseries('b', hit.ions['b']), hit.weights['b'],
                   hit.y_fractions)

    def getKeys(self):
        return list(self.psms.keys())

    def get_subset(self, keyset):
        import copy
        parser = copy.copy(self)
        parser.psms = {k: v for k, v in self.psms.items() if k in keyset}
        return parser

    def incrementNeg(self):
        self.neg += 1
        self._updateFDR()

    def incrementPos(self):
        self.pos += 1

    def _updateFDR(self):
        self.fdr = self.neg / (self.neg + self.pos)
        return self.fdr

    def __contains__(self, key):
        return self.psms.__contains__(key)

    def processChunk(self, df, skip_sort):
        if self.debug:
            print("{0:10}: {1:>12,}".format("Data Frame", sys.getsizeof(df)))
            print("{0:10}: {1:>12,}".format("Parser", sys.getsizeof(self)))
            input("Enter to continue...")

        if not skip_sort:
            df = df.sort_values(by='PEP')

        '''
        Initiate variables, TODO: Consider placing these in a container
        '''
        for row in df.itertuples():
            '''
            Filtering rows based on reverse hits, modified peptides or missing intensities
            '''
            if row.Reverse == '+' or row.Proteins == '':
                self.incrementNeg()
                continue
            if row.Modifications != 'Unmodified':
                continue
            if row.Intensities == '':
                continue
            if row.Number_of_Matches == 0:
                continue
            if not self._checkMisscleavage(row.Sequence):
                continue

            '''
            Use (Peptide ID, Charge) tuple as a key. OBS: The consistency of peptide IDs is delegated to MQ
            '''
            key = (int(row.Peptide_ID), int(row.Charge))
            intensities = [float(x) for x in row.Intensities.split(';')]
            ion_series = re.findall('[abcxyz]\d+', row.Matches)
            ii = list(zip(intensities, ion_series))
            total_int = {}
            temp = {}

            '''
            Normalize each ion in b/y series to the sum of respective series
            '''
            if key not in self:
                self.newPSM(key, row.Sequence)

            for ion_type in 'by':
                total_int[ion_type] = sum([i for (i, s) in ii if s[0] == ion_type])
                ion_int = collections.defaultdict(float)
                for _int, _ion in ii:
                    if _ion[0] == ion_type:
                        ion_int[_ion] += _int / total_int[ion_type]

                if len(ion_int) != 0:
                    _ions, _weights = zip(*(x for x in ion_int.items() if x[1] > self.ion_threshold))
                    temp[ion_type] = (key, _ions, _weights)
                else:
                    # If a spectra has only y- or b- ions an empty list is appended
                    # TODO: Is there a better way to handle this? Are the empty lists used ever?
                    temp[ion_type] = (key, [], [])
            try:
                yfrac = total_int['y'] / (total_int['y'] + total_int['b'])
                self.updateInfo(key, temp, float(row.Score), yfrac)

            except ZeroDivisionError:
                '''
                This happens when there are no y- or b- ions, for ex. when one spectra
                only has an a2 ion, and no other ions! Such spectra are useless and thus deleted
                '''

                logging.debug("Empty/Useless spectra found:")
                logging.debug(row)

            '''
            Update FDR counter and check limit criterion
            '''
            self.incrementPos()
            if self.fdr > self.fdr_threshold:
                print("Breaking loop on FDR limit: pos={}, neg={}".format(self.pos, self.neg))
                return False

        return True

    def outputResults(self, outf, spectra_t, nslices):
        all_keys = self.getKeys()
        l = len(all_keys)
        random.shuffle(all_keys)
        nfiles = nslices
        key_slices = [all_keys[i * l // nfiles:(i + 1) * l // nfiles] for i in range(nfiles)]

        filepath = os.path.join(outf, "training_set_{}_{}.tsv")
        _openf = lambda i: open(filepath.format(i, spectra), 'w')

        for i, keys in enumerate(key_slices):
            with open(filepath.format(i, spectra_t), 'w') as outfile:
                for key in keys:
                    data = list(self.getDataAsTuple(key))
                    # data = sorted(data, key=itemgetter(0), reverse=True) # TODO: Unnecessary, data is already sorted
                    if not data:    # check for empty spectra
                        continue

                    charge = key[1]
                    seq = self.psms[key].seq
                    spectra = self._getSpectra(data, spectra_t)
                    for s in spectra:
                        self.write_results(outfile, charge, seq, *s)

    def _checkMisscleavage(self, seq):
        matches = re.findall('[KR]', seq)
        if seq.endswith('K') or seq.endswith('R'):
            return len(matches) <= self.mc + 1
        else:
            return len(matches) <= self.mc

    def _getSpectra(self, data, spectra):
        # Pick the spectra to use, depending on runtime param
        if spectra == 'best':
            return [data[0]]

        elif spectra == 'rand':
            r = random.randint(len(data))
            return [data[r]]

        elif spectra == 'med':
            return [data[len(data) // 2]]

        elif spectra == 'all':
            return data

        elif spectra == 'ave':
            _ysum_weight = collections.defaultdict(float)
            _bsum_weight = collections.defaultdict(float)
            yf = 0
            score = 0
            nhits = len(data)
            for _s, _yi, _yw, _bi, _bw, _yf in data:
                for i, w in zip(_yi, _yw):
                    _ysum_weight[i] += w / nhits

                for i, w in zip(_bi, _bw):
                    _bsum_weight[i] += w / nhits

                yf += _yf
                score += _s

            yi, yw, bi, bw = [], [], [], []
            if len(_ysum_weight) != 0:
                yi, yw = zip(*_ysum_weight.items())
            if len(_bsum_weight) != 0:
                bi, bw = zip(*_bsum_weight.items())

            yf /= nhits
            score /= nhits

            return [(score, yi, yw, bi, bw, yf)]

    def _write(self, f, charge, seq, score, y_ions, y_weights, b_ions, b_weights, y_frag):
        y_ions = ' '.join(map(str, y_ions))
        b_ions = ' '.join(map(str, b_ions))
        y_weights = ' '.join(map(str, y_weights))
        b_weights = ' '.join(map(str, b_weights))

        f.write("\t".join(map(str, (charge, seq, score, y_ions, y_weights, b_ions, b_weights, y_frag))))
        f.write('\n')

    def write_results(self, f, charge, seq, score, y_ions, y_weights, b_ions, b_weights, y_frag, i=-1):
        if isinstance(f, io.IOBase):
            self._write(f, charge, seq, score, y_ions, y_weights, b_ions, b_weights, y_frag)
        else:
            # TODO: is this necessary, when is it ever run?
            if i % 5 == 0:
                # write to benchmark file every 1/5 time
                self._write(f[-1], charge, seq, score, y_ions, y_weights, b_ions, b_weights, y_frag)
            elif i % 5 == 1:
                # write to test or training file every 1/5 time
                self._write(f[-2], charge, seq, score, y_ions, y_weights, b_ions, b_weights, y_frag)
            else:
                # write to training the rest of the time
                self._write(f[0], charge, seq, score, y_ions, y_weights, b_ions, b_weights, y_frag)


#continue challenge3_Vehicles.py and add electronic vehicle car with same description
import vehicles

def main():
    used_car = vehicles.Car_demo('Audi',2022,45000,80000.0,4,'no')
    used_car2 = vehicles.Car_demo('Honda',2022,45000,80000,2,'no')
    used_car3 = vehicles.Car_demo('Kia',2022,50000,65000,4,'yes')
    used_car.print_description()
    used_car2.print_description()
    used_car3.print_description()

main()

Module 10/Class Exercise #10/challenge6.py


from BitVector import BitVector
from random import sample, randrange, randint

MAX_N = 32000

def solution (dups=1):
    bv = BitVector(size = MAX_N)
    lst = gen_input_list(dups)
    return mark_bv(lst, bv)

def mark_bv(lst, bv):
    for i in lst:
        bv[i] = 1 ^ bv[i]
    return get_dups(bv)

def get_dups(bv):
    dups = set()
    for i in range(len(bv)-1):
        if bv[i] == 0:
            dups.add(i)
    return dups

def gen_input_list(dups=1):
    size = randrange(MAX_N-1, MAX_N, 1)
    input =  list(sample(range(size), size))
    while dups > 0:
        input.append(randint(0, MAX_N))
        dups -= 1
    return input

#print(list(gen_input_list(3)))
print(solution(2))


from individual import INDIVIDUAL
import pickle
from environments import ENVIORNMENTS
from platforms import PLATFORMS
from population import POPULATION
from recorder import Recorder
import os
import constants as c
from archive import Archive

envs = PLATFORMS()
# f = open('saved_files/bestJumpEver2019-04-27.p', 'rb')
# f = open('saved_files/4hidden_500gen_apfo_thruwall2019-05-02.p', 'rb')
# f = open('saved_files/2019-05-02.p', 'rb')
# f = open('saved_files/samplesave2019-05-01.p', 'rb')
# f = open('saved_files/4hidden_6legs_apfo_500gen_ind.p', 'rb')
f = open('saved_files/1env_500gens_genetic_6legs_4hidden_trial32019-05-05.p', 'rb')
best_genome = pickle.load(f)
f.close()
best_pop = POPULATION(1)

if isinstance(best_genome, Archive):
    best_pop.Initialize(best_genome.genome, best_genome.cppn)
elif isinstance(best_genome, list):
    best_pop.Initialize(best_genome)
else:
    print("Could not determine class")
    exit()

# best_pop.Initialize(best_genome)
best_pop.Evaluate_Best(envs, pb = False, pp = True, retain_data = True)

recorder = Recorder()
recorder.plot_touch_values(best_pop.pop[0])

newSave = input("New Save? (y/n)")
if (newSave == 'y'):
    label = input("New Label: ") + '.p'
    manSav = open(os.path.join(c.save_file_folder,label), "wb")
    saved_ind = Archive(best_pop.pop[0].genome, best_pop.pop[0].cppn.save())
    pickle.dump(saved_ind, manSav)


#-------------------------------------------------------------------------------
# Copyright (c) 2012 Gael Honorez.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the GNU Public License v3.0
# which accompanies this distribution, and is available at
# http://www.gnu.org/licenses/gpl.html
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#-------------------------------------------------------------------------------

from PyQt4 import QtCore, QtGui, QtWebKit
import util

class TourneyItemDelegate(QtGui.QStyledItemDelegate):
    #colors = json.loads(util.readfile("client/colors.json"))

    def __init__(self, *args, **kwargs):
        QtGui.QStyledItemDelegate.__init__(self, *args, **kwargs)
        self.height = 125

    def paint(self, painter, option, index, *args, **kwargs):
        self.initStyleOption(option, index)

        painter.save()

        html = QtGui.QTextDocument()
        html.setHtml(option.text)
        if self.height < html.size().height() :
            self.height = html.size().height()

        option.text = ""
        option.widget.style().drawControl(QtGui.QStyle.CE_ItemViewItem, option, painter, option.widget)

        #Description
        painter.translate(option.rect.left(), option.rect.top())
        #painter.fillRect(QtCore.QRect(0, 0, option.rect.width(), option.rect.height()), QtGui.QColor(36, 61, 75, 150))
        clip = QtCore.QRectF(0, 0, option.rect.width(), option.rect.height())
        html.drawContents(painter, clip)

        painter.restore()

    def sizeHint(self, option, index, *args, **kwargs):
        self.initStyleOption(option, index)
        html = QtGui.QTextDocument()
        html.setHtml(option.text)
        return QtCore.QSize(int(html.size().width()), int(html.size().height()))

class QWebPageChrome(QtWebKit.QWebPage):
    def __init__(self, *args, **kwargs):
        QtWebKit.QWebPage.__init__(self, *args, **kwargs)

    def userAgentForUrl(self, url):
        return "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"

class TourneyItem(QtGui.QListWidgetItem):
    FORMATTER_SWISS_OPEN = unicode(util.readfile("tournaments/formatters/open.qthtml"))

    def __init__(self, parent, uid, *args, **kwargs):
        QtGui.QListWidgetItem.__init__(self, *args, **kwargs)

        self.uid = int(uid)

        self.parent = parent

        self.type = None
        self.client = None
        self.title  = None
        self.description = None
        self.state  = None
        self.players = []
        self.playersname = []

        self.viewtext = ""
        self.height = 40
        self.setHidden(True)

    def update(self, message, client):
        '''
        Updates this item from the message dictionary supplied
        '''
        self.client  = client
        old_state       = self.state
        self.state      = message.get('state', "close")

        ''' handling the listing of the tournament '''
        self.title      = message['name']
        self.type       = message['type']
        self.url        = message['url']
        self.description    = message.get('description', "")
        self.players        = message.get('participants', [])

        if old_state != self.state and self.state == "started" :
            widget = QtWebKit.QWebView()
            webPage = QWebPageChrome()
            widget.setPage(webPage)
            widget.setUrl(QtCore.QUrl(self.url))
            self.parent.topTabs.addTab(widget, self.title)

        self.playersname= []
        for player in self.players :
            self.playersname.append(player["name"])
            if old_state != self.state and self.state == "started" and player["name"] == self.client.login :
                channel = "#" + self.title.replace(" ", "_")
                self.client.autoJoin.emit([channel])
                QtGui.QMessageBox.information(self.client, "Tournament started !", "Your tournament has started !\nYou have automatically joined the tournament channel.")

        playerstring = "<br/>".join(self.playersname)

        self.viewtext = self.FORMATTER_SWISS_OPEN.format(title=self.title, description=self.description, numreg=str(len(self.players)), playerstring=playerstring)
        self.setText(self.viewtext)

    def display(self):
        return self.viewtext

    def data(self, role):
        if role == QtCore.Qt.DisplayRole:
            return self.display()
        elif role == QtCore.Qt.UserRole :
            return self
        return super(TourneyItem, self).data(role)

    def permutations(self, items):
        """Yields all permutations of the items."""
        if items == []:
            yield []
        else:
            for i in range(len(items)):
                for j in self.permutations(items[:i] + items[i+1:]):
                    yield [items[i]] + j

    def __ge__(self, other):
        ''' Comparison operator used for item list sorting '''
        return not self.__lt__(other)

    def __lt__(self, other):
        ''' Comparison operator used for item list sorting '''
        if not self.client: return True # If not initialized...
        if not other.client: return False;

        # Default: Alphabetical
        return self.title.lower() < other.title.lower()


import json
import codecs
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr

#IMPORT LIBRARIES
wnom = importr('wnominate')
imjson = importr('RJSONIO')
r = robjects.r

#READ ARCHIVE
votaciones = r['read.csv']('./Data/nSalida.csv', encoding='utf-8')
votaciones2 =r['as.matrix'](votaciones).rx(True,-1).rx(True,-1).rx(True,-1) #Quitar 3 Columnas

#SAVE DATA
iD = r['matrix'](votaciones.rx(True,1))  #Rescatar Columna de iD
nombres = r['matrix'](votaciones.rx(True,2)) #Rescatar Columna de nombres
partidos = r['matrix'](votaciones.rx(True,3)) # #Rescatar Columna de partidos

#FORMAT WNOMINATE
rc = r.rollcall(votaciones2, yea = [1,2,3], nay = [4,5,6],
                missing = [7,8,9], notInLegis = 0,)
#Inicio de Calculos
result = wnom.wnominate(rc,polarity=robjects.IntVector((1,90)),minvotes = 10)
#Muestra Resultados
sum =r.summary(result)

#rescatar Coordenadas
cx = result[0][6]
cy = result[0][7]

#CREATE AND EXPORTATION ARCHIVE JSON
objects = {}
objects['Legislatura'] = []
for object in range(len(nombres)):
    objects['Legislatura'].append(
    {
        'Id_P':''+str(iD[object])+'',
        'Nombre': ''+ nombres[object]+'',
        'Partido': ''+partidos[object]+'',
        'X': ''+str(cx[object])+'',
        'Y': ''+str(cy[object])+''
        })
with open('./Data/Datos.json', 'w') as file:
    json.dump(objects, file, encoding='latin1')

new/Pruebas R/Prueba 5 -Stress/new-Rnominate.py


__author__    = "Individual contributors (see AUTHORS file)"
__date__      = "$DATE$"
__rev__       = "$REV$"
__license__   = "AGPL v.3"
__copyright__ = """
This file is part of the ESP Web Site
Copyright (c) 2007 by the individual contributors
  (see AUTHORS file)

The ESP Web Site is free software; you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Contact information:
MIT Educational Studies Program
  84 Massachusetts Ave W20-467, Cambridge, MA 02139
  Phone: 617-253-4882
  Email: esp-webmasters@mit.edu
Learning Unlimited, Inc.
  527 Franklin St, Cambridge, MA 02139
  Phone: 617-379-0178
  Email: web-team@learningu.org
"""
from esp.users.models import ContactInfo, ESPUser
from esp.program.models import ArchiveClass, ClassSubject, ClassCategories
from esp.utils.web import render_to_response
from django.db.models.query import QuerySet
from django.http import HttpResponse, Http404, HttpResponseNotAllowed, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from datetime import datetime

#    Two inputs to each function:
#    -    category: what you sort or view by
#        (such as: year, program, subject category, title)
#    -    options: the particular filter for that data
#        (eg. 2004, Splash, ComputerScience)

class ArchiveFilter(object):
    category = ""
    options = ""
    def __init__(self, category = "", options = ""):
        self.category = unicode(category)
        self.options  = unicode(options)

    def __unicode__(self):
        return '%s, %s' % (self.category, self.options)

def compute_range(postvars, num_records):
    default_num_records = 10
    results_start = 0
    results_end = None
    if 'results_start' in postvars:
        results_start = int(postvars['results_start'])
    if 'results_end' in postvars:
        results_end = int(postvars['results_end'])
    if (num_records > default_num_records) and results_end == None:
        if postvars.get('max_num_results') == "Show all":
            results_end = num_records
        elif postvars.get('max_num_results'):
            results_end = results_start + int(postvars['max_num_results'])
        else:
            results_end = results_start + default_num_records

    return {'start': results_start, 'end': results_end}

def extract_criteria(postvars):
    #    Use filters
    criteria = []
    for key in postvars.keys():
        if len(postvars[key]) > 0 and key.find("filter_") != -1 and len(key) > 7: criteria.append(ArchiveFilter(category = key[7:], options = postvars[key]))

    return criteria

def filter_archive(records, criteria):
    result = records
    for c in criteria:
        if c.category == 'year':
            result = result.filter(year = c.options)
        elif c.category == 'program':
            result = result.filter(program__iexact = c.options)
        elif c.category == 'title':
            result = result.filter(title__istartswith = c.options)
        elif c.category == 'category':
            result = result.filter(category__istartswith = c.options)
        elif c.category == 'teacher':
            result = result.filter(teacher__icontains = c.options)
        elif c.category == 'description':
            result = result.filter(description__icontains = c.options)

    return result

def title_heading(title_content):
    if len(title_content) > 0:
        return title_content[0]
    else:
        return '[No Title]'

def archive_classes(request, category, options, sortorder = None):
    context = {'selection': 'Classes'}
    context['category'] = category
    context['options'] = options

    #    Take filtering criteria both from form and from URL
    if category != None and options != None:
        url_criterion = ArchiveFilter(category = category, options = options)
        criteria_list = [url_criterion]
    else:
        criteria_list = []

    criteria_list += extract_criteria(request.POST)
    criteria_list += extract_criteria(request.GET)

    category_list = list(ArchiveClass.objects.all().values_list('category', flat=True).distinct())
    program_list = list(ArchiveClass.objects.all().values_list('program', flat=True).distinct())

    category_dict = {}
    classcatList = ClassCategories.objects.all()
    for letter in map(chr, range(65, 91)):
        category_dict[letter] = 'Unknown Category'

    for category in classcatList:
        category_dict[category.category[0].upper()] = category.category

    filter_keys = {'category': [{'name': c, 'value': c, 'selected': False} for c in category_list],
            'year': [{'name': str(y), 'value': str(y), 'selected': False} for y in range(1998, datetime.now().year + 1)],
            'title': [{'name': 'Starts with ' + letter, 'value': letter, 'selected': False} for letter in map(chr, range(65,91))],
             'program': [{'name': p, 'value': p, 'selected': False} for p in program_list],
            'teacher': [{}],
            'description': [{}]
            }
    if 'filter_teacher' in request.POST: filter_keys['teacher'][0]['default_value'] = request.POST['filter_teacher']
    if 'filter_description' in request.POST: filter_keys['description'][0]['default_value'] = request.POST['filter_description']

    results = filter_archive(ArchiveClass.objects.all(), criteria_list)

    #    Sort the results by the specified order
    if (not isinstance(sortorder, list)) or len(sortorder) < 1:
        sortorder = ['year', 'category', 'program', 'title', 'teacher', 'description']

    sortorder.reverse()
    for parameter in sortorder:
        results = results.order_by(parameter)
    sortorder.reverse()

    context['sortorder'] = sortorder

    for c in criteria_list:
        for k in filter_keys[c.category]:
            if 'name' in k and 'value' in k:
                if c.options == k['value']:
                    k['selected'] = True

    context['sortparams'] = [{'name': k, 'options': filter_keys[k]} for k in filter_keys.keys()]

    #    Display the appropriate range of results
    postvars = request.POST.copy()
    relevant_keys = ['max_num_results', 'results_start', 'results_end']
    for k in relevant_keys:
        if k in request.GET:
            postvars[k] = request.GET[k]

    res_range = compute_range(postvars, results.count())
    context['results_range'] = res_range

    #    Deal with the Django bug preventing you from using no "end"
    if res_range['end'] is None:
        res_range['end'] = results.count()

    #    Rename all of the class categories and uppercase the programs
    for entry in results[res_range['start']:res_range['end']]:
        entry.category = category_dict[entry.category[:1].upper()]
        entry.program = entry.program.upper()
        #    entry.title = entry.title.capitalize()

    #    Compute the headings for the 'jump to category' part
    if sortorder[0] == 'title':
        headings = [title_heading(item.__dict__['title']) for item in results[res_range['start']:res_range['end']]]
    else:
        headings = [item.__dict__[sortorder[0]] for item in results[res_range['start']:res_range['end']]]

    context['headings'] = list(set([unicode(h) for h in headings]))
    context['headings'].sort()

    #    Fill in context some more
    context['num_results'] = results.count()
    context['results'] = list(results[res_range['start']:res_range['end']])
    context['results_range']['start'] = context['results_range']['start'] + 1
    if res_range['end'] > context['num_results']:
        context['results_range']['end'] = context['num_results']
    if 'max_num_results' in request.POST:
        context['max_num_results'] = request.POST['max_num_results']
    else:
        context['max_num_results'] = '25'
    context['num_results_list'] = ['10', '25', '50', '100', '250', 'Show all']
    context['num_results_shown'] = len(context['results'])

    return render_to_response('program/archives.html', request, context)

def archive_teachers(request, category, options):
    context = {'selection': 'Teachers'}
    context['category'] = category
    context['options'] = options

    return render_to_response('program/archives.html', request, context)

def archive_programs(request, category, options):
    context = {'selection': 'Programs'}
    context['category'] = category
    context['options'] = options

    return render_to_response('program/archives.html', request, context)

archive_handlers = {    'classes': archive_classes,
            'teachers': archive_teachers,
            'programs': archive_programs,
        }


#File: CustomFunctions.py

def _euclidianAlgorithm(a, b):
        r = a%b
        if r == 0: return b
        return _euclidianAlgorithm(b, r)

def fibonacci(n):
        if n<0: raise ValueError("The Fibonacci sequence is only defined for n>=0.")
        elif n == 0: return 0
        elif n == 1: return 1
        f0 = 0
        f1 = 1
        i = 1
        while i < n:
                fSum = f1 + f0
                f0 = f1
                f1 = fSum
                i += 1
        return f1

def floatNumberToString(floatN, precision=0):
        numberStr = str(floatN)
        pointIndex = numberStr.find('.')
        if pointIndex<0: return numberStr
        if precision<=0: return numberStr[0: pointIndex]
        return numberStr[0: pointIndex+precision+1]

def greatestCommonDivisor(a, b):
        if type(a) is not int:
                raise ValueError("The gcd is defined for integers only. Received "
                                 + str(a) + ".")
        if type(b) is not int:
                raise ValueError("The gcd is defined for integers only. Received "
                                 + str(b) + ".")
        a = abs(a)
        b = abs(b)
        if a==0 or b==0: return -1
        if a < b: a,b = b,a
        return _euclidianAlgorithm(a, b)

def isLeapYear(year):
        if year == 0: raise ValueError("Year 0 does not exist.")
        return year%4==0 and year%100!=0 or year%400==0

def leastCommonMultiple(a, b):
        if a==0 or b==0: return -1
        else: return int(abs(a*b)/greatestCommonDivisor(a, b))


# a program to calculate the volume and surface area of a sphere when provided its radius
import math

def sphereArea(radius):
	a = 4 * (math.pi * (radius ** 2))
	return a

def sphereVolume(radius):
	a = 4/3 * (math.pi * (radius ** 3))
	return a
def main():
	#print the greeting and what the program does to the user
	print("This application will give us the Volume and Surface of a Sphere when radius is given as input")
	radius = eval(input("What is the radius of the sphere: "))

	volume = sphereVolume(radius)
	area = sphereArea(radius)

	print("The volume of your sphere is: ", volume)
	print("The area of your sphere is: ", area)
main()


from Core.entrada import main
from Components.messages import *

if __name__ == "__main__":
    while True:
        print('\nBEM VINDO AO SISTEMA ANDRÔMEDA PORTO DE SUPORTE')
        sep('*', 30)
        escolha = input('Escolha uma opção:\n1. Inicializar\n2. Sair\n')

        if escolha == '1':
            main()
        elif escolha == '2':
            print('Você saiu da aplicação.')
            break
        else:
            print('Opção inválida. Tente novamente.')


# 990. 等式方程的可满足性

class Solution:
    def equationsPossible(self, equations: list[str]) -> bool:
        connected = {}
        def find(a):
            while connected.get(a) and a != connected.get(a):
                connected[a] = connected.get(connected[a], connected[a])
                a = connected[a]
            return a

        def union(a, b):
            root_a = find(a)
            root_b = find(b)
            connected[root_a] = root_b

        def is_connected(a, b):
            return find(a) == find(b)

        for equation in equations:
            if equation[1] == "=":
                a, b = equation[0], equation[-1]
                union(a, b)

        for equation in equations:
            if equation[1] == "!":
                a, b = equation[0], equation[-1]
                if is_connected(a, b): return False

        return True

s = Solution()
print(s.equationsPossible(["a==b", "b==c", "d==e", "e==f", "d==a", "f!=a"]))


import numpy as np

d = 'multimodalb_10'

for fold in range(10):
    fold_id = '{:02d}'.format(fold+1)

    sfile = open( d + '/trainlist'+fold_id+'.txt' , 'r' )
    dlist = list()
    for line in sfile:
        dlist.append( line.strip('\n') )
    np.save( d + '/trainlist'+fold_id+'.npy' , np.array(dlist) )
    sfile.close()

    sfile = open( d + '/testlist' +fold_id+'.txt' , 'r' )
    dlist = list()
    for line in sfile:
        dlist.append( line.strip('\n') )
    np.save( d + '/testlist' +fold_id+'.npy' , np.array(dlist) )
    sfile.close()


import math

cnt=0

def test(i):
    tmp=str(i)
    for i in range(0,len(tmp)):
        if tmp[i]!=tmp[len(tmp)-1-i]:
            return False
    return True

def isproperty(i):
    tcnt=0
    while (tcnt<50):
        tmp=str(i)
        for j in range(0,len(tmp)):
            i+=(10**j)*int(tmp[j])
        if(test(i)):
            return True
        tcnt+=1
    return False

for i in range(1,10000):
    if(isproperty(i)):
        cnt+=1

print(9999-cnt)


from datetime import datetime
from click import style
import pandas as pd
from tabulate import tabulate
from process_dataset import process_data
from utility import print_scores, print_result
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier, AdaBoostClassifier, RandomForestClassifier, VotingClassifier

def naive_bayes_classifier():
    print ("TRAINING NAIVE BAYES ALGORITHM")
    start_time = datetime.now()
    mnb = MultinomialNB().fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()), 'Score on train (Naive Bayes)', 'Score on test (Naive Bayes)', str(mnb.score(x_train,y_train)), str(mnb.score(x_test,y_test)))

def logistic_regression_classifier():
    print ("TRAINING LOGISTIC REGRESSION ALGORITHM")
    start_time = datetime.now()
    lr = LogisticRegression(max_iter=1000).fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (Logistic Regression)', 'Score on test (Logistic Regression)', str(lr.score(x_train,y_train)), str(lr.score(x_test,y_test)))

def k_nearest_neighbors_classifier():
    print ("TRAINING K-NEAREST NEIGHBOR ALGORITHM")
    start_time = datetime.now()
    knn = KNeighborsClassifier(algorithm = 'brute', n_jobs=-1).fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (K-Nearest Neighbor)', 'Score on test (K-Nearest Neighbor)', str(knn.score(x_train,y_train)), str(knn.score(x_test,y_test)))

def support_vector_machine_classifier():
    print ("TRAINING SUPPORT VECTOR MACHINE ALGORITHM")
    start_time = datetime.now()
    svm = LinearSVC(C=0.0001).fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (Support Vector Machine)', 'Score on test (Support Vector Machine)', str(svm.score(x_train,y_train)), str(svm.score(x_test,y_test)))

def decision_tree_classifier():
    print ("TRAINING DECISION TREE CLASSIFIER")
    start_time = datetime.now()
    dt = DecisionTreeClassifier().fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (Decision Tree)', 'Score on test (Decision Tree)', str(dt.score(x_train,y_train)), str(dt.score(x_test,y_test)))

def bagging_decision_tree_classifier():
    print ("TRAINING BAGGING DECISION TREE CLASSIFIER")
    # max_samples = maximum size 0.5=50% of each sample taken from the full dataset
    # max_features =  maximum of features 1=100% taken here all 10K
    # n_estimators: number of decision trees
    start_time = datetime.now()
    bdt = BaggingClassifier(DecisionTreeClassifier(),max_samples=0.5,max_features=1.0,n_estimators=10).fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (Bagging Decision Tree)', 'Score on test (Bagging Decision Tree)', str(bdt.score(x_train,y_train)), str(bdt.score(x_test,y_test)))

def boosting_decision_tree_classifier():
    print ("TRAINING BOOSTING DECISION TREE CLASSIFIER")
    start_time = datetime.now()
    adb = AdaBoostClassifier(DecisionTreeClassifier(min_samples_split=10,max_depth=4), n_estimators=10,learning_rate=0.6).fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (Boosting Decision Tree)', 'Score on test (Boosting Decision Tree)', str(adb.score(x_train,y_train)), str(adb.score(x_test,y_test)))

def random_forest_classifier():
    print ("TRAINING RANDOM FOREST CLASSIFIER")
    start_time = datetime.now()
    rf = RandomForestClassifier(n_estimators=30, max_depth=9).fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (Random Forest)', 'Score on test (Random Forest)', str(rf.score(x_train,y_train)), str(rf.score(x_test,y_test)))

def voting_classifier():
    print ("TRAINING VOTING CLASSIFIER")
    mnb = MultinomialNB()
    lr = LogisticRegression()
    rf = RandomForestClassifier(n_estimators=30, max_depth=9)
    svm = LinearSVC(C=0.0001)
    start_time = datetime.now()
    evc = VotingClassifier(estimators=[('mnb', mnb), ('lr', lr), ('rf', rf), ('svm', svm)], voting='hard').fit(x_train, y_train)
    end_time = datetime.now()
    print_scores(str((end_time - start_time).total_seconds()),'Score on train (Voting Classifier)', 'Score on test (Voting Classifier)', str(evc.score(x_train,y_train)), str(evc.score(x_test,y_test)))

def main():
    naive_bayes_classifier()
    logistic_regression_classifier()
    k_nearest_neighbors_classifier()
    support_vector_machine_classifier() # due to large number of features we use LinearSVC
    decision_tree_classifier()
    # bagging_decision_tree_classifier()
    # boosting_decision_tree_classifier()
    random_forest_classifier()
    voting_classifier()
    print_result()

if __name__ == "__main__":
    print ("------------------")
    x_train, y_train, x_test, y_test = process_data()
    main()


from geopy.distance import geodesic
from itertools import permutations
import math

def calculate_distance(coords1, coords2):
    return geodesic(coords1, coords2).kilometers

def calculate_total_distance(route, points):
    total_distance = 0
    for i in range(len(route) - 1):
        total_distance += calculate_distance(points[route[i]], points[route[i+1]])
    return total_distance

def nearest_neighbor_algorithm(points):
    n = len(points)
    unvisited = list(range(n))
    route = [0]  # Start at the first point (index 0)
    unvisited.remove(0)

    while unvisited:
        nearest_point = min(unvisited, key=lambda x: calculate_distance(points[route[-1]], points[x]))
        route.append(nearest_point)
        unvisited.remove(nearest_point)

    route.append(0)  # Return to the starting point to complete the cycle
    return route

def find_optimal_route(points):
    n = len(points)
    optimal_route = None
    min_distance = float('inf')

    for perm in permutations(range(1, n)):
        route = [0] + list(perm) + [0]
        total_distance = calculate_total_distance(route, points)
        if total_distance < min_distance:
            min_distance = total_distance
            optimal_route = route

    return optimal_route, min_distance

def distance(point1, point2):
    lat1, lng1 = point1
    lat2, lng2 = point2
    return math.sqrt((lat2 - lat1)**2 + (lng2 - lng1)**2)

def nearest_neighbor_tsp(points, start_point):
    tour = [start_point]
    remaining_points = set(points)
    remaining_points.remove(start_point)

    while remaining_points:
        nearest_point = min(remaining_points, key=lambda x: distance(tour[-1], x))
        tour.append(nearest_point)
        remaining_points.remove(nearest_point)

    #tour.append(start_point) # enable for return to starting point

    return tour

def approximatePathOrdering(datasetFeatures):
    # Using Nearest Neighbor algorithm to get an approximate optimal route
    gps_points = []
    for feature in datasetFeatures:
        gps_points.append((feature['geometry']['coordinates'][1], feature['geometry']['coordinates'][0]))

    #nn_route = nearest_neighbor_algorithm(gps_points)
    starting_point = (51.50448,-0.07662)

    # Find the optimal tour starting from the chosen data point
    ordered_gps_points = nearest_neighbor_tsp(points=gps_points, start_point=starting_point)
    print("Nearest Neighbor route calculated")
    return ordered_gps_points


from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove

async def kb_return() -> ReplyKeyboardMarkup:

    kb = InlineKeyboardMarkup()
    button = InlineKeyboardButton("Вернуться", callback_data="return")
    kb.add(button)

    return kb

async def kb_remove():
    return ReplyKeyboardRemove()


from typing import Union
from fastapi import Depends, HTTPException, Response, status
from fastapi.security import HTTPBearer

from src.database import AsyncSession, get_db_session
from src import models
from src.utils import VerifyToken
from src.crud.crud_user import UserCRUD

token_auth_scheme = HTTPBearer()

async def get_current_user(
        response: Response,
        user_crud: UserCRUD = Depends(),
        token: str = Depends(token_auth_scheme)
) -> Union[models.User, Response]:
    pyload_from_auth = await VerifyToken(token.credentials).verify_token_from_auth0()

    if pyload_from_auth.get("status"):
        pyload_from_me = await VerifyToken(token.credentials).verify_token_from_me()
        if pyload_from_me.get("status"):
            response.status_code = status.HTTP_400_BAD_REQUEST
            return response

        user = await user_crud.get_user_by_email(email=pyload_from_me.get("email"))
        if not user:
            raise HTTPException(status_code=404, detail="User not found")
        return user

    user = await user_crud.get_user_by_email(email=pyload_from_auth.get("email"))
    if not user:
        user = await user_crud.create_user_by_email(email=pyload_from_auth.get("email"))
    return user


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep  5 02:18:20 2019

@author: rajdua
"""

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('train.csv')
a = list(df.columns)
b = a[0:79]
a = a[79:127]

med  = df[a]
med = med.sum(axis = 1)

df['Product_Info_2'] = pd.Categorical(df['Product_Info_2'])
dfDummies = pd.get_dummies(df['Product_Info_2'], prefix = 'P2')
df = pd.concat([df, dfDummies], axis=1)

train = df[b]
train = train.drop(columns = 'Product_Info_2')
train['Response'] = df['Response']
train['Keyword'] = med
#train = pd.concat([train, dfDummies], axis=1)
# train = pd.concat([df, dfDummies], axis=1)

df = pd.read_csv('test.csv')

a = list(df.columns)
b = a[0:79]
a = a[79:127]

med  = df[a]
med = med.sum(axis = 1)

df['Product_Info_2'] = pd.Categorical(df['Product_Info_2'])
dfDummies = pd.get_dummies(df['Product_Info_2'], prefix = 'P2')
df = pd.concat([df, dfDummies], axis=1)

test = df[b]
test = test.drop(columns = 'Product_Info_2')
test['Keyword'] = med
# test = pd.concat([test, dfDummies], axis = 1)
# test = pd.concat([df, dfDummies], axis=1)


import csv
import sqlite3
from cs50 import SQL
from sys import argv, exit

if len(argv) == 2:
    db = SQL("sqlite:///students.db")
    lsts = db.execute("SELECT * FROM students WHERE house = (?) ORDER BY last, first", argv[1])
    for lst in lsts:
        if lst['middle'] == None:
            print(f"{lst['first']} {lst['last']}, born {lst['birth']}")
        else:
            print(f"{lst['first']} {lst['middle']} {lst['last']}, born {lst['birth']}")

else:
    print(f"Error there should be 2 argv, you have {argv}")
    exit(1)


'''
Created on Feb 8, 2018

@author: stelios
'''
from base.base_table import BaseTable
import adql
import logging
from astropy.table import Table as astropy_Table
import config as config

class AdqlTable(BaseTable):
    """
    classdocs
    """

    def __init__(self, adql_schema, json_object=None, url=None):
        """
        Constructor
        """
        super().__init__(adql_schema, json_object, url)

    def select_columns(self):
        column_list = []
        json_list = self.get_json(self.url + "/columns/select")

        for column in json_list:
            column_list.append(adql.AdqlColumn(json_object=column, adql_table=self))

        return column_list

    def select_column_by_ident(self, ident):
        return adql.AdqlColumn(adql_table=self, url=ident)

    def select_column_by_name(self, column_name):
        """Get table by name

        Parameters
        ----------
        table_name: string, required
            The name of the Table being searched

        Returns
        -------
        table_list: list
            List of table names
        """
        response_json = {}
        try :
            response_json = self.get_json(  self.url + "/columns/select", { "adql.column.name": column_name })
        except Exception as e:
            logging.exception(e)

        return adql.AdqlColumn(json_object = response_json, adql_table=self)

    def create_adql_column(self, column_name):
        return

    def count(self):
        """Get Row count

        Returns
        -------
        rowcount: integer
            Count of rows
        """
        rowcount=-1

        try:
            if (self.json_object!=None):
                rowcount = self.json_object.get("metadata",[]).get("adql",[]).get("count",None)
        except Exception as e:
            logging.exception(e)

        return rowcount

    def as_astropy (self, limit=True):
        """Get Astropy table

        Returns
        -------
        astropy_table: Astropy.Table
            Table as Astropy table
        """
        if (limit):
            if (self.count()>config.maxrows):
                raise Exception ("Max row limit exceeded")
            else :
                return astropy_Table.read(self.url + "/votable", format="votable",use_names_over_ids=True)
        else:
            return astropy_Table.read(self.url + "/votable", format="votable", use_names_over_ids=True)


result = []
for i in range(15):
    if i == 0:
        result.append(list(range(1,15)))
    else :
        result2 = []
        for j in range(14):
            result2.append(sum(result[i-1][:j+1]))
        result.append(result2)

T = int(input())
for i in range(T):
    k = int(input())
    n = int(input())
    print(result[k][n-1])


import tensorflow as tf
import numpy as np

class HandMotion():
    def __init__(self, class_names, modelfile):
        self.handmotion = np.zeros((50, 42))
        self.interprefer = tf.lite.Interpreter(model_path=modelfile)
        self.interprefer.allocate_tensors()
        self.input_details = self.interprefer.get_input_details()
        self.output_details = self.interprefer.get_output_details()
        # self.class_names = ['BYE', 'COMEON', 'PUSH', 'STONEPAPER', 'SWIPE']
        self.class_names = class_names

    def input(self, hands, framesize):
        for hand in hands:
            self.handmotion = np.delete(self.handmotion, 0, 0)
            self.handmotion = np.concatenate([self.handmotion, np.concatenate([np.array(hand.landmarks / framesize)[:,0].reshape(21), np.array(hand.landmarks / framesize)[:,1].reshape(21)], axis=0).reshape(1, 42)], axis=0)

    def predict(self):
        self.interprefer.set_tensor(self.input_details[0]['index'], self.handmotion.reshape(1, 50, 42).astype(np.float32))
        self.interprefer.invoke()
        output_data = self.interprefer.get_tensor(self.output_details[0]['index'])
        return self.class_names[np.argmax(output_data)]

    def run(self, hands, framesize):
        self.input(hands, framesize)
        return self.predict()


import io
import re

from setuptools import find_packages
from setuptools import setup

with io.open("src/upparat/__init__.py", "rt", encoding="utf8") as f:
    version = re.search(r'__version__ = "(.*?)"', f.read()).group(1)

setup(
    name="upparat",
    version=version,
    description="Update Service for AWS IoT Core",
    url="https://github.com/caruhome/upparat",
    author="Livio Bieri / Reto Aebersold",
    long_description_content_type="text/markdown",
    long_description="The Upparat is a secure and robust service that runs on your IoT device to download and install files such as firmware updates. [Learn more](https://github.com/caruhome/upparat).",  # noqa
    license="MIT",
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Intended Audience :: System Administrators",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Operating System :: POSIX",
        "Programming Language :: Python :: 3",
        "Topic :: System :: Installation/Setup",
        "Topic :: System :: Operating System",
    ],
    python_requires=">=3.6",
    packages=find_packages("src"),
    package_dir={"": "src"},
    include_package_data=True,
    entry_points={"console_scripts": ["upparat=upparat.cli:main"]},
    install_requires=["paho-mqtt>=1.4.0", "backoff>=1.8.1", "pysm>=0.3.7"],
    extras_require={
        "dev": ["pytest", "pytest-mock", "boto3", "ipdb", "coverage"],
        "sentry": ["sentry-sdk"],
    },
)


# files to be ignored
binary_files = [
    "lgt",
    "inf",
    "map",
    "mat",
    "bmp",
    "wav",
    "ogg",
    "tga",
    "png",
    "hg2",
    "hgt",
    "geo",
    "sdf",
    "vdf",
    "mesh",
    "skeleton",
    "py",
]

textchars = bytearray({7, 8, 9, 10, 12, 13, 27} | set(range(0x20, 0x100)) - {0x7F})
is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))

def is_binary_file(file):
    bfile = False
    for ext in binary_files:
        if file.lower().endswith(ext):
            return True
    return is_binary_string(open(file, "rb").read(1024))


import gc
import glob
import os
import random
import sys

import numpy as np

import pcl

import config
from utils.reader import Reader

def augment(xyzs):
    axyz = np.ones((len(xyzs), 4))
    axyz[:, :3] = xyzs
    return axyz

def estimate(xyzs):
    return np.linalg.svd(xyzs)[-1][-1, :]

def run_ransac(data, sample_size, goal_inliers, max_iterations,
               stop_at_goal=True, random_seed=None, thresh=0.05):
    data = augment(data)

    best_ic = 0
    best_model = None
    if random_seed:
        random.seed(random_seed)

    idx = list(range(data.shape[0]))
    for i in range(max_iterations):
        s = data[random.sample(idx, sample_size)]
        m = estimate(s)
        ic = np.count_nonzero(np.abs(m.dot(data.T)) < thresh)
        if ic > best_ic:
            best_ic = ic
            best_model = m
            if stop_at_goal and ic > goal_inliers:
                break

    return best_model, best_ic

def calculate_ransac(name):
    print("calculating ransac planes for", name)
    reader = Reader(name, color=False, infra=False, coords=True)

    good_idx = np.zeros(shape=(reader.coords.shape[0], reader.coords.shape[1]), dtype=np.bool)
    planes = np.ndarray(shape=(reader.coords.shape[0], 4), dtype=np.float32)

    for i in range(reader.coords.shape[0]):
        coords = reader.coords_not_nan(i)
        p = pcl.PointCloud(coords)
        seg = config.make_segmenter(p)
        indices, model = seg.segment()

        del p
        gc.collect()
        sys.stdout.write(".")
        sys.stdout.flush()

        good_idx[i, indices] = True
        planes[i, :] = model

        # DEBUG: show segmented cloud
        if config.debug_step4:
            from utils.pcl import draw_pointcloud, color_inlier
            draw_pointcloud(coords, colors=color_inlier(coords, indices), plane=model)

    print(" done.")
    np.save(reader.path("good_idx", ext="npy"), good_idx, allow_pickle=False)
    np.save(reader.path("planes", ext="npy"), planes, allow_pickle=False)

def calculate_all_ransac():
    folders = glob.glob(os.path.join(config.data_directory, "**/coords.npy"))
    for folder in folders:
        dir = os.path.dirname(folder)
        if os.path.exists(dir + "/planes.npy"):
            continue

        calculate_ransac(dir)

if __name__ == "__main__":
    calculate_all_ransac()


from django.shortcuts import render
import xml.dom.minidom, os, shelve, pickle, datetime, sys, time
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
try:
    from names.models import Name, LegacyName, SpeciesMeta
except:
    print ("should be available in models")
from .models import *
from django.views.decorators.cache import cache_page
from .__init__ import VERSION

def index_legacy_simplified(request, template, category='nonvascular', mobile=False):
    print ("simplified ... non vascular", category)
    start = datetime.datetime.now()
    groups = [
        {"name": "Liverworts — Marchantiophyta", "pnid": 16002 , "fams":[], "species": 0, "published": 0},
        {"name": "Mosses — Bryophyta", "pnid":11876 , "fams":[], "species": 0, "published": 0},
        {"name": "Brown Algae — Ochrophyta", "pnid": 16479, "fams":[], "species": 0, "published": 0},
        {"name": "Red Algae — Rhodophyta", "pnid": 16478, "fams":[], "species": 0, "published": 0},
        {"name": "Green Algae — Chlorophyta", "pnid": 16477, "fams":[], "species": 0, "published": 0},
        {"name": "Lichens — Lichenophyta", "pnid": 16476, "fams":[], "species": 0, "published": 0},
        {"name": "Fungi", "pnid": 11998, "fams":[], "species": 0, "published": 0},
    ]
    total_spp = 0
    total_published = 0
    for gr in groups:
        g = Name.objects.get(pnid=gr["pnid"])
        fams = Name.objects.filter(upper = g).order_by('latname')
        for fam in fams:
            fid = fam.pnid
            family = {"latname": fam.latname, "fid": fid, "total_photos": 0, "total_species": 0, "species_pub": 0, "genera":[], "published":0}
            genera = Name.objects.filter(upper = fam).order_by('latname')
            for gen in genera:
                gid = gen.pnid
                genus = {"latname": gen.latname, "fid": fid, "gid": gid, "total_photos": 0, "total_species": 0, "species_pub": 0, "species":[], "published":0}
                species = Name.objects.filter(upper = gen).order_by('latname')
                for spec in species:
                    spid = spec.pnid

                    genus["total_photos"] += 0 ##
                    published_photos = NonVascularImage.objects.filter(plant=spec).filter(nr__gt=0).filter(nr__lt=100).count()
                    spe = {"latname": spec.latname, "fid": fid, "gid": gid, "pnid": spid,
                           "total_photos": 0, "published": published_photos,
                           "colnames": spec.colnames, "authors": spec.authors,
                            "modified": spec.modified.isoformat()[:19]}
                    if published_photos:
                        genus['published'] += published_photos
                        family['published'] += published_photos
                        family["total_species"] += 1
                        gr["published"] += published_photos
                        gr["species"] += 1
                        genus["species"].append(spe)
                if genus["published"]:
                    family["genera"].append(genus)
            if family["published"]:
                gr["fams"].append(family)
        print (gr.get("species"), gr.get("published"))
    ends = datetime.datetime.now()
    delta = ends - start
    print (delta)
    return render(request, template, locals())

def get_table(category):
    if category == 'vascular':
        table = VascularImage
    elif category == 'nonvascular':
        table = NonVascularImage
    elif category == 'animals':
        table = AnimalImage
    else:
        table = VariaImage
    return table

def get_status(meta):
    if meta.rare:
        if meta.rare == "WL":
            return "Watch Listed"
        elif meta.rare == "H":
            return "Historic"
        elif meta.rare == "SC":
            return "Special Concern"
        elif meta.rare == "T":
            return "Threatened"
        elif meta.rare == "E":
            return "Endangered"
        else:
            return meta.rare ## should never happen
    elif meta.invasive:
        return meta.invasive
    elif meta.introduced:
        return meta.introduced
    else:
        return "Native"

def species_mobile(request, spid, public=True):
    plant = Name.objects.get(pk=int(spid))
    try:
        meta = SpeciesMeta.objects.get(pk=int(spid))
        print ("got meta", meta)
        status = get_status(meta)
    except:
        meta = {}
        status = ""
    if not plant.level == 'species': ## rank
        return HttpResponseBadRequest("mobile version works only at species level")
    genname = plant.upper.latname
    latname = plant.latname
    category = plant.category
    table = get_table(category)
    irecs = table.objects.filter(plant=plant)
    if public:
        irecs = irecs.filter(nr__gt=0).filter(nr__lt=100).order_by('nr')
    else:
        irecs = irecs.order_by('nr')
    print ("finished species mobile for", spid, "got", len(irecs), "photos")
    return render (request, "photodb/thums_species_mobile.htm", locals())

def imgview_mobile(request, spid, imid, public=True):
    plant = Name.objects.get(pk=int(spid))
    genname = plant.upper.latname
    latname = plant.latname
    authors = plant.authors
    comname = plant.colnames
    table = get_table(plant.category)
    irecs = table.objects.filter(plant=plant)
    if public:
        irecs = irecs.filter(nr__gt=0).filter(nr__lt=100).order_by('nr')
    else:
        irecs = irecs.order_by('nr')
    total = len(irecs)
    for curnum in range(total):
        current = irecs[curnum]
        if current.imid == imid:
            try:
                previmid = irecs[curnum - 1].imid
            except:
                previmid = None
            try:
                nextimid = irecs[curnum + 1].imid
            except:
                nextimid = None
            break
    curnum += 1
    caption = current.caption
    locality = current.location
    date = current.date
    return render (request, "photodb/imgview_mobile.htm", locals())


from qiskit import QuantumCircuit, assemble, Aer
from math import pi, sqrt
import matplotlib
from matplotlib import pyplot as plt
from qiskit.visualization import plot_bloch_multivector, plot_histogram, circuit_drawer
sim = Aer.get_backend('aer_simulator')

# Let's do an X-gate on a |0> qubit
qc = QuantumCircuit(1)
qc.x(0)
qc.x(0)
qc.draw(output = "mpl" )
print(qc)

plt.show()

def had_test(gate_type,num_of_qubits, parameters):

    ancilla = QuantumRegister(1,'ancilla')
    qr = QuantumRegister(num_of_qubits,'q')
    cr = ClassicalRegister(1)
    circ =QuantumCircuit(ancilla,qr,cr)
    circ.h(ancilla[0])

    apply_fixed_ansatz(circ,qr[0:3], parameters)

    for i in range (0, len(gate_type[0])):
        if (gate_type[0][i] == 1):
            circ.cz(ancilla[0], qr[i])

    for i in range (0, len(gate_type[0])):
        if (gate_type[1][i] == 1):
            circ.cz(ancilla[0], qr[i])

    circ.h(ancilla[0])
    circ.measure(ancilla,cr)
    circ.draw(output="mpl")

had_test([[0, 0, 0], [0, 0, 1]], 3, [[1, 1, 1], [1, 1, 1], [1, 1, 1]])


import numpy as np
from mteb import MTEB
from sentence_transformers import SentenceTransformer
import os
import ctypes
from typing import Union, List
import sys

os.chdir(os.path.dirname(__file__))

MODEL_NAME = 'all-MiniLM-L6-v2'
if len(sys.argv) > 1:
    MODEL_NAME = sys.argv[1]

HF_PREFIX = ''
if 'all-MiniLM' in MODEL_NAME:
    HF_PREFIX = 'sentence-transformers/'
N_THREADS = 6

modes = ['q4_0', 'q4_1', 'f32', 'f16', 'sbert', 'sbert-batchless']

TASKS = [
    "STSBenchmark",
    "EmotionClassification",
]

os.environ["TOKENIZERS_PARALLELISM"] = "false" # Get rid of the warning spam from sbert tokenizer

class BertModel:
    def __init__(self, fname):
        self.lib = ctypes.cdll.LoadLibrary("../build/libbert.so")

        self.lib.bert_load_from_file.restype = ctypes.c_void_p
        self.lib.bert_load_from_file.argtypes = [ctypes.c_char_p]

        self.lib.bert_n_embd.restype = ctypes.c_int32
        self.lib.bert_n_embd.argtypes = [ctypes.c_void_p]

        self.lib.bert_free.argtypes = [ctypes.c_void_p]

        self.lib.bert_encode_batch.argtypes = [
            ctypes.c_void_p,
            ctypes.c_int32,
            ctypes.c_int32,
            ctypes.c_int32,
            ctypes.POINTER(ctypes.c_char_p),
            ctypes.POINTER(ctypes.POINTER(ctypes.c_float)),
        ]

        self.ctx = self.lib.bert_load_from_file(fname.encode("utf-8"))
        self.n_embd = self.lib.bert_n_embd(self.ctx)

    def __del__(self):
        self.lib.bert_free(self.ctx)

    def encode(self, sentences: Union[str, List[str]], batch_size: int = 16) -> np.ndarray:
        if isinstance(sentences, str):
            sentences = [sentences]

        n = len(sentences)

        embeddings = np.zeros((n, self.n_embd), dtype=np.float32)
        embeddings_pointers = (ctypes.POINTER(ctypes.c_float) * len(embeddings))(*[e.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) for e in embeddings])

        texts = (ctypes.c_char_p * n)()
        for j, sentence in enumerate(sentences):
            texts[j] = sentence.encode("utf-8")

        self.lib.bert_encode_batch(
            self.ctx, N_THREADS, batch_size, len(sentences), texts, embeddings_pointers
        )

        return embeddings

class BatchlessModel():
    def __init__(self, model) -> None:
        self.model = model

    def encode(self, sentences, batch_size=32, **kwargs):
        return self.model.encode(sentences, batch_size=1, **kwargs)

for mode in modes:
    if mode == 'sbert':
        model = SentenceTransformer(f"{HF_PREFIX}{MODEL_NAME}")
    elif mode == 'sbert-batchless':
        model = BatchlessModel(SentenceTransformer(f"{HF_PREFIX}{MODEL_NAME}"))
    else:
        model = BertModel(f'../models/{MODEL_NAME}/ggml-model-{mode}.bin')

    evaluation = MTEB(tasks=TASKS)
    output_folder = f"results/{MODEL_NAME}_{mode}"

    evaluation.run(model, output_folder=output_folder, eval_splits=["test"], task_langs=["en"])


import sys, re, subprocess, json
from math import *
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.spatial.distance import pdist,squareform
from scipy.cluster.hierarchy import linkage, dendrogram
from scipy.cluster import hierarchy

jsonFile = open(sys.argv[1], 'r')

print("Loading json file ..."),
probDict = json.load(jsonFile)
print("Done")

sortedRowNames = sorted(probDict.keys())
sortedColNames = sorted(probDict[sortedRowNames[0]].keys())

probArray = np.array([[probDict[rowName][colName] for colName in sortedRowNames] for rowName in sortedRowNames])

probDf = pd.DataFrame(probArray, columns = sortedRowNames, index = sortedRowNames)

'''
fig = plt.figure()

ax = fig.add_subplot(111)

cax = ax.matshow(probDf, interpolation='nearest', cmap='hot_r')
fig.colorbar(cax)

ax.set_xticklabels([''] + list(probDf.columns))
ax.set_yticklabels([''] + list(probDf.index))

plt.show()
'''

rowDist = pd.DataFrame(squareform(pdist(probDf, metric='euclidean')), columns=sortedRowNames, index=sortedRowNames)

rowClusters = linkage(pdist(probDf, metric='euclidean'), method='complete')

hierarchy.set_link_color_palette(['black'])

fig = plt.figure(figsize = (8,8))
axd = fig.add_axes([0.09,0.1,0.2,0.6])

rowDendr = dendrogram(rowClusters, orientation = 'right', color_threshold = np.inf,)
dfRowClust = probDf.ix[rowDendr['leaves'][::-1]]
print(rowDendr['leaves'])

axd.set_xticks([])
axd.set_yticks([])

for i in axd.spines.values():
        i.set_visible(False)

axm = fig.add_axes([0.26,0.1,0.6,0.6]) # x-pos, y-pos, width, height
cax = axm.matshow(probDf, interpolation='nearest')
fig.colorbar(cax)
plt.xticks(range(0, len(list(dfRowClust.columns))))
axm.set_xticklabels(list(dfRowClust.columns), rotation = 90, fontsize = 5, visible = True)
axm.yaxis.tick_right()
axm.set_yticklabels([''] + list(dfRowClust.index), fontsize = 5, visible = True)

plt.show()

#http://nbviewer.ipython.org/github/rasbt/pattern_classification/blob/master/clustering/hierarchical/clust_complete_linkage.ipynb

#Heatmaps in R:
#http://sebastianraschka.com/Articles/heatmaps_in_r.html


import os,sys
from imutils.video import VideoStream
from pyzbar import pyzbar
import argparse
import datetime
import imutils
import time
import cv2
#from barcode import EAN13
#from barcode.writer import ImageWriter
from qtpy.QtCore import Signal, QThread, QObject, Slot

class Camera(QObject):
    global camPtr
    global vid
    signal = Signal(str)
    def __init__(self):
        self.vid = cv2.VideoCapture(0)
        self.thread = QThread()
        super().__init__()
        self.thread.started.connect(self.loop)
        self.img = None

    @Slot()
    def loop(self):
        try:
            while True:
                _, frm = self.vid.read()
                frame = imutils.resize(frm, width=1024)
                image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                barcodes = pyzbar.decode(image)
                self.img = image
                if len(barcodes) > 0:
                    self.signal.emit(str(len(barcodes)))
                else:
                    self.signal.emit("")
                time.sleep(0.5)
        except Exception as e:
            print(str(e))

    def startLoop(self):
        self.moveToThread(self.thread)
        self.thread.start()

    def start(self):
        try:
            #camPtr = VideoStream(usePiCamera=True).start()
            camPtr = VideoStream(src=0).start()
            print("Got CamPtr as: ", camPtr)
        except Exception as err:
            print("Got Error while opening Camera interface :", err)
            sys.exit()
        return camPtr

    def capture(self):
        vid = cv2.VideoCapture(0)
        vid.read()
        vid.read()
        vid.read()
        vid.read()
        vid.read()
        vid.read()
        ret, frame = vid.read()
        frame = imutils.resize(frame, width=640)
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        vid.release()
        return image

    def Barcode(self):
        frame = self.capture()
        barcodes = pyzbar.decode(frame)
        if len(barcodes) > 0:
            print("barcode found - " + str(len(barcodes)))
        else:
            print("barcode not found")
        for barcode in barcodes:
            pass

    def getBarcode(self, camPtr):
        frame = camPtr.read()
        if frame is not None:
            frame = imutils.resize(frame, width=640)
        return frame