def __init__(self):
        super().__init__(None)
        self.sir = GlobalModel()
        self.ui = Ui_CoronaSimulationDialog()
        self.ui.setupUi(self)
        self.fig = Figure()
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.ax = self.canvas.figure.add_subplot(111)
        self.ax2 = self.ax.twinx()
        self.ui.figuresLayout.addWidget(self.canvas)
        self.showMaximized()
        self.simulation_days = 30
        self.days = []
        self.first_day = ''

        actual_data_file = QSettings().value("actual_data_file", "")
        if actual_data_file:
            self.load_actual_data_file(actual_data_file)

        r_data_file = QSettings().value("r_data_file", "")
        if r_data_file:
            self.load_r_data_file(r_data_file)

        connectivity_data_file = QSettings().value("connectivity_data_file",
                                                   "")
        if connectivity_data_file:
            self.load_connectivity_data_file(connectivity_data_file)

        self.start_execution()
示例#2
0
    def execute(pc, text, mnemonic):

        if mnemonic == 0xb8:        # mov ax, #1234
            val, = unpack('<H', text[1:3])
            Mov.mov2reg(pc, 'ax', val)
            pcInc = 3
        elif mnemonic == 0xbb:      # mov bx, #1234
            val, = unpack('<H', text[1:3])
            Mov.mov2reg(pc, 'bx', val)
            pcInc = 3
        elif mnemonic == 0xb9:      # mov cx, #1234
            val, = unpack('<H', text[1:3])
            Mov.mov2reg(pc, 'cx', val)
            pcInc = 3
        elif mnemonic == 0xb5:      # mov ch, #12
            val, = unpack('B', text[1])
            Mov.mov2reg(pc, 'ch', val)
            pcInc = 2
        elif mnemonic == 0xb1:      # mov cl, #12
            val, = unpack('B', text[1])
            Mov.mov2reg(pc, 'cl', val)
            pcInc = 2
        elif mnemonic == 0xc707:    # mov [bx], #1234
            Mov.mov2derefer(pc, 'bx', text[2:4], 0)
            pcInc = 4
        elif mnemonic == 0xc747:    # mov [bx+x], #1234
            Mov.mov2derefer(pc, 'bx', text[3:5], convNum(text[2], 1))
            pcInc = 5
        elif mnemonic == 0xc607:    # mov ptr [bx], #12
            Mov.mov2derefer(pc, 'bx', text[2], 0)
            pcInc = 3
        elif mnemonic == 0xc647:    # mov ptr [bx+x], #12
            Mov.mov2derefer(pc, 'bx', text[3], convNum(text[2], 1))
            pcInc = 4
        elif mnemonic == 0x8907:    # mov [bx], ax
            Mov.mov2derefer(pc, 'bx', pack('<H', gm.getReg('ax')), 0)
            pcInc = 2
        elif mnemonic == 0x8807:    # mov [bx], al
            Mov.mov2derefer(pc, 'bx', pack('B', gm.getReg('al')), 0)
            pcInc = 2
        elif mnemonic == 0x8867:    # mov [bx], ah
            Mov.mov2derefer(pc, 'bx', pack('B', gm.getReg('ah')), convNum(text[2], 1))
            pcInc = 3
        elif mnemonic == 0x890f:    # mov [bx], cx
            Mov.mov2derefer(pc, 'bx', pack('<H', gm.getReg('cx')), 0)
            pcInc = 3
        elif mnemonic == 0x894f:    # mov [bx+x], cx
            Mov.mov2derefer(pc, 'bx', pack('<H', gm.getReg('cx')), convNum(text[2], 1))
            pcInc = 3
        else:
            msg = "unknown register name specified: " + hex(mnemonic)
            raise Exception(msg)

        return pcInc
示例#3
0
    def execute(pc, text, mnemonic):

        if mnemonic == 0xb8:        # mov ax, #1
            val, = unpack('<H', text[1:3])
            Mov.mov2reg(pc, 'ax', val)
            pcInc = 3
        elif mnemonic == 0xbb:      # mov bx, #1
            val, = unpack('<H', text[1:3])
            Mov.mov2reg(pc, 'bx', val)
            pcInc = 3
        elif mnemonic == 0xb9:      # mov cx, #1
            val, = unpack('<H', text[1:3])
            Mov.mov2reg(pc, 'cx', val)
            pcInc = 3
        elif mnemonic == 0xc707:    # mov [bx], #1234
            Mov.mov2derefer(pc, 'bx', text[2:4], 0)
            pcInc = 4
        elif mnemonic == 0xc747:    # mov [bx+x], #1234
            Mov.mov2derefer(pc, 'bx', text[3:5], convNum(text[2], 1))
            pcInc = 5
        elif mnemonic == 0xc607:    # mov ptr [bx], #12
            Mov.mov2derefer(pc, 'bx', text[2], 0)
            pcInc = 3
        elif mnemonic == 0xc647:    # mov ptr [bx+x], #12
            Mov.mov2derefer(pc, 'bx', text[3], convNum(text[2], 1))
            pcInc = 4
        elif mnemonic == 0x8907:    # mov [bx], ax
            Mov.mov2derefer(pc, 'bx', pack('<H', gm.getReg('ax')), 0)
            pcInc = 2
        elif mnemonic == 0x894f:    # mov [bx+x], cx
            Mov.mov2derefer(pc, 'bx', pack('<H', gm.getReg('cx')), convNum(text[2], 1))
            pcInc = 3
        else:
            raise "unknown register name specified"

        return pcInc
示例#4
0
def testGlobalModel():
    f = open("a.out", "rb")
    header = f.read(16)
    tsize, dsize = unpack("<HH", header[2:6])

    text = f.read(tsize)

    print map(ord, text)
    print "tsize=%d, dsize=%d" % (tsize, dsize)
    gm.initData(len(text), text + f.read(dsize))

    print gm.getData(tsize, 11)
    print gm.setData(tsize, pack("<2B", 0x48, 0x45))
    print gm.getData(tsize, 11)

    gm.setReg("ax", 0b1100101011001100)
    gm.setReg("ah", 0b10101010)
    print bin(gm.getReg("ah"))
    print bin(gm.getReg("al"))
    gm.setReg("al", 0b00000000)
    print bin(gm.getReg("al"))
    print bin(gm.getReg("ax"))
示例#5
0
 def mov2derefer(pc, regname, val, displace):
     addr = gm.getReg(regname)
     gm.setData(addr+displace, val)
示例#6
0
 def mov2reg(pc, regname, val):
     gm.setReg(regname, val)
示例#7
0
 def subFromAddr(pc, addr, val, fmt, byte):
     baseVal, = unpack(fmt, gm.getData(addr, byte))
     result = baseVal - val
     gm.setData(addr, pack(fmt, result))
示例#8
0
 def mov2mem(pc, addr, val, displace):
     gm.setData(addr + displace, val)
示例#9
0
    def execute(pc, text, mnemonic):

        if mnemonic == 0xB8:  # mov ax, #1234
            val, = unpack("<H", text[1:3])
            Mov.mov2reg(pc, "ax", val)
            pcInc = 3
        elif mnemonic == 0xBB:  # mov bx, #1234
            val, = unpack("<H", text[1:3])
            Mov.mov2reg(pc, "bx", val)
            pcInc = 3
        elif mnemonic == 0xB9:  # mov cx, #1234
            val, = unpack("<H", text[1:3])
            Mov.mov2reg(pc, "cx", val)
            pcInc = 3
        elif mnemonic == 0xB5:  # mov ch, #12
            val, = unpack("B", text[1])
            Mov.mov2reg(pc, "ch", val)
            pcInc = 2
        elif mnemonic == 0xB1:  # mov cl, #12
            val, = unpack("B", text[1])
            Mov.mov2reg(pc, "cl", val)
            pcInc = 2
        elif mnemonic == 0xC707:  # mov [bx], #1234
            Mov.mov2derefer(pc, "bx", text[2:4], 0)
            pcInc = 4
        elif mnemonic == 0xC747:  # mov [bx+x], #1234
            Mov.mov2derefer(pc, "bx", text[3:5], convNum(text[2], 1))
            pcInc = 5
        elif mnemonic == 0xC607:  # mov ptr [bx], #12
            Mov.mov2derefer(pc, "bx", text[2], 0)
            pcInc = 3
        elif mnemonic == 0xC647:  # mov ptr [bx+x], #12
            Mov.mov2derefer(pc, "bx", text[3], convNum(text[2], 1))
            pcInc = 4
        elif mnemonic == 0x8907:  # mov [bx], ax
            Mov.mov2derefer(pc, "bx", pack("<H", gm.getReg("ax")), 0)
            pcInc = 2
        elif mnemonic == 0x8807:  # mov [bx], al
            Mov.mov2derefer(pc, "bx", pack("B", gm.getReg("al")), 0)
            pcInc = 2
        elif mnemonic == 0x8867:  # mov [bx], ah
            Mov.mov2derefer(pc, "bx", pack("B", gm.getReg("ah")), convNum(text[2], 1))
            pcInc = 3
        elif mnemonic == 0x890F:  # mov [bx], cx
            Mov.mov2derefer(pc, "bx", pack("<H", gm.getReg("cx")), 0)
            pcInc = 3
        elif mnemonic == 0x894F:  # mov [bx+x], cx
            Mov.mov2derefer(pc, "bx", pack("<H", gm.getReg("cx")), convNum(text[2], 1))
            pcInc = 3
        elif mnemonic == 0xC706:  # mov [0xNNNN], #1234
            addr, = unpack("<H", text[2:4])
            Mov.mov2mem(pc, addr, text[4:6], 0)
            pcInc = 6
        elif mnemonic == 0xC606:  # mov byte [0xNNNN], #1234
            addr, = unpack("<H", text[2:4])
            Mov.mov2mem(pc, addr, text[4], 0)
            pcInc = 5
        else:
            msg = "unknown register name specified: " + hex(mnemonic)
            raise Exception(msg)

        return pcInc
示例#10
0
 def execute(offset, length):
     data = gm.getData(offset, length)
     sys.stdout.write(data)
class CoronaSimulationDialog(QDialog):
    def __init__(self):
        super().__init__(None)
        self.sir = GlobalModel()
        self.ui = Ui_CoronaSimulationDialog()
        self.ui.setupUi(self)
        self.fig = Figure()
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.ax = self.canvas.figure.add_subplot(111)
        self.ax2 = self.ax.twinx()
        self.ui.figuresLayout.addWidget(self.canvas)
        self.showMaximized()
        self.simulation_days = 30
        self.days = []
        self.first_day = ''

        actual_data_file = QSettings().value("actual_data_file", "")
        if actual_data_file:
            self.load_actual_data_file(actual_data_file)

        r_data_file = QSettings().value("r_data_file", "")
        if r_data_file:
            self.load_r_data_file(r_data_file)

        connectivity_data_file = QSettings().value("connectivity_data_file",
                                                   "")
        if connectivity_data_file:
            self.load_connectivity_data_file(connectivity_data_file)

        self.start_execution()

    # noinspection PyUnusedLocal
    def x_formatter(self, x, pos):
        x = int(x)
        if 0 < x < len(self.days):
            return self.days[x]
        return ''

    # noinspection PyUnusedLocal
    @staticmethod
    def y_formatter(x, pos):
        return '{:,}'.format(int(x))

    def start_execution(self):
        if ActualData.empty():
            QMessageBox.warning(None, 'Warning', 'Actual data is empty')
            return

        if RData.empty():
            QMessageBox.warning(None, 'Warning', 'R data is empty')
            return

        base = datetime.datetime.strptime(ActualData.first_day, '%Y-%m-%d')
        self.days = [
            (base + datetime.timedelta(days=x)).strftime('%Y-%m-%d')
            for x in range(len(ActualData.actual_data) + self.simulation_days)
        ]

        t0 = len(ActualData.actual_infected)
        t = np.linspace(t0, t0 + self.simulation_days - 1,
                        self.simulation_days)
        i = self.sir.execute(t)

        self.ax.cla()
        self.ax.xaxis.grid()
        self.ax.yaxis.grid()
        self.ax.minorticks_on()
        self.ax.set_xticklabels(self.days, rotation=90)
        self.ax.xaxis.set_major_formatter(FuncFormatter(self.x_formatter))
        self.ax.yaxis.set_major_formatter(FuncFormatter(self.y_formatter))

        self.ax.plot(list(range(len(ActualData.actual_infected))),
                     ActualData.actual_infected,
                     label='Actual')
        t_for_i = [int(i) for i in t]
        t_for_i = t_for_i[:len(t_for_i) - 1]
        self.ax.plot(t_for_i, i, label='Expected')

        self.ax.set_xlim(0, len(ActualData.actual_data) + self.simulation_days)
        self.ax.legend(loc="upper right")
        self.canvas.draw_idle()

    def load_actual_data_file(self, file_name):
        if ActualData.load_file(file_name):
            self.ui.actualDataFileLineEdit.setText(file_name)

    def load_r_data_file(self, file_name):
        if RData.load_file(file_name):
            self.ui.RDataFileLineEdit.setText(file_name)

    def load_connectivity_data_file(self, file_name):
        if ConnectivityData.load_file(file_name):
            self.ui.connectivityFileLineEdit.setText(file_name)

    @pyqtSlot()
    def actual_data_file_selected(self):
        t = QFileDialog.getOpenFileName(self, filter="csv(*.csv)")
        file_name = t[0]
        if file_name:
            QSettings().setValue("actual_data_file", file_name)
            self.load_actual_data_file(file_name)
            self.start_execution()

    @pyqtSlot()
    def r_data_file_selected(self):
        t = QFileDialog.getOpenFileName(self, filter="csv(*.csv)")
        file_name = t[0]
        if file_name:
            QSettings().setValue("r_data_file", file_name)
            self.load_r_data_file(file_name)
            self.start_execution()

    @pyqtSlot()
    def connectivity_data_file_selected(self):
        t = QFileDialog.getOpenFileName(self, filter="csv(*.csv)")
        file_name = t[0]
        if file_name:
            QSettings().setValue("connectivity_data_file", file_name)
            self.load_connectivity_data_file(file_name)
            self.start_execution()

    @pyqtSlot()
    def estimate_R(self):
        if ActualData.empty():
            QMessageBox.warning(None, 'Warning', 'Actual data is empty')
            return

        communities_count = ActualData.actual_data.shape[1] // 3
        time_for_r = []
        super_r = None

        for i in range(communities_count):
            actual_infected = ActualData.actual_data[:, i * 3 + 1]
            actual_infected = [int(i) for i in actual_infected]
            incidents = [actual_infected[0]]
            incidents.extend(np.diff(actual_infected))
            r, rt = RtEstimator(incidents, 5.1, 1).estimR()
            rt = np.trim_zeros(rt)
            r = r[:len(rt)]
            time_for_r = rt
            if super_r is None:
                super_r = np.empty([len(rt), communities_count])
            super_r[:, i] = r

        t = QFileDialog.getSaveFileName(self, filter="csv(*.csv)")
        file_name = t[0]
        if file_name:
            RData.Rt, RData.Rt_t1 = super_r, time_for_r
            if RData.save_r_data_file(file_name):
                QSettings().setValue("r_data_file", file_name)
                self.load_r_data_file(file_name)

        self.start_execution()

    @pyqtSlot()
    def simulationDaysChanged(self):
        default = 30
        str = self.ui.lineEditSimulationDays.text()
        x = int(str)
        if x < 10:
            x = default
        if self.simulation_days != x:
            self.simulation_days = x
            self.start_execution()
示例#12
0
 def mov2derefer(pc, regname, val):
     addr = gm.getReg(regname)
     gm.setData(addr, val)