text:
import readcfg as r_d
from mpdaInstance import MPDAInstance
import os, sys
from popDecode.mpdaRobot import RobotState,Robot
from popDecode.mpdaTask import  Task
from popDecode.mpdaDecoderActSeq import ActionSeq,ActionTuple,EventType,MPDADecoderActionSeq
import numpy as np
from enum import Enum
from collections import  namedtuple

RobTaskPair = namedtuple('RobTaskPair',['robID','taskID'])

class CalType(Enum):
    arriveCond = 1
    leaveCond = 2
    endCond = 3
    backCond = 4
    stateInvalidCond = 5

def generateRandEncode(robNum,taskNum):
    encode = np.zeros((robNum, taskNum),dtype =int)
    for i in range(robNum):
        permLst = [x for x in range(taskNum)]
        np.random.shuffle(permLst)
        encode[i][:] = permLst
    return encode

def generateRandPopEncode(robNum,taskNum):
    pop = []
    encode = np.zeros((robNum, taskNum),dtype =int)
    for i in range(robNum):
        permLst = [x for x in range(taskNum)]
        np.random.shuffle(permLst)
        encode[i][:] = permLst
    robIndLst = [0 for _ in range(robNum)]

    while len(pop) == (robNum*taskNum):
        rdRobID = np.random.randint(0,robNum -1)
        rdTaskID = encode[rdRobID][robIndLst[rdRobID]]
        pop.append(RobTaskPair(rdRobID,rdTaskID))
        robIndLst[rdRobID] += 1

    '''
    there are still some problems to construct a
    '''

AbsolutePath = os.path.abspath(__file__)
# 将相对路径转换成绝对路径
SuperiorCatalogue = os.path.dirname(AbsolutePath)
# 相对路径的上级路径
BaseDir = os.path.dirname(SuperiorCatalogue)

degBoolean = False

class PopDecoder(object):
    def __init__(self,ins :MPDAInstance):
        self._insName = ins._insName
        # readCfg = rd.Read_Cfg(fileName)
        self._robNum = ins._robNum
        self._taskNum = ins._taskNum
            # int(readCfg.getSingleVal('taskNum'))
        self._threhold = ins._threhold
        self._robAbiLst  = ins._robAbiLst
        self._robVelLst = ins._robVelLst
        self._taskStateLst = ins._taskStateLst
        self._taskRateLst = ins._taskRateLst
        self._rob2taskDisMat = ins._rob2taskDisMat
        self._taskDisMat = ins._taskDisMat
        if degBoolean:
            self._degFile = open(BaseDir+ '/debugData/deg.dat', 'w')

class MPDATriDecoder(object):
    def __init__(self,ins :MPDAInstance):
        self._insName = ins._insName
        # readCfg = rd.Read_Cfg(fileName)
        self._robNum = ins._robNum
        self._taskNum = ins._taskNum
            # int(readCfg.getSingleVal('taskNum'))
        self._threhold = ins._threhold
        self._robAbiLst  = ins._robAbiLst
        self._robVelLst = ins._robVelLst
        self._taskStateLst = ins._taskStateLst
        self._taskRateLst = ins._taskRateLst
        self._rob2taskDisMat = ins._rob2taskDisMat
        self._taskDisMat = ins._taskDisMat
        if degBoolean:
            self._degFile = open(BaseDir+ '/debugData/deg.dat', 'w')

    def decode(self, x):
        self.encode = x
        # if self.decodeProcessor():
        while True:
            print('xxx')
            print(self.encode)
            self.initStates()
            self._actSeq = ActionSeq()
            validStateBoolean,cal_type = self.decodeProcessor()
            if cal_type == CalType.backCond:
                continue
            else:
                break
        if degBoolean:
            self._degFile.write(str(self.cmpltLst))
        # print(self._actSeq.convert2MultiPerm(self._robNum))
        return validStateBoolean, self._actSeq
    def initStates(self):
        '''
        initialize states of decode method
        '''
        self.taskLst = []
        self.robotLst = []
        self.cmpltLst = [False] * self._taskNum
        for i in range(self._robNum):
            rob = Robot()
            rob._ability = self._robAbiLst[i]
            rob._vel = self._robVelLst[i]
            rob.encodeIndex = 0
            rob.taskID, rob.encodeIndex, stopBool = self.getRobTask(robID=i, encodeIndex=0)
            if not stopBool:
                dis = self._rob2taskDisMat[i][rob.taskID]
                dis_time = dis / rob._vel
                rob.arriveTime = dis_time
            rob.stopBool = stopBool
            rob.stateType = RobotState['onRoad']
            rob.leaveTime = 0
            self.robotLst.append(rob)

        for i in range(self._taskNum):
            task = Task()
            task.cState = self._taskStateLst[i]
            task._initState = self._taskStateLst[i]
            task.cRate = self._taskRateLst[i]
            task._initRate = self._taskRateLst[i]
            task._threhod = self._threhold
            task.cmpltTime = sys.float_info.max
            self.taskLst.append(task)

        self.decodeTime = 0
        # self.validStateBool = True
    def decodeProcessor(self):
        while not self.allTaskCmplt():
            cal_type, actionID = self.findActionID()
            if cal_type  == CalType['arriveCond']:
                rob = self.robotLst[actionID]
                arriveTime = rob.arriveTime
                encodeInd = rob.encodeIndex
                taskID = self.encode[actionID][encodeInd]
                self._actSeq.append(ActionTuple(robID =actionID,taskID= taskID, eventType = EventType.arrive,eventTime = arriveTime))
                self.decodeTime = rob.arriveTime

                if self.cmpltLst[taskID]:
                    # =============================================================================
                    #  the task has been cmplt
                    # =============================================================================
                    # raise Exception('XXX')
                    self.arriveCmpltTask(actionID,encodeInd)
                    # rob = self.robotLst[actionID]
                    # rob.leaveTime = rob.arriveTime
                    # rob.stateType = RobotState['onTask']
                    # self._actSeq._arrCmpltTaskLst.append((actionID, taskID))
                else:
# =============================================================================
# the task has not been cmplt
# =============================================================================
                    task = self.taskLst[taskID]
                    rob.taskID = taskID
                    validStateBool = task.calCurrentState(arriveTime)
                    if not validStateBool:
                        break
                    task.cRate = task.cRate - rob._ability
# can not be cmplted
                    if task.cRate >= 0:
                        leaveTime = sys.float_info.max
# can be completed
                    else:
                        rob.executeDur = task.calExecuteDur()
                        rob.executeBool = False
                        leaveTime = rob.arriveTime + rob.executeDur
                        coordLst = self.findCoordRobot(actionID)
                        for coordID in coordLst:
                            coordRob = self.robotLst[coordID]
                            coordRob.leaveTime = leaveTime
                            coordRob.executeDur = coordRob.leaveTime - coordRob.arriveTime
                    rob.leaveTime = leaveTime
                    rob.stateType = RobotState['onTask']

            if cal_type == CalType['leaveCond']:
                rob = self.robotLst[actionID]
                taskID = rob.taskID
                task = self.taskLst[taskID]
                preTaskID = taskID
                self.cmpltLst[taskID] = True
                self._actSeq.append(ActionTuple(robID =actionID,taskID= taskID, eventType = EventType.leave,eventTime = rob.leaveTime))
                task.cmpltTime = rob.leaveTime
                self.decodetIme = rob.leaveTime
                coordLst = self.findCoordRobot(actionID)
                for coordID in coordLst:
                    self.updateRobLeaveCond(robID = coordID)
                    self._actSeq.append(ActionTuple(robID = coordID, taskID= taskID,eventType = EventType.leave,eventTime = task.cmpltTime))
                self.updateRobLeaveCond(robID = actionID)

                '''
                debug is here
                '''

                if degBoolean:
                    self._degFile.write(str(taskID) + ' have been completed\n')

            if cal_type == CalType.endCond:
                # invalidFitness = True
                validStateBool = False
                break
            if cal_type == CalType.backCond:
                break
        if not validStateBool:
            pass
            # print('the state is explosion')
        return  validStateBool,cal_type
    '''
    some fucntions
    '''

    def allTaskCmplt(self):
        if False in self.cmpltLst:
            return False
        else:
            return True

    def findActionID(self):
        cal_type = CalType['endCond']
        actionID = sys.float_info.max
        minTime = sys.float_info.max
        for i in range(self._robNum):
            rob = self.robotLst[i]
            if rob.stopBool != True:
                if rob.stateType == RobotState['onRoad']:
                    if rob.arriveTime < minTime:
                        minTime = rob.arriveTime
                        cal_type = CalType['arriveCond']
                        actionID = i
                if rob.stateType == RobotState['onTask']:
                    if rob.leaveTime < minTime:
                        minTime = rob.leaveTime
                        cal_type = CalType['leaveCond']
                        actionID = i
        if degBoolean:
            self.saveRobotInfo(degFile= self._degFile)
            self._degFile.write(str(actionID) + ' time = '+ str(minTime)
                              + ' type = ' + str(cal_type) + '\n')

        if minTime < self.decodeTime:
            cal_type = CalType['backCond']
            raise Exception('xxx')
        # self.saveEventInMemory()
        # if minTime < self.decodeTime:
        #     cal_type = CalType['backCond']
        #            print(minTime)
        #            print(self.decodeTime)
        #            taskID = self.robotLst[actionI].taskID
        #        self.saveRobotInfo()
        return cal_type, actionID

    def findCoordRobot(self, robID):
        '''
        find robots which are corrdinated with the robot A
        '''
        coordLst = []
        rob = self.robotLst[robID]
        taskID = rob.taskID
        for i in range(self._robNum):
            if i == robID:
                continue
            #            crob = self.robotLst[i]
            if self.robotLst[i].stateType == RobotState['onRoad']:
                continue
            if self.robotLst[i].stopBool == True:
                continue
            if self.robotLst[i].taskID == taskID:
                coordLst.append(i)
        return coordLst

    def updateRobLeaveCond(self,robID):
        rob = self.robotLst[robID]
        preTaskID = rob.taskID
        while True:
            if rob.encodeIndex == (self._taskNum - 1):
                rob.stopBool = True
                break
            rob.encodeIndex += 1
            taskID = self.encode[robID][rob.encodeIndex]
            if self.cmpltLst[taskID]:
                continue
            else:
                roadDur = self.calRoadDur(preTaskID, taskID, robID)
                arriveTime = rob.leaveTime + roadDur
                if arriveTime > self.taskLst[taskID].cmpltTime:
                    continue
                rob.roadDur = roadDur
                rob.taskID = taskID
                rob.arriveTime = rob.leaveTime + rob.roadDur
                rob.stateType = RobotState['onRoad']
                break

    def getRobTask(self, robID=0, encodeIndex=0):
            '''
            get the robot next task ID
            '''
            stopBool = False
            while True:
                if encodeIndex == self._taskNum:
                    stopBool = True
                    break
                taskID = self.encode[robID][encodeIndex]
                if taskID < 0:
                    encodeIndex += 1
                    continue
                else:
                    break
            return taskID, encodeIndex, stopBool

    def calRoadDur(self,taskID1,taskID2,robID):
        '''
        calculate the time fragment from the time when robID leaves the taskID1 to
        the time when rob arrives the taskID2
        '''
        dis = self._taskDisMat[taskID1][taskID2]
        rob = self.robotLst[robID]
        roadDur = dis/rob._vel
        return roadDur

    def saveRobotInfo(self,degFile):
        '''
        save robot information into the deg files
        '''
        deg = degFile
        deg.write('\n')
        for i in range(self._robNum):
            lst = []
            lst.append(i)
            lst.append('arriveTime')
            lst.append(self.robotLst[i].arriveTime)
            lst.append('leaveTime')
            lst.append(self.robotLst[i].leaveTime)
            lst.append('state')
            lst.append(self.robotLst[i].stateType)
            lst.append('taskID')
            lst.append(self.robotLst[i].taskID)
            str_lst  = [str(x) for x in lst]
            robInfo = '  '
            robInfo = robInfo.join(str_lst)
            deg.write(robInfo+'\n')
        deg.write('\n')
        deg.flush()

    def arriveCmpltTask(self,actionID,encodeInd):
        # self.encode[actionID][encodeInd]

        self.encode[actionID][encodeInd] = -1
        rob = self.robotLst[actionID]
        while True:
            if len(rob.cmpltTaskLst) == 0:
                if encodeInd == self.taskNum - 1:
                    rob.stopBool = True
                    break
                taskID = self.encode[actionID][encodeInd]
                if taskID < 0:
                    encodeInd  += 1
                    continue
                dis  = self.rob2taskDisMat[actionID][taskID]
                dis_time = dis/rob.vel
                rob.arriveTime = dis_time
                rob.encodeIndex = encodeInd
                break
            else:
                if encodeInd == self.taskNum - 1:
                    rob.stopBool = True
                    break
                taskID = self.encode[actionID][encodeInd]
                if taskID < 0:
                    encodeInd  += 1
                    continue
                preTaskID = rob.cmpltTaskLst[-1]
                roadDur = self.calRoadDur(preTaskID,taskID,actionID)
                rob.arriveTime  = rob.leaveTime + roadDur
                rob.encodeIndex = encodeInd
                break
        rob.taskID = taskID

if __name__ == '__main__':
    print('test_mpdaDecoder')

    print(BaseDir)
    ins = MPDAInstance()
    insFileName = BaseDir +'//benchmark//11_11_RANDOMCLUSTERED_CLUSTERED_MSVFLV_QUADRANT_thre0.1MPDAins.dat'
    ins.loadCfg(fileName =  insFileName)

    decoder = MPDATriDecoder(ins)
    for _ in range(100):
        np.random.seed(2)
        # x = []
        # for _ in range(10):
        #     x.append(RobTaskPair(robID = np.random.randint(0, ins._robNum -1),taskID = np.random.randint(0, ins._taskNum -1)))
        # print(x)
        #
        print(_)
        x = generateRandEncode(robNum= ins._robNum, taskNum= ins._taskNum)
        # x = [RobTaskPair(robID = 1, taskID = 2),RobTaskPair(robID = 3,taskID = 4)]
        # print(x)
        # exit()
        # x = [[6, 1, 0, 7, 5 ,4, 2, 3],
        #     [6, 4, 7 ,0 ,5 ,3 ,2 ,1],
        #     [3, 0 ,4 ,2 ,6 ,7 ,5 ,1],
        #     [3, 5 ,6 ,0 ,2 ,7 ,1 ,4],
        #     [4, 5 ,1 ,0 ,6 ,7 ,3 ,2],
        #     [6, 3 ,4 ,0 ,7 ,5 ,1 ,2],
        #     [1 ,0 ,3 ,6 ,7 ,4 ,5, 2],
        #     [1, 4, 7, 0, 2, 3, 6, 5]]
        validStateBoolean,actSeq = decoder.decode(x)
    actSeqDecoder = MPDADecoderActionSeq(ins)
    actSeqDecoder.decode(actSeq)
    # print(np.array(actSeq.convert2MultiPerm(ins._robNum),dtype = object))
    for perm in actSeq.convert2MultiPerm(ins._robNum):
        print(perm)
    # actSeqDecoder.drawActionSeqGantt()
    # actSeqDecoder.drawTaskScatter()
    # actSeqDecoder.drawTaskDependence()

    print('first decoder is over')
    # np.random.seed(1)

    chrom = [1, 0, 6, 3, 7, 5, 2, 4, 6, 3, 2, 5, 0, 7, 1, 4, 6, 3, 1, 2, 5, 7, 4, 0, 1, 0, 6, 5, 7, 3, 4, 2, 6, 3, 5, 7, 4,
         1, 0, 2, 6, 3, 2, 5, 7, 4, 1, 0, 1, 6, 0, 5, 7, 2, 4, 3, 6, 3, 2, 7, 4, 1, 5, 0]

    encode =  np.zeros((ins._robNum, ins._taskNum), dtype=int)
    i = 0
    for robID in range(ins._robNum):
        for taskID in range(ins._taskNum):
            encode[robID][taskID] = chrom[i]
            i += 1
    # mpda_decode_nb = MPDA_Decode_Discrete_NB()
    print(encode)
    validBoolean,actSeq = decoder.decode(encode)

    # x = generateRandEncode(robNum= ins._robNum, taskNum= ins._taskNum)
    # print(x)
    # validStateBoolean,actSeq = decoder.decode(x)
    # print(actSeq.convert2MultiPerm(ins._robNum))
    for perm in actSeq.convert2MultiPerm(ins._robNum):
        print(perm)
    # print(actSeq)
    actSeqDecoder = MPDADecoderActionSeq(ins)
    actSeqDecoder.decode(actSeq)

    # actSeqDecoder.drawActionSeqGantt()
    # actSeqDecoder.drawTaskScatter()
    # actSeqDecoder.drawTaskDependence()


text:
# coding: utf-8

# # Create tables summarising contents of each dataset

# In[1]:

import os
from decimal import Decimal

import numpy as np
import pandas as pd

pd.set_option('display.max_colwidth', -1)

# ## Paths to directories

# In[2]:

# Network dataset construction directory
network_dir = os.path.join(os.path.curdir, os.path.pardir, '1_network')

# Generator dataset construction directory
generators_dir = os.path.join(os.path.curdir, os.path.pardir, '2_generators')

# Signals dataset construction directory
signals_dir = os.path.join(os.path.curdir, os.path.pardir, '3_load_and_dispatch_signals')

# Output directory
output_dir = os.path.join(os.path.curdir, 'output')

# ## Functions used to parse data

# In[3]:

def get_numerical_range(df, column_name, no_round=False, round_lower=False, round_upper=False, sci_notation_lower=False, sci_notation_upper=False):
    "Round output"

    lower = df[column_name].min()
    upper = df[column_name].max()

    if no_round:
        return '${0}-{1}$'.format(lower, upper)

    else:
        if round_lower:
            lower_out = np.around(lower, decimals=round_lower)

        if round_upper:
            upper_out = np.around(upper, decimals=round_upper)

        if sci_notation_lower:
            lo = df[column_name].min()

            lo_exp = '{:.2e}'.format(lo) # Exponential notation

            # Make latex friendly
            lo_exp = lo_exp.replace('-0','-')
            lower_out = lo_exp.replace('e', ' \times 10^{') + '}'

        if sci_notation_upper:
            up = df[column_name].max()

            up_exp = '{:.2e}'.format(up) # Exponential notation

            # Make latex friendly
            up_exp = up_exp.replace('-0','-')
            upper_out = up_exp.replace('e', ' \times 10^{') + '}'

        return '${0}-{1}$'.format(lower_out, upper_out)

def add_caption(table, caption, label):
    "Add caption to table"
    return table.replace('\\end{tabular}\n', '\\end{tabular}\n\\caption{%s}\n\\label{%s}\n' % (caption, label))

def wrap_in_table(table):
    "Wrap tabular in table environment"
    table = table.replace('\\begin{tabular}', '\\begin{table}[H]\n\\begin{tabular}')
    table = table + '\\end{table}'
    return table

def format_table(df_out, caption, label, filename, add_caption_and_label=False):
    "Format table to add caption and labels"

    # Reset index and rename column
    df_out = df_out.reset_index().rename(columns={'index': 'Col. Name'})

    # Format col names so underscores don't cause errors
    df_out['Col. Name'] = df_out['Col. Name'].map(lambda x: x.replace('_', '\_'))
    df_out.index = range(1, len(df_out.index) + 1)
    df_out.index.name = 'Col.'
    df_out = df_out.reset_index()

    # Raw table
    table = df_out.to_latex(escape=False,  index=False, multicolumn=False)

    # Add caption and labels and wrap in table environment
    if add_caption_and_label:
        table_out = add_caption(table, caption=caption, label=label)
        table_out = wrap_in_table(table_out)
    else:
        table_out = table

    # Save to file
    with open(os.path.join(output_dir, filename), 'w') as f:
        f.write(table_out)

    return table_out

# ## Network
# ### Nodes

# In[4]:

def create_network_nodes_table():
    "Create summary of network node datasets"

    # Input DataFrame
    df = pd.read_csv(os.path.join(network_dir, 'output', 'network_nodes.csv'))

    # Initialise output DataFrame
    df_out = pd.DataFrame(index=df.columns, columns=['Format', 'Units', 'Range', 'Description'])

    df_out.loc['NODE_ID'] = {'Format': 'str', 'Units': '-', 'Range': get_numerical_range(df, 'NODE_ID', no_round=True), 'Description': 'Node ID'}
    df_out.loc['STATE_NAME'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'State in which node is located'}
    df_out.loc['NEM_REGION'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'NEM region in which node is located'}
    df_out.loc['NEM_ZONE'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'NEM zone in which node is located'}
    df_out.loc['VOLTAGE_KV'] = {'Format': 'int', 'Units': 'kV', 'Range': get_numerical_range(df, 'VOLTAGE_KV', no_round=True), 'Description': 'Node voltage'}
    df_out.loc['RRN'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'RRN', no_round=True), 'Description': 'If 1 node is a RRN, if 0 node is not a RNN'}
    df_out.loc['PROP_REG_D'] = {'Format': 'float', 'Units': '-', 'Range': get_numerical_range(df, 'PROP_REG_D', round_lower=3, round_upper=3), 'Description': 'Proportion of NEM regional demand consumed at node'}
    df_out.loc['LATITUDE'] = {'Format': 'float', 'Units': 'N$^{\circ}$', 'Range': get_numerical_range(df, 'LATITUDE', round_lower=1, round_upper=1), 'Description': 'Latitude (GDA94)'}
    df_out.loc['LONGITUDE'] = {'Format': 'float', 'Units': 'E$^{\circ}$', 'Range': get_numerical_range(df, 'LONGITUDE', round_lower=1, round_upper=1), 'Description': 'Longitude (GDA94)'}

    # Output table after formatting
    table_out = format_table(df_out, caption='Network nodes dataset summary', label='tab: nodes', filename='network_nodes.tex', add_caption_and_label=True)

    return table_out

create_network_nodes_table()

# ### AC edges

# In[5]:

def create_network_edges_table():
    "Create table summarising AC network edges dataset"

    # Input DataFrame
    df = pd.read_csv(os.path.join(network_dir, 'output', 'network_edges.csv'))

    # Initialise output DataFrame
    df_out = pd.DataFrame(index=df.columns, columns=['Format', 'Units', 'Range', 'Description'])

    df_out.loc['LINE_ID'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Network edge ID'}
    df_out.loc['NAME'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Name of network edge'}
    df_out.loc['FROM_NODE'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'FROM_NODE', no_round=True), 'Description': 'Node ID for origin node'}
    df_out.loc['TO_NODE'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'TO_NODE', no_round=True), 'Description': 'Node ID for destination node'}
    df_out.loc['R_PU'] = {'Format': 'float', 'Units': 'p.u.', 'Range': get_numerical_range(df, 'R_PU', sci_notation_lower=True, round_upper=3), 'Description': 'Per-unit resistance'}
    df_out.loc['X_PU'] = {'Format': 'float', 'Units': 'p.u.', 'Range': get_numerical_range(df, 'X_PU', sci_notation_lower=True, round_upper=3), 'Description': 'Per-unit reactance'}
    df_out.loc['B_PU'] = {'Format': 'float', 'Units': 'p.u.', 'Range': get_numerical_range(df, 'B_PU', sci_notation_lower=True, round_upper=3), 'Description': 'Per-unit susceptance'}
    df_out.loc['NUM_LINES'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'NUM_LINES', no_round=True), 'Description': 'Number of parallel lines'}
    df_out.loc['LENGTH_KM'] = {'Format': 'float', 'Units': 'km', 'Range': get_numerical_range(df, 'LENGTH_KM', round_lower=2, round_upper=1), 'Description': 'Line length'}
    df_out.loc['VOLTAGE_KV'] = {'Format': 'float', 'Units': 'kV', 'Range': get_numerical_range(df, 'VOLTAGE_KV', no_round=True), 'Description': 'Line voltage'}

     # Output table after formatting
    table_out = format_table(df_out, caption='Network edges dataset summary', label='tab: edges', filename='network_edges.tex', add_caption_and_label=True)

    return table_out
create_network_edges_table()

# ### HVDC links

# In[6]:

def create_hvdc_links_table():
    "Create summary of HVDC links dataset"

    df = pd.read_csv(os.path.join(network_dir, 'output', 'network_hvdc_links.csv'))

    # Initialise output DataFrame
    df_out = pd.DataFrame(index=df.columns, columns=['Format', 'Units', 'Range', 'Description'])

    df_out.loc['HVDC_LINK_ID'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'HVDC link ID'}
    df_out.loc['FROM_NODE'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'FROM_NODE', no_round=True), 'Description': 'Node ID of origin node'}
    df_out.loc['TO_NODE'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'TO_NODE', no_round=True), 'Description': 'Node ID of destination node'}
    df_out.loc['FORWARD_LIMIT_MW'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'FORWARD_LIMIT_MW', no_round=True), 'Description': "`From' node to `To' node power-flow limit"}
    df_out.loc['REVERSE_LIMIT_MW'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'REVERSE_LIMIT_MW', no_round=True), 'Description': "`To' node to `From' node power-flow limit"}
    df_out.loc['VOLTAGE_KV'] = {'Format': 'float', 'Units': 'kV', 'Range': get_numerical_range(df, 'VOLTAGE_KV', no_round=True), 'Description': 'HVDC link voltage'}

    # Output table after formatting
    table_out = format_table(df_out, caption='Network HVDC links dataset summary', label='tab: hvdc links', filename='network_hvdc_links.tex', add_caption_and_label=True)

    return table_out
create_hvdc_links_table()

# ### AC interconnector links

# In[7]:

def create_ac_interconnector_links_table():
    "Create table summarising AC interconnector connection point locations"

    # Input DataFrame
    df = pd.read_csv(os.path.join(network_dir, 'output', 'network_ac_interconnector_links.csv'))

    # Initialise output DataFrame
    df_out = pd.DataFrame(index=df.columns, columns=['Format', 'Units', 'Range', 'Description'])

    df_out.loc['INTERCONNECTOR_ID'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'AC interconnector ID'}
    df_out.loc['FROM_NODE'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'FROM_NODE', no_round=True), 'Description': 'Node ID of origin node'}
    df_out.loc['FROM_REGION'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': "Region in which `From' node is located"}
    df_out.loc['TO_NODE'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'TO_NODE', no_round=True), 'Description': 'Node ID for destination node'}
    df_out.loc['TO_REGION'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': "Region in which `To' node is located"}
    df_out.loc['VOLTAGE_KV'] = {'Format': 'float', 'Units': 'kV', 'Range': get_numerical_range(df, 'VOLTAGE_KV', no_round=True), 'Description': 'Line voltage'}

     # Output table after formatting
    table_out = format_table(df_out, caption='AC interconnector locations dataset summary', label='tab: interconnectors - links', filename='network_ac_interconnector_links.tex', add_caption_and_label=True)

    return table_out
create_ac_interconnector_links_table()

# ### Interconnector flow limits

# In[8]:

def create_ac_interconnector_flow_limits_table():
    "Create table summarising interconnector flow limits"

    # Input DataFrame
    df = pd.read_csv(os.path.join(network_dir, 'output', 'network_ac_interconnector_flow_limits.csv'))

    # Initialise output DataFrame
    df_out = pd.DataFrame(index=df.columns, columns=['Format', 'Units', 'Range', 'Description'])

    df_out.loc['INTERCONNECTOR_ID'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'AC interconnector ID'}
    df_out.loc['FROM_REGION'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': "Region in which `From' node is located"}
    df_out.loc['TO_REGION'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': "Region in which `To' node is located"}
    df_out.loc['FORWARD_LIMIT_MW'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'FORWARD_LIMIT_MW', no_round=True), 'Description': "`From' node to `To' node power-flow limit"}
    df_out.loc['REVERSE_LIMIT_MW'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'REVERSE_LIMIT_MW', no_round=True), 'Description': "`To' node to `From' node power-flow limit"}

    # Output table after formatting
    table_out = format_table(df_out, caption='AC interconnector flow limits summary', label='tab: interconnectors - flow limits', filename='network_ac_interconnector_flow_limits.tex', add_caption_and_label=True)

    return table_out
create_ac_interconnector_flow_limits_table()

# ## Generators

# In[9]:

def create_generators_tables():
    "Create table summarising fields in generator datasets"

    # Input DataFrame
    column_dtypes = {'NODE': int, 'REG_CAP': int, 'RR_STARTUP': int, 'RR_SHUTDOWN': int, 'RR_UP': int,
                     'RR_DOWN': int, 'MIN_ON_TIME': int, 'MIN_OFF_TIME': int, 'SU_COST_COLD': int, 'SU_COST_WARM': int,
                     'SU_COST_HOT': int}

    df = pd.read_csv(os.path.join(generators_dir, 'output', 'generators.csv'), dtype=column_dtypes)

    # Initialise output DataFrame
    df_out = pd.DataFrame(index=df.columns, columns=['Format', 'Units', 'Range', 'Description'])

    df_out.loc['DUID'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Unique ID for each unit'}
    df_out.loc['STATIONID'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'ID of station to which DUID belongs'}
    df_out.loc['STATIONNAME'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Name of station to which DUID belongs'}
    df_out.loc['NEM_REGION'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Region in which DUID is located'}
    df_out.loc['NEM_ZONE'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Zone in which DUID is located'}
    df_out.loc['NODE'] = {'Format': 'int', 'Units': '-', 'Range': get_numerical_range(df, 'NODE', no_round=True), 'Description': 'Node to which DUID is assigned'}
    df_out.loc['FUEL_TYPE'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Primary fuel type'}
    df_out.loc['FUEL_CAT'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Primary fuel category'}
    df_out.loc['EMISSIONS'] = {'Format': 'float', 'Units': 'tCO$_{2}$/MWh', 'Range': get_numerical_range(df, 'EMISSIONS', round_lower=2, round_upper=2), 'Description': 'Equivalent CO$_{2}$ emissions intensity'}
    df_out.loc['SCHEDULE_TYPE'] = {'Format': 'str', 'Units': '-', 'Range': '-', 'Description': 'Schedule type for unit'}
    df_out.loc['REG_CAP'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'REG_CAP', no_round=True), 'Description': 'Registered capacity'}
    df_out.loc['MIN_GEN'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'MIN_GEN', no_round=True), 'Description': 'Minimum dispatchable output'}
    df_out.loc['RR_STARTUP'] = {'Format': 'float', 'Units': 'MW/h', 'Range': get_numerical_range(df, 'RR_STARTUP', no_round=True), 'Description': 'Ramp-rate for start-up'}
    df_out.loc['RR_SHUTDOWN'] = {'Format': 'float', 'Units': 'MW/h', 'Range': get_numerical_range(df, 'RR_SHUTDOWN', no_round=True), 'Description': 'Ramp-rate for shut-down'}
    df_out.loc['RR_UP'] = {'Format': 'float', 'Units': 'MW/h', 'Range': get_numerical_range(df, 'RR_UP', no_round=True), 'Description': 'Ramp-rate up when running'}
    df_out.loc['RR_DOWN'] = {'Format': 'float', 'Units': 'MW/h', 'Range': get_numerical_range(df, 'RR_DOWN', no_round=True), 'Description': 'Ramp-rate down when running'}
    df_out.loc['MIN_ON_TIME'] = {'Format': 'int', 'Units': 'h', 'Range': get_numerical_range(df, 'MIN_ON_TIME', no_round=True), 'Description': 'Minimum on time'}
    df_out.loc['MIN_OFF_TIME'] = {'Format': 'int', 'Units': 'h', 'Range': get_numerical_range(df, 'MIN_OFF_TIME', no_round=True), 'Description': 'Minimum off time'}
    df_out.loc['SU_COST_COLD'] = {'Format': 'int', 'Units': '\$', 'Range': get_numerical_range(df, 'SU_COST_COLD', no_round=True), 'Description': 'Cold start start-up cost'}
    df_out.loc['SU_COST_WARM'] = {'Format': 'int', 'Units': '\$', 'Range': get_numerical_range(df, 'SU_COST_WARM', no_round=True), 'Description': 'Warm start start-up cost'}
    df_out.loc['SU_COST_HOT'] = {'Format': 'int', 'Units': '\$', 'Range': get_numerical_range(df, 'SU_COST_HOT', no_round=True), 'Description': 'Hot start start-up cost'}
    df_out.loc['VOM'] = {'Format': 'float', 'Units': '\$/MWh', 'Range': get_numerical_range(df, 'VOM', no_round=True), 'Description': 'Variable operations and maintenance costs'}
    df_out.loc['HEAT_RATE'] = {'Format': 'float', 'Units': 'GJ/MWh', 'Range': get_numerical_range(df, 'HEAT_RATE', round_lower=1, round_upper=1), 'Description': 'Heat rate'}
    df_out.loc['NL_FUEL_CONS'] = {'Format': 'float', 'Units': '-', 'Range': get_numerical_range(df, 'NL_FUEL_CONS', no_round=True), 'Description': 'No-load fuel consumption as a proportion of full load consumption'}
    df_out.loc['FC_2016-17'] = {'Format': 'float', 'Units': '\$/GJ', 'Range': get_numerical_range(df, 'FC_2016-17', round_lower=1, round_upper=1), 'Description': 'Fuel cost for the year 2016-17'}
    df_out.loc['SRMC_2016-17'] = {'Format': 'float', 'Units': '\$/MWh', 'Range': get_numerical_range(df, 'SRMC_2016-17', round_lower=1, round_upper=1), 'Description': 'Short-run marginal cost for the year 2016-17'}

    # Sources for generator dataset records
    source = dict()
    source['DUID'] = '\cite{aemo_data_2018}'
    source['STATIONID'] = '\cite{aemo_data_2018}'
    source['STATIONNAME'] = '\cite{aemo_data_2018}'
    source['FUEL_TYPE'] = '\cite{aemo_data_2018}'
    source['EMISSIONS'] = '\cite{aemo_current_2018}'
    source['SCHEDULE_TYPE'] = '\cite{aemo_data_2018}'
    source['REG_CAP'] = '\cite{aemo_data_2018}'
    source['MIN_GEN'] = '\cite{aemo_data_2018, aemo_ntndp_2018}'
    source['RR_STARTUP'] = '\cite{aemo_ntndp_2018}'
    source['RR_SHUTDOWN'] = '\cite{aemo_ntndp_2018}'
    source['RR_UP'] = '\cite{aemo_ntndp_2018}'
    source['RR_DOWN'] = '\cite{aemo_ntndp_2018}'
    source['MIN_ON_TIME'] = '\cite{aemo_ntndp_2018}'
    source['MIN_OFF_TIME'] = '\cite{aemo_ntndp_2018}'
    source['SU_COST_COLD'] = '\cite{aemo_ntndp_2018}'
    source['SU_COST_WARM'] = '\cite{aemo_ntndp_2018}'
    source['SU_COST_HOT'] = '\cite{aemo_ntndp_2018}'
    source['VOM'] = '\cite{aemo_ntndp_2018}'
    source['HEAT_RATE'] = '\cite{aemo_ntndp_2018}'
    source['NL_FUEL_CONS'] = '\cite{aemo_ntndp_2018}'
    source['FC_2016-17'] = '\cite{aemo_ntndp_2018}'

    df_out['Source\tnote{$\dagger$}'] = df_out.apply(lambda x: '' if x.name not in source.keys() else source[x.name], axis=1)

    # Include double dagger symbol for heatrate record
    df_out = df_out.rename(index={'HEAT_RATE': 'HEAT_RATE\tnote{$\ddagger$}'})

    table_out = format_table(df_out, caption='Generator dataset summary', label='tab: generator dataset', filename='generators.tex')

    # Wrap in three part table. Append environments to beginning of tabular
    table_out = '\\begin{table}[H]\n\\begin{threeparttable}\n\\centering\n\\small' + table_out

    # Append table notes and environment to end of table
    append_to_end = """\\begin{tablenotes}
    \\item[$\\dagger$] Where no source is given, the value has been derived as part of the dataset construction procedure. NEM\\_REGION and NEM\\_ZONE were found by determining the region and zone of each generator's assigned node. FUEL\\_CAT assigns a generic category to FUEL\\_TYPE. MIN\\_GEN was computed by combining minimum output as a proportion of nameplate capacity from~\cite{aemo_ntndp_2018} with registered capacities from~\\cite{aemo_data_2018}. SRMC\\_2016-17 is calculated from VOM, HEAT\\_RATE, and FC\\_2016-17 fields, using equation~(\\ref{eqn: SRMC calculation}).
    \\item[$\\ddagger$] While not explicitly stated, it is assumed that a lower heating value is referred to. This is consistent with another field in~\\cite{aemo_ntndp_2018} that gives DUID thermal efficiency in terms of lower heating values.
    \\end{tablenotes}
    \\end{threeparttable}
    \\caption{Generator dataset summary}
    \\label{tab: generator dataset}
    \\end{table}"""

    table_out = table_out + append_to_end

    # Save to file
    with open(os.path.join(output_dir, 'generators.tex'), 'w') as f:
        f.write(table_out)

    return table_out
create_generators_tables()

# ## Load and dispatch signals
# ### Load signals

# In[10]:

def create_load_signals_table():
    df = pd.read_csv(os.path.join(signals_dir, 'output', 'signals_regional_load.csv'))

    # Initialise output DataFrame
    df_out = pd.DataFrame(index=df.columns, columns=['Format', 'Units', 'Range', 'Description'])

    df_out.loc['SETTLEMENTDATE'] = {'Format': 'timestamp', 'Units': '-', 'Range': '1/6/2017  12:30:00 AM - 1/7/2017  12:00:00 AM', 'Description': 'Trading interval'}
    df_out.loc['NSW1'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'NSW1', round_lower=1, round_upper=1), 'Description': 'New South Wales demand signal'}
    df_out.loc['QLD1'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'QLD1', round_lower=1, round_upper=1), 'Description': 'Queensland demand signal'}
    df_out.loc['SA1'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'SA1', round_lower=1, round_upper=1), 'Description': 'South Australia demand signal'}
    df_out.loc['TAS1'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'TAS1', round_lower=1, round_upper=1), 'Description': 'Tasmania demand signal'}
    df_out.loc['VIC1'] = {'Format': 'float', 'Units': 'MW', 'Range': get_numerical_range(df, 'VIC1', round_lower=1, round_upper=1), 'Description': 'Victoria demand signal'}

    table_out = format_table(df_out, caption='Regional demand signals dataset summary', label='tab: regional demand signals', filename='signals_regional_demand.tex', add_caption_and_label=True)

    return table_out
create_load_signals_table()

# ### Dispatch signals

# In[11]:

df = pd.read_csv(os.path.join(signals_dir, 'output', 'signals_dispatch.csv'))

df_out = pd.DataFrame(columns=['Format', 'Units', 'Range', 'Description'])
df_out.loc['SETTLEMENTDATE'] = {'Format': 'timestamp', 'Units': '-', 'Range': '1/6/2017  12:30:00 AM - 1/7/2017  12:00:00 AM', 'Description': 'Trading interval'}
df_out.loc['(DUID)'] = {'Format': 'float', 'Units': 'MW', 'Range': '-', 'Description': 'DUID dispatch profile'}

# Rename columns
df_out = df_out.reset_index().rename(columns={'index': 'Col. Name'})
df_out.index.name = 'Col.'
df_out = df_out.rename(index={0: '1', 1: '2-{0}'.format(len(df.columns))})
df_out = df_out.reset_index()

# Raw table
table = df_out.to_latex(escape=False, index=False, multicolumn=False)

table_out = add_caption(table, caption='DUID dispatch profiles. Columns correspond to DUIDs.', label='tab: duid dispatch profiles')
table_out = wrap_in_table(table_out)

# Save to file
with open(os.path.join(output_dir, 'signals_dispatch.tex'), 'w') as f:
    f.write(table_out)

ext: py

max_stars_repo_name: akxen/egrimod-nem

max_issues_repo_name: akxen/egrimod-nem

max_forks_repo_name: akxen/egrimod-nem

reason: import numpy


text:
from collections import defaultdict
from time import time

import numpy as np
import torch
from torch.utils.data.dataset import IterableDataset

class ReplayBufferIterableDatasetDisk(IterableDataset):
    '''
    This implementation fixes the memory leak issue with ReplayBufferIterableDataset;
    however, the processing speed has reduced significantly from 10it/s to 5.3it/s.
    Direct image loading approach is around 5.3it/s.
    '''
    def __init__(self, keys, buffer_size, batch_size):
        self._size = buffer_size
        self._batch_size = batch_size

        # memory management
        self._idx = 0
        self._current_size = 0

        # create the buffer to store info
        self._keys = keys
        self._buffers = defaultdict(list)

    # store the episode
    def store_episode(self, rollout):
        idx = self._idx = (self._idx + 1) % self._size
        self._current_size += 1

        if self._current_size > self._size:
            for k in self._keys:
                self._buffers[k][idx] = rollout[k]
        else:
            for k in self._keys:
                self._buffers[k].append(rollout[k])

    def state_dict(self):
        return self._buffers

    def load_state_dict(self, state_dict):
        self._buffers = state_dict
        self._current_size = len(self._buffers["ac"])

    def _sample_func(self, start_index, end_index):
        episode_batch = self._buffers
        episode_idxs = torch.randint(start_index, end_index, (self._batch_size,))
        t_samples = [
            torch.randint(0, len(episode_batch["ac"][episode_idx]), (1,)).numpy()[0]
            for episode_idx in episode_idxs
        ]

        transitions = {}
        for key in episode_batch.keys():
            transitions[key] = [
                episode_batch[key][episode_idx][t]
                for episode_idx, t in zip(episode_idxs, t_samples)
            ]

        if "ob_next" not in episode_batch.keys():
            transitions["ob_next"] = [
                episode_batch["ob"][episode_idx][t + 1]
                for episode_idx, t in zip(episode_idxs, t_samples)
            ]

        new_transitions = {}
        for k, v in transitions.items():
            if isinstance(v[0], dict):
                sub_keys = v[0].keys()
                new_transitions[k] = {
                    sub_key: np.stack([v_[sub_key] for v_ in v]) for sub_key in sub_keys
                }
            else:
                new_transitions[k] = np.stack(v)

        ob_images = []
        ob_next_images = []
        for i in range(self._batch_size):
            ob_images.append(np.load(transitions['ob'][i]['image'])[0])
            ob_next_images.append(np.load(transitions['ob_next'][i]['image'])[0])
        ob_images = np.array(ob_images)
        ob_next_images = np.array(ob_next_images)
        transitions.clear()
        return new_transitions, ob_images, ob_next_images

    def __iter__(self):
        worker_info = torch.utils.data.get_worker_info()
        if worker_info is None:
            iter_start = 0
            iter_end = len(self._buffers["ac"])
        else:
            per_worker = int(len(self._buffers["ac"]) / float(worker_info.num_workers))
            worker_id = worker_info.id
            iter_start = worker_id * per_worker
            iter_end = min(iter_start + per_worker, len(self._buffers["ac"]))
        yield self._sample_func(iter_start, iter_end)

class ReplayBufferIterableDataset(IterableDataset):
    '''
    This implementation uses IterableDataset. Although this implementation was designed to
    save memory by only loading a batch of images in RAM, the following code has memory leak
    issue (loaded images in RAM are not freed):

    'transitions[key][i]['image'] = np.load(transitions[key][i]['image'])[0]'

    This problem is actually not well documented on the Internet, but there is a person who
    has the same issue when he assigns np.array to a dictionary in the Dataloader:
    https://discuss.pytorch.org/t/memory-leak-in-dataloader/66617.

    del, gc.collect, .clear() have all been tried but to no effect.

    Under the same environment and machine, this implementation can process around 10it/s.
    '''
    def __init__(self, keys, buffer_size, batch_size):
        self._size = buffer_size
        self._batch_size = batch_size

        # memory management
        self._idx = 0
        self._current_size = 0

        # create the buffer to store info
        self._keys = keys
        self._buffers = defaultdict(list)

    # store the episode
    def store_episode(self, rollout):
        idx = self._idx = (self._idx + 1) % self._size
        self._current_size += 1

        if self._current_size > self._size:
            for k in self._keys:
                self._buffers[k][idx] = rollout[k]
        else:
            for k in self._keys:
                self._buffers[k].append(rollout[k])

    def state_dict(self):
        return self._buffers

    def load_state_dict(self, state_dict):
        self._buffers = state_dict
        self._current_size = len(self._buffers["ac"])

    def _sample_func(self, start_index, end_index):
        episode_batch = self._buffers
        episode_idxs = torch.randint(start_index, end_index, (self._batch_size,))
        t_samples = [
            torch.randint(0, len(episode_batch["ac"][episode_idx]), (1,)).numpy()[0]
            for episode_idx in episode_idxs
        ]

        transitions = {}
        for key in episode_batch.keys():
            transitions[key] = [
                episode_batch[key][episode_idx][t]
                for episode_idx, t in zip(episode_idxs, t_samples)
            ]
            if key == 'ob':
                for i in range(len(transitions[key])):
                    if isinstance(transitions[key][i]['image'], str):
                        transitions[key][i]['image'] = np.load(transitions[key][i]['image'])[0]

        if "ob_next" not in episode_batch.keys():
            transitions["ob_next"] = [
                episode_batch["ob"][episode_idx][t + 1]
                for episode_idx, t in zip(episode_idxs, t_samples)
            ]
            for i in range(len(transitions["ob_next"])):
                if isinstance(transitions["ob_next"][i]['image'], str):
                    transitions["ob_next"][i]['image'] = np.load(transitions["ob_next"][i]['image'])[0]

        new_transitions = {}
        for k, v in transitions.items():
            if isinstance(v[0], dict):
                sub_keys = v[0].keys()
                new_transitions[k] = {
                    sub_key: np.stack([v_[sub_key] for v_ in v]) for sub_key in sub_keys
                }
            else:
                new_transitions[k] = np.stack(v)

        transitions.clear()
        return new_transitions

    def __iter__(self):
        worker_info = torch.utils.data.get_worker_info()
        if worker_info is None:
            iter_start = 0
            iter_end = len(self._buffers["ac"])
        else:
            per_worker = int(len(self._buffers["ac"]) / float(worker_info.num_workers))
            worker_id = worker_info.id
            iter_start = worker_id * per_worker
            iter_end = min(iter_start + per_worker, len(self._buffers["ac"]))
        yield self._sample_func(iter_start, iter_end)

class ReplayBuffer:
    def __init__(self, keys, buffer_size, sample_func):
        self._size = buffer_size

        # memory management
        self._idx = 0
        self._current_size = 0
        self._sample_func = sample_func

        # create the buffer to store info
        self._keys = keys
        self._buffers = defaultdict(list)

    def clear(self):
        self._idx = 0
        self._current_size = 0
        self._buffers = defaultdict(list)

    # store the episode
    def store_episode(self, rollout):
        idx = self._idx = (self._idx + 1) % self._size
        self._current_size += 1

        if self._current_size > self._size:
            for k in self._keys:
                self._buffers[k][idx] = rollout[k]
        else:
            for k in self._keys:
                self._buffers[k].append(rollout[k])

    # sample the data from the replay buffer
    def sample(self, batch_size):
        # sample transitions
        transitions = self._sample_func(self._buffers, batch_size)
        return transitions

    def state_dict(self):
        return self._buffers

    def load_state_dict(self, state_dict):
        self._buffers = state_dict
        self._current_size = len(self._buffers["ac"])

class RandomSampler:
    def sample_func(self, episode_batch, batch_size_in_transitions):
        rollout_batch_size = len(episode_batch["ac"])
        batch_size = batch_size_in_transitions
        episode_idxs = np.random.randint(0, rollout_batch_size, batch_size)
        t_samples = [
            np.random.randint(len(episode_batch["ac"][episode_idx]))
            for episode_idx in episode_idxs
        ]

        transitions = {}
        for key in episode_batch.keys():
            transitions[key] = [
                episode_batch[key][episode_idx][t]
                for episode_idx, t in zip(episode_idxs, t_samples)
            ]

        if "ob_next" not in episode_batch.keys():
            transitions["ob_next"] = [
                episode_batch["ob"][episode_idx][t + 1]
                for episode_idx, t in zip(episode_idxs, t_samples)
            ]

        new_transitions = {}
        for k, v in transitions.items():
            if isinstance(v[0], dict):
                sub_keys = v[0].keys()
                new_transitions[k] = {
                    sub_key: np.stack([v_[sub_key] for v_ in v]) for sub_key in sub_keys
                }
            else:
                new_transitions[k] = np.stack(v)

        return new_transitions

class HERSampler:
    def __init__(self, replay_strategy, replay_k, reward_func=None):
        self.replay_strategy = replay_strategy
        if self.replay_strategy == "future":
            self.future_p = 1 - (1.0 / 1 + replay_k)
        else:
            self.future_p = 0
        self.reward_func = reward_func

    def sample_func(self, episode_batch, batch_size_in_transitions):
        rollout_batch_size = len(episode_batch["ac"])
        batch_size = batch_size_in_transitions

        # select which rollouts and which timesteps to be used
        episode_idxs = np.random.randint(0, rollout_batch_size, batch_size)
        t_samples = [
            np.random.randint(len(episode_batch["ac"][episode_idx]))
            for episode_idx in episode_idxs
        ]

        transitions = {}
        for key in episode_batch.keys():
            transitions[key] = [
                episode_batch[key][episode_idx][t]
                for episode_idx, t in zip(episode_idxs, t_samples)
            ]

        transitions["ob_next"] = [
            episode_batch["ob"][episode_idx][t + 1]
            for episode_idx, t in zip(episode_idxs, t_samples)
        ]
        transitions["r"] = np.zeros((batch_size,))

        # hindsight experience replay
        for i, (episode_idx, t) in enumerate(zip(episode_idxs, t_samples)):
            replace_goal = np.random.uniform() < self.future_p
            if replace_goal:
                future_t = np.random.randint(
                    t + 1, len(episode_batch["ac"][episode_idx]) + 1
                )
                future_ag = episode_batch["ag"][episode_idx][future_t]
                if (
                    self.reward_func(
                        episode_batch["ag"][episode_idx][t], future_ag, None
                    )
                    < 0
                ):
                    transitions["g"][i] = future_ag

            transitions["r"][i] = self.reward_func(
                episode_batch["ag"][episode_idx][t + 1], transitions["g"][i], None
            )

        new_transitions = {}
        for k, v in transitions.items():
            if isinstance(v[0], dict):
                sub_keys = v[0].keys()
                new_transitions[k] = {
                    sub_key: np.stack([v_[sub_key] for v_ in v]) for sub_key in sub_keys
                }
            else:
                new_transitions[k] = np.stack(v)

        return new_transitions

max_stars_repo_name: clvrai/mopa-pd

max_issues_repo_name: clvrai/mopa-pd

max_forks_repo_name: clvrai/mopa-pd


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2018/09/12 09:01
# @Author   : Iydon
# @File     : 2.2.py
# @Abstract : Homework auxiliary code.
import numpy as np

def error(p:float, p_star:float) -> tuple:
    """
    From 1.2.py.
    """
    absolute_error = abs(p - p_star)
    relative_error = absolute_error / p
    return (absolute_error, relative_error)

def fixed_point(fun, start:float=0.0, max_step:int=128, sign_dig:int=6)->float:
    """
    Fixed-point iteration to find the zero.
    ----------------------------
    Args:
        fun: Function.
        start: Float, the first iteration point.
        max_step: Integer, max number of iteration.
        sign_dig: Integer, significant digits.

    Returns:
        Float zero.
        zero | fun(zero)\\sim 0.

    Raises:
        None.
    """
    fl = lambda x: round(x, 100)
    eps = 10**(-sign_dig)
    new_val = fun(start)
    for i in range(0, max_step):
        old_val = fl(new_val)
        new_val = fl(fun(old_val))
        print(new_val)
        if abs(old_val-new_val)<=2*eps:
            return (i,new_val)
    return "Max_step..."

title = """
11. For each of the following equations, determine an interval [a,b] on which fixed-point iteration will
    converge. Estimate the number of iterations necessary to obtain approximations accurate to within
    1e-5, and perform the calculations.
"""
print(title)
f = lambda x: (2-np.exp(x)+x**2)/3
df = lambda x: np.exp(x)/3+2*x/3

f = lambda x: 5/x**2+2
df = lambda x: -10*x**(-3)

f = lambda x: 5**(-x)
df = lambda x: -5**(-x)*np.log(5)
print(fixed_point(f, 0, 100,sign_dig=5))

title = """
19. Use Theorem 2.4 to show that the sequence defined by
    a) x_n = \\frac{1}{2}x_{n-1}+\\frac{1}{x_{n-1}},\\quad for\\, n\\geq 1,
       converges to \\sqrt(x) whenever x_0>\\sqrt(2).
    b) Use the fact that 0<(x_0-sqrt(2))^2 whenever x_0\\neq\\sqrt(2) to show
       that if 0<x_0<\\sqrt(2)
"""
print(title)

title = """
24. Let g\\in C^1[a,b] and p be in (a,b) with g(p)=p and |g'(p)|>1. Show that there exists a \\delta >0 such
    that if 0<|p_0-p|<\\delta, then |p_0-p|<|p_1-p|. Thus, no matter how close the initial approximation
    p_0 is to p, the next iterate p_1 is farther away, so the fixed-point iteration does not converge if p_0\\neq p.
"""
print(title)

max_stars_repo_name: Iydon/NumericalAnalysisNotes

max_issues_repo_name: AllenYZB/homework

max_forks_repo_name: AllenYZB/homework


text:
import unittest

#from dolo.symbolic.symbolic import Variable,Parameter,TSymbol
#import pickle

class  TriangularSolveCase(unittest.TestCase):

    def test_solve_simple_system(self):

        from dolo.misc.triangular_solver import triangular_solver

        system = [
            [1,2,3],
            [2,3],
            [3],
            [],
            []
        ]

        solution = triangular_solver(system)

        assert( tuple( solution ) == (3,4,2,1,0) ) # it should have been left unmodified

    def test_solve_symbolic_system(self):
        from dolo.misc.triangular_solver import solve_triangular_system

        import sympy
        [w,x,y,z,t] = vars = sympy.symbols('w, x, y, z, t')

        eqs = [
            x + y + z + t,
            y + z,
            z,
            1,
            1
        ]

        sdict = {s:eqs[i] for i,s in enumerate(vars) }

        solution = solve_triangular_system(sdict)

        assert(solution[z]==1)
        assert(solution[t]==1)
        assert(solution[y]==1)
        assert(solution[x]==2)
        assert(solution[w]==5)

if __name__ == '__main__':

    unittest.main()

max_stars_repo_name: christophe-gouel/dolo

max_issues_repo_name: christophe-gouel/dolo

max_forks_repo_name: christophe-gouel/dolo

reason: import sympy


text:
import os
import cv2
import paddle
import argparse
import numpy as np

from models.rexnetv1 import ReXNetV1
from hand_data_iter.datasets import draw_bd_handpose

default_path = './weights/ReXNetV1-size-256-wingloss102-0.122.pdparams'

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description=' camera ')
    parser.add_argument('--model_path' , type=str  , default = default_path, help = 'model_path' )
    parser.add_argument('--model'      , type=str  , default = 'ReXNetV1'  , help = 'model'      )
    parser.add_argument('--num_classes', type=int  , default = 42          , help = 'num_classes')
    parser.add_argument('--GPUS'       , type=str  , default = '0'         , help = 'GPUS'       )
    parser.add_argument('--img_size'   , type=tuple, default = (256,256)   , help = 'img_size'   )
    parser.add_argument('--camera_id'  , type=int  , default = 0           , help = 'camera id'  )
    parser.add_argument('--fps'        , type=int  , default = 30           , help = 'fps'       )
    ops = parser.parse_args()

    os.environ['CUDA_VISIBLE_DEVICES'] = ops.GPUS

    if ops.model == "ReXNetV1":
        model_ = ReXNetV1(num_classes=ops.num_classes)

    if os.access(ops.model_path,os.F_OK):
        chkpt = paddle.load(ops.model_path)
        model_.set_state_dict(chkpt)
        print('load test model : {}'.format(ops.model_path))

    model_.eval()

    capture = cv2.VideoCapture(ops.camera_id)

    with paddle.no_grad():

        while 1:
            ret, img = capture.read()

            img_width = img.shape[1]
            img_height = img.shape[0]

            img_ = cv2.resize(img, (ops.img_size[1],ops.img_size[0]), interpolation = cv2.INTER_CUBIC)
            img_ = img_.astype(np.float32)
            img_ = (img_-128.)/256.
            img_ = img_.transpose(2, 0, 1)
            img_ = paddle.to_tensor(img_)
            img_ = img_.unsqueeze_(0)

            pre_ = model_(img_)
            output = pre_.numpy()
            output = np.squeeze(output)

            pts_hand = {}
            for i in range(int(output.shape[0]/2)):
                x = (output[i*2+0]*float(img_width))
                y = (output[i*2+1]*float(img_height))

                pts_hand[str(i)] = {}
                pts_hand[str(i)] = {
                    "x":x,
                    "y":y,
                    }

            draw_bd_handpose(img, pts_hand, 0, 0)

            for i in range(int(output.shape[0]/2)):
                x = (output[i*2+0]*float(img_width))
                y = (output[i*2+1]*float(img_height))

                cv2.circle(img, (int(x),int(y)), 3, (255,50,60),-1)
                cv2.circle(img, (int(x),int(y)), 1, (255,150,180),-1)

            cv2.imshow("capture", img)

            if cv2.waitKey(1000//ops.fps) == ord('q'):
                break

    capture.release()
    cv2.destroyAllWindows()


text:
"""
Use this python file as for interactive jupyter notebook data analysis.
This isn't an .ipynb file format, but you can export output cells into a
notebook after analysis.
Using a regular python file as my workflow, I have a clearler view
of the defined variables.
Also, you can better commit code and have full vscode tools at your disposal.
"""

#%%
# import all the modules needed for data analysis
import sys
import logging

import numpy as np

# import scipy as sp
# import sklearn
# import statsmodels.api as sm
# from statsmodels.formula.api import ols

# import pandas as pd
import pandas as pd

pd.set_option("display.max_rows", 120)
pd.set_option("display.max_columns", 120)

# %load_ext autoreload
# %autoreload 2

# import matplotlib as mpl
# import matplotlib.pyplot as plt
# %matplotlib inline
# %config InlineBackend.figure_format = 'retina'

# import seaborn as sns
# sns.set_context("poster")
# sns.set(rc={'figure.figsize': (16, 9.)})
# sns.set_style("whitegrid")

#%%
frame_data = np.random.randint(0, 100, size=(2 ** 10, 2 ** 8))
df = pd.DataFrame(frame_data)
df.head()

#%%
frame_data

#%%

reason: import numpy,import scipy,import statsmodels,from statsmodels


text:
import matplotlib.pyplot as plt
import numpy as np
import wave
import sys

spf = wave.open('0.wav','r')

#Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int16')
fs = spf.getframerate()

#If Stereo
if spf.getnchannels() == 2:
    print('Just mono files')
    sys.exit(0)
Time=np.linspace(0, len(signal)/fs, num=len(signal))
plt.figure(1)
plt.plot(Time,signal)
plt.grid()
plt.savefig("wave")


text:
# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Some parts are taken from https://github.com/Liusifei/UVC
"""
import os
import sys
import copy
import glob
import queue
from urllib.request import urlopen
import argparse
import numpy as np
from tqdm import tqdm
from pdb import set_trace
import multiprocessing as mp

import cv2
import torch
import torch.nn as nn
from torch.nn import functional as F
from PIL import Image
from torchvision import transforms

import utils
import vision_transformer as vits
import helper

@torch.no_grad()
def eval_img_seg_sanity_davis(args, model, frame_list, video_dir, seg_list, color_palette, noise, diffuse):
	"""
	Evaluate tracking on a video given first frame & segmentation
	"""
	# video_folder = os.path.join(args.output_dir, video_dir.split('/')[-1])
	video_folder = os.path.join(args.output_dir+'_'+str(noise)+'_'+str(diffuse), video_dir.split('/')[-1])
	os.makedirs(video_folder, exist_ok=True)

	# The queue stores the n preceeding frames
	# que = queue.Queue(args.n_last_frames)

	# first frame
	_, ori_h, ori_w = helper.read_frame(frame_list[0])
	# extract first frame feature
	# frame1_feat = extract_feature(model, frame1).T #  dim x h*w

	# saving first segmentation
	# out_path = os.path.join(video_folder, "00000.png")
	# imwrite_indexed(out_path, seg_ori, color_palette)
	mask_neighborhood = None
	for cnt in tqdm(range(0, len(frame_list))):
		frame_tar = helper.read_frame(frame_list[cnt])[0]
		seg, seg_ori = helper.read_seg(seg_list[cnt], args.patch_size)

		# we use the first segmentation and the n previous ones
		# used_frame_feats = [frame1_feat] + [pair[0] for pair in list(que.queue)]
		# used_segs = [first_seg] + [pair[1] for pair in list(que.queue)]

		# pop out oldest frame if neccessary
		# if que.qsize() == args.n_last_frames:
		# 	que.get()
		# push current results into queue
		# seg = copy.deepcopy(frame_tar_avg)
		# que.put([feat_tar, seg])

		seg = helper.salt_pepper(seg, noise)
		frame_tar_avg, _, mask_neighborhood = label_propagation(args, model, frame_tar, seg, diffuse, mask_neighborhood)
		# frame_tar_avg = copy.deepcopy(seg)
		# set_trace()

		# upsampling & argmax
		frame_tar_avg = F.interpolate(frame_tar_avg, scale_factor=args.patch_size, mode='bilinear', align_corners=False, recompute_scale_factor=False)[0]
		frame_tar_avg = helper.norm_mask(frame_tar_avg)
		_, frame_tar_seg = torch.max(frame_tar_avg, dim=0)

		# saving to disk
		frame_tar_seg = np.array(frame_tar_seg.squeeze().cpu(), dtype=np.uint8)
		frame_tar_seg = np.array(Image.fromarray(frame_tar_seg).resize((ori_w, ori_h), 0))
		frame_nm = frame_list[cnt].split('/')[-1].replace(".jpg", ".png")
		helper.imwrite_indexed(os.path.join(video_folder, frame_nm), frame_tar_seg, color_palette)

def restrict_neighborhood(h, w):
	# We restrict the set of source nodes considered to a spatial neighborhood of the query node (i.e. ``local attention'')
	mask = torch.zeros(h, w, h, w)
	for i in range(h):
		for j in range(w):
			for p in range(2 * args.size_mask_neighborhood + 1):
				for q in range(2 * args.size_mask_neighborhood + 1):
					if i - args.size_mask_neighborhood + p < 0 or i - args.size_mask_neighborhood + p >= h:
						continue
					if j - args.size_mask_neighborhood + q < 0 or j - args.size_mask_neighborhood + q >= w:
						continue
					mask[i, j, i - args.size_mask_neighborhood + p, j - args.size_mask_neighborhood + q] = 1

	mask = mask.reshape(h * w, h * w)
	return mask.cuda(non_blocking=True)

def label_propagation(args, model, frame_tar, seg, diffuse, mask_neighborhood=None):
	"""
	propagate segs of frames in list_frames to frame_tar
	"""
	## we only need to extract feature of the target frame
	feat_tar, h, w = extract_feature(model, frame_tar, return_h_w=True)
	seg = seg.cuda()

	return_feat_tar = feat_tar.T # dim x h*w

	aff, mask_neighborhood = helper.diffusion_image(feat_tar, args.size_mask_neighborhood, args.topk,
        h, w, mask_neighborhood, infi_alpha=diffuse, sparse = args.sparse)

	C = seg.shape[1]
	seg = seg.reshape(C, -1)#.transpose(2, 1).reshape(-1, C).T # C x nmb_context*h*w
	seg_tar = torch.mm(seg, aff)
	seg_tar = seg_tar.reshape(1, C, h, w)
	return seg_tar, return_feat_tar, mask_neighborhood

def extract_feature(model, frame, return_h_w=False):
	"""Extract one frame feature everytime."""
	out = model.get_intermediate_layers(frame.unsqueeze(0).cuda(), n=1)[0]
	out = out[:, 1:, :]  # we discard the [CLS] token
	h, w = int(frame.shape[1] / model.patch_embed.patch_size), int(frame.shape[2] / model.patch_embed.patch_size)
	dim = out.shape[-1]
	out = out[0].reshape(h, w, dim)
	out = out.reshape(-1, dim)
	if return_h_w:
		return out, h, w
	return out

def process_vid_main(args, model, color_palette, video_list, noise=0, diffuse=0):
	print(os.getpid(), "Start Noise: ", noise, "\tAlpha: ", diffuse, "\tGPU: ", args.gpu)
	log_path = os.path.join(args.log, "img_snt_" + str(noise) + "_" + str(diffuse) + ".txt")
	sys.stdout = open(log_path, "w")
	sys.stderr = sys.stdout
	print("Eval image sanity")
	print("Start Noise: ", noise, "\tAlpha: ", diffuse, "\tGPU: ", args.gpu)
	# log_file.write("Noise: " + str(noise) + "\tAlpha: " + str(diffuse) + "\n")
	for i, video_name in enumerate(video_list):
		video_name = video_name.strip()
		print(f'[{i}/{len(video_list)}] Begin to segmentate video {video_name}.')
		video_dir = os.path.join(args.data_path, "JPEGImages/480p/", video_name)
		frame_list = helper.read_frame_list(video_dir)
		seg_list = helper.get_seg_list(frame_list)
		eval_img_seg_sanity_davis(args, model, frame_list, video_dir,
			seg_list, color_palette, noise, diffuse)
	sys.stdout.close()
	sys.stderr.close()
	sys.stdout = sys.__stdout__
	sys.stderr = sys.__stderr__
	print("Done Noise: ", noise, "\tAlpha: ", diffuse)

if __name__ == '__main__':
	parser = argparse.ArgumentParser('Evaluation with video object segmentation on DAVIS 2017')
	parser.add_argument('--pretrained_weights', default='', type=str, help="Path to pretrained weights to evaluate.")
	parser.add_argument('--arch', default='vit_small', type=str,
		choices=['vit_tiny', 'vit_small', 'vit_base'], help='Architecture (support only ViT atm).')
	parser.add_argument('--patch_size', default=16, type=int, help='Patch resolution of the model.')
	parser.add_argument("--checkpoint_key", default="teacher", type=str, help='Key to use in the checkpoint (example: "teacher")')
	parser.add_argument('--output_dir', default=".", help='Path where to save segmentations')
	parser.add_argument('--data_path', default='/path/to/davis/', type=str)
	parser.add_argument("--n_last_frames", type=int, default=7, help="number of preceeding frames")
	parser.add_argument("--size_mask_neighborhood", default=12, type=int,
		help="We restrict the set of source nodes considered to a spatial neighborhood of the query node")
	parser.add_argument("--topk", type=int, default=5, help="accumulate label from top k neighbors")
	parser.add_argument("--bs", type=int, default=6, help="Batch size, try to reduce if OOM")
	parser.add_argument("--log", type=str, default="logs", help="Log file")
	parser.add_argument("--noise", type=float, default=0, help="Noise level to add to the segmentation")
	parser.add_argument("--gpu", type=str, default="0", help="GPU number to use")
	parser.add_argument("--sparse", type=int, default=1, help="Sparse or not")
	# parser.add_argument("--diffuse", type=float, default=0, help="Diffusion alpha")
	args = parser.parse_args()
	os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu

	print(os.getpid())
	print("git:\n  {}\n".format(utils.get_sha()))
	print("\n".join("%s: %s" % (k, str(v)) for k, v in sorted(dict(vars(args)).items())))

	# building network
	model = vits.__dict__[args.arch](patch_size=args.patch_size, num_classes=0)
	print(f"Model {args.arch} {args.patch_size}x{args.patch_size} built.")
	model.cuda()
	utils.load_pretrained_weights(model, args.pretrained_weights, args.checkpoint_key, args.arch, args.patch_size)
	for param in model.parameters():
		param.requires_grad = False
	model.eval()

	color_palette = []
	for line in urlopen("https://raw.githubusercontent.com/Liusifei/UVC/master/libs/data/palette.txt"):
		color_palette.append([int(i) for i in line.decode("utf-8").split('\n')[0].split(" ")])
	color_palette = np.asarray(color_palette, dtype=np.uint8).reshape(-1,3)

	video_list = open(os.path.join(args.data_path, "ImageSets/2017/val.txt")).readlines()
	noise_list = [args.noise]#[0]#, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1]
	diffuse_list = list(np.arange(1.0, 1.02, 0.1))

	mp.set_start_method('spawn')
	# all_processes = mp.Queue()
	for i, noise in enumerate(noise_list):
		for diffuse in diffuse_list:
			p = mp.Process(target=process_vid_main, args=(args, model, color_palette, video_list, noise, diffuse))
			p.start()
			# print(all_processes.get())
			# all_processes.append(p)
			# process_vid_main(args, model, color_palette, video_list, noise, diffuse)

	# for p in all_processes:
	# 	p.join()

max_stars_repo_name: maitreygram/DiNO

max_issues_repo_name: maitreygram/DiNO

max_forks_repo_name: maitreygram/DiNO


text:
# PolynomialNetwork.py
#
# Bryan Daniels
# 7.20.2009
#
# SloppyCell network that does not have any ODEs, but simply
# consists of an nth degree polynomial.
# (Branched from laguerreNetwork.py)
#

from SloppyCell.ReactionNetworks import *
import scipy.special

def PolynomialNetwork(degree,outputName='output',netid='PolynomialNet',
    initOptimizable=False):
    """
    SloppyCell ('network') model that does not have any ODEs, but simply
    consists of an nth degree polynomial.

    Currently produces one output, with number of parameters = degree+1:

    output(t) = sum_{i=0}^{degree} g_i x^i

    (g0 is renamed to outputName+'_init')
    """

    net = Network(netid, name='Polynomial (network) model')
    net.addCompartment('Comp',name='Compartment')

    net.addParameter( 'degree', degree, isOptimizable=False )

    # Do I need this?  Unfortunately, I think I do...
    net.addSpecies('dummy','Comp',0.)
    net.addRateRule('dummy','0.')

    # add optimizable parameters
    net.addParameter('g0', 0., isOptimizable=False)
    net.addParameter(outputName+'_init', 0., isOptimizable=initOptimizable)
    net.addAssignmentRule('g0',outputName+'_init')
    for i in range(1,degree+1):
        net.addParameter( 'g'+str(i), 0., isOptimizable=True )

    net.addSpecies( outputName, 'Comp', 0. )

    polynomial = ''
    for i in range(degree+1):
        polynomial += 'g'+str(i)+'*time**'+str(i)+' + '

    net.addAssignmentRule( outputName, polynomial[:-2] )

    return net

max_stars_repo_name: sidambhire/SirIsaac

max_issues_repo_name: sidambhire/SirIsaac

max_forks_repo_name: sidambhire/SirIsaac

reason: import scipy


text:
import sys
import skimage
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' #No prints from the tensorflow side

from ..constants import OCRD_TOOL

from ocrd import Processor
from ocrd_modelfactory import page_from_file
from ocrd_utils import (
    getLogger,
    concat_padded,
    MIMETYPE_PAGE,
    coordinates_for_segment,
    points_from_polygon,
    polygon_from_points
    )
import cv2
import warnings
import ocrolib
warnings.filterwarnings('ignore',category=FutureWarning)
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from pathlib import Path
import numpy as np
import matplotlib.path as pltPath
from shapely.geometry import Polygon

from ocrd_anybaseocr.mrcnn import model
from ocrd_anybaseocr.mrcnn.config import Config
from ocrd_models.constants import NAMESPACES as NS

from ocrd_models.ocrd_page import (
    CoordsType,
    TextRegionType,
    AlternativeImageType,
    to_xml,
    MetadataItemType,
    LabelsType, LabelType,
    RegionRefIndexedType, OrderedGroupType, ReadingOrderType
)
from ocrd_models.ocrd_page_generateds import CoordsType

TOOL = 'ocrd-anybaseocr-block-segmentation'
LOG = getLogger('OcrdAnybaseocrBlockSegmenter')
FALLBACK_IMAGE_GRP = 'OCR-D-IMG-BLOCK-SEGMENT'

class InferenceConfig(Config):

    def __init__(self,confidence):
        Config.__init__(self, confidence)

    NAME = "block"
    IMAGES_PER_GPU = 1
    NUM_CLASSES = 1 + 14

#     NAME = "block"
#     IMAGES_PER_GPU = 1
#     NUM_CLASSES = 1 + 14
#     DETECTION_MIN_CONFIDENCE = 0.9 # needs to be changed back to parameter
    #     DETECTION_MIN_CONFIDENCE = DETECTION_MIN_CONFIDENCE #taken as a parameter from tools.json

class OcrdAnybaseocrBlockSegmenter(Processor):

    def __init__(self, *args, **kwargs):
        kwargs['ocrd_tool'] = OCRD_TOOL['tools'][TOOL]
        kwargs['version'] = OCRD_TOOL['version']
        super(OcrdAnybaseocrBlockSegmenter, self).__init__(*args, **kwargs)
        #self.reading_order = []
        self.order = 0

    def process(self):

        if not tf.test.is_gpu_available():
            LOG.error("Your system has no CUDA installed. No GPU detected.")
            sys.exit(1)
        try:
            page_grp, self.image_grp = self.output_file_grp.split(',')
        except ValueError:
            page_grp = self.output_file_grp
            self.image_grp = FALLBACK_IMAGE_GRP
            LOG.info("No output file group for images specified, falling back to '%s'", FALLBACK_IMAGE_GRP)

        model_path = Path(self.parameter['block_segmentation_model'])
        model_weights = Path(self.parameter['block_segmentation_weights'])

        confidence = self.parameter['DETECTION_MIN_CONFIDENCE']
#         DETECTION_MIN_CONFIDENCE = Path(self.parameter['DETECTION_MIN_CONFIDENCE'])

        class_names = ['BG','page-number', 'paragraph', 'catch-word', 'heading', 'drop-capital', 'signature-mark','header',
                       'marginalia', 'footnote', 'footnote-continued', 'caption', 'endnote', 'footer','keynote',
                       'image','table', 'graphics']

        if not Path(model_weights).is_file():
            LOG.error("""\
                Block Segmentation model weights file was not found at '%s'. Make sure the `model_weights` parameter
                points to the local model weights path.
                """ % model_weights)
            sys.exit(1)

#         config = InferenceConfig(Config,DETECTION_MIN_CONFIDENCE)

        config = InferenceConfig(confidence)
#         config = InferenceConfig()
        mrcnn_model = model.MaskRCNN(mode="inference", model_dir=str(model_path), config=config)
        mrcnn_model.load_weights(str(model_weights), by_name=True)

        oplevel = self.parameter['operation_level']
        for (n, input_file) in enumerate(self.input_files):

            pcgts = page_from_file(self.workspace.download_file(input_file))
            page = pcgts.get_Page()
            page_id = input_file.pageId or input_file.ID

            metadata = pcgts.get_Metadata()
            metadata.add_MetadataItem(
                    MetadataItemType(type_="processingStep",
                                     name=self.ocrd_tool['steps'][0],
                                     value=TOOL,
                                     Labels=[LabelsType(#externalRef="parameter",
                                                        Label=[LabelType(type_=name,
                                                                         value=self.parameter[name])
                                                               for name in self.parameter.keys()])]))

            page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter ='binarized,deskewed,cropped,clipped,non_text')
            # try to load pixel masks
            try:
                mask_image, mask_xywh, mask_image_info = self.workspace.image_from_page(page, page_id, feature_selector ='clipped', feature_filter='binarized,deskewed,cropped,non_text')
            except:
                mask_image=None
            #Display Warning If image segment results already exist or not in StructMap?
            regions = page.get_TextRegion() + page.get_TableRegion()
            if regions:
                LOG.warning("Image already has text segments!")

            if oplevel=="page":
                self._process_segment(page_image, page, page_xywh, page_id, input_file, n, mrcnn_model, class_names, mask_image)
            else:
                LOG.warning('Operation level %s, but should be "page".', oplevel)
                break
            file_id = input_file.ID.replace(self.input_file_grp, page_grp)

            # Use input_file's basename for the new file -
            # this way the files retain the same basenames:
            if file_id == input_file.ID:
                file_id = concat_padded(page_grp, n)
            self.workspace.add_file(
                ID=file_id,
                file_grp=page_grp,
                pageId=input_file.pageId,
                mimetype=MIMETYPE_PAGE,
                local_filename=os.path.join(self.output_file_grp,
                                            file_id + '.xml'),
                content=to_xml(pcgts).encode('utf-8'),
                force=self.parameter['force']
            )

    def _process_segment(self,page_image, page, page_xywh, page_id, input_file, n, mrcnn_model, class_names, mask):
        #check for existing text regions and whether to overwrite them
        border = None
        if page.get_TextRegion():
            if self.parameter['overwrite']:
                LOG.info('removing existing TextRegions in page "%s"', page_id)
                textregion.set_TextRegion([])
            else:
                LOG.warning('keeping existing TextRegions in page "%s"', page_id)
                return
        #check if border exists
        if page.get_Border():
            border_coords = page.get_Border().get_Coords()
            border_points = polygon_from_points(border_coords.get_points())
            border = Polygon(border_points)
#            page_image, page_xy = self.workspace.image_from_segment(page.get_Border(), page_image, page_xywh)

        img_array = ocrolib.pil2array(page_image)
        page_image.save('./checkthis.png')
        if len(img_array.shape) <= 2:
            img_array = np.stack((img_array,)*3, axis=-1)
        results = mrcnn_model.detect([img_array], verbose=1)
        r = results[0]

        th=self.parameter['th']
        # check for existing semgentation mask
        #this code executes only when use_deeplr is set to True in ocrd-tool.json file
        if mask:
            mask = ocrolib.pil2array(mask)
            mask = mask//255
            mask = 1-mask
            # multiply all the bounding box part with 2
            for i in range(len(r['rois'])):

                min_x = r['rois'][i][0]
                min_y = r['rois'][i][1]
                max_x = r['rois'][i][2]
                max_y = r['rois'][i][3]
                mask[min_x:max_x, min_y:max_y] *= i+2
            cv2.imwrite('mask_check.png', mask*(255/(len(r['rois'])+2)))

            # check for left over pixels and add them to the bounding boxes
            pixel_added = True

            while pixel_added:

                pixel_added = False
                left_over = np.where(mask==1)
                for x,y in zip(left_over[0], left_over[1]):
                    local_mask = mask[x-th:x+th,y-th:y+th]
                    candidates = np.where(local_mask>1)
                    candidates = [k for k in zip(candidates[0], candidates[1])]
                    if len(candidates)>0:
                        pixel_added = True
                        #find closest pixel with x>1
                        candidates.sort(key= lambda j: np.sqrt((j[0]-th)**2+(j[1]-th)**2))
                        index = local_mask[candidates[0]]-2

                        #add pixel to mask/bbox
                        #x,y to bbox with index
                        if x < r['rois'][index][0]:
                            r['rois'][index][0] = x

                        elif x > r['rois'][index][2]:
                            r['rois'][index][2] = x

                        if y < r['rois'][index][1]:
                            r['rois'][index][1] = y

                        elif y > r['rois'][index][3]:
                            r['rois'][index][3] = y

                        # update the mask
                        mask[x,y] = index + 2

        #resolving overlapping problem
        bbox_dict = {} #to check any overlapping bbox
        class_id_check = []

        for i in range(len(r['rois'])):
            min_x = r['rois'][i][0]
            min_y = r['rois'][i][1]
            max_x = r['rois'][i][2]
            max_y = r['rois'][i][3]

            region_bbox = [min_y, min_x, max_y, max_x]

            for key in bbox_dict:
                for bbox in bbox_dict[key]:

                    #checking for ymax case with vertical overlapping
                    #along with y, check both for xmax and xmin
                    if (region_bbox[3] <= bbox[3] and region_bbox[3] >= bbox[1] and
                        ((region_bbox[0] >= bbox[0] and region_bbox[0] <= bbox[2]) or (region_bbox[2] >= bbox[0]
                        and region_bbox[2] <= bbox[2]) or (region_bbox[0] <= bbox[0] and region_bbox[2] >= bbox[2]))
                        and r['class_ids'][i] != 5 ):

                        r['rois'][i][2] = bbox[1] -1

                    #checking for ymin now
                    #along with y, check both for xmax and xmin
                    if (region_bbox[1] <= bbox[3] and region_bbox[1] >= bbox[1] and
                        ((region_bbox[0] >= bbox[0] and region_bbox[0] <= bbox[2]) or (region_bbox[2] >= bbox[0]
                        and region_bbox[2] <= bbox[2]) or (region_bbox[0] <= bbox[0] and region_bbox[2] >= bbox[2]))
                        and r['class_ids'][i] != 5 ):

                        r['rois'][i][0] = bbox[3] + 1

            if r['class_ids'][i] not in class_id_check:
                bbox_dict[r['class_ids'][i]] = []
                class_id_check.append(r['class_ids'][i])

            bbox_dict[r['class_ids'][i]].append(region_bbox)

        #resolving overlapping problem code

        # define reading order on basis of coordinates
        reading_order = []

        for i in range(len(r['rois'])):
            width,height,_ = img_array.shape
            min_x = r['rois'][i][0]
            min_y = r['rois'][i][1]
            max_x = r['rois'][i][2]
            max_y = r['rois'][i][3]

            if (min_y - 5) > width and r['class_ids'][i] == 2:
                min_y-=5
            if (max_y + 10) < width and r['class_ids'][i] == 2:
                min_y+=10
            reading_order.append((min_y, min_x, max_y, max_x))

        reading_order = sorted(reading_order, key=lambda reading_order:(reading_order[1], reading_order[0]))
        for i in range(len(reading_order)):
            min_y, min_x, max_y, max_x = reading_order[i]
            min_y = 0
            i_poly = Polygon([[min_x, min_y], [max_x, min_y], [max_x, max_y], [min_x, max_y]])
            for j in range(i+1,len(reading_order)):
                min_y, min_x, max_y, max_x = reading_order[j]
                j_poly = Polygon([[min_x, min_y], [max_x, min_y], [max_x, max_y], [min_x, max_y]])
                inter = i_poly.intersection(j_poly)
                if inter:
                    reading_order.insert(j+1,reading_order[i])
                    del reading_order[i]

        #Creating Reading Order object in PageXML
        order_group = OrderedGroupType(caption="Regions reading order",id=page_id)

        for i in range(len(r['rois'])):
            min_x = r['rois'][i][0]
            min_y = r['rois'][i][1]
            max_x = r['rois'][i][2]
            max_y = r['rois'][i][3]
            if (min_y - 5) > width and r['class_ids'][i] == 2:
                min_y-=5
            if (max_y + 10) < width and r['class_ids'][i] == 2:
                min_y+=10

            region_polygon = [[min_x, min_y], [max_x, min_y], [max_x, max_y], [min_x, max_y]]

            if border:
                cut_region_polygon = border.intersection(Polygon(region_polygon))
                if cut_region_polygon.is_empty:
                    continue
            else:
                cut_region_polygon = Polygon(region_polygon)

            order_index = reading_order.index((min_y, min_x, max_y, max_x))
            region_id = '%s_region%04d' % (page_id, i)
            regionRefIndex = RegionRefIndexedType(index=order_index, regionRef=region_id)
            order_group.add_RegionRefIndexed(regionRefIndex)

        reading_order_object = ReadingOrderType()
        reading_order_object.set_OrderedGroup(order_group)
        page.set_ReadingOrder(reading_order_object)

        for i in range(len(r['rois'])):
            width,height,_ = img_array.shape
            min_x = r['rois'][i][0]
            min_y = r['rois'][i][1]
            max_x = r['rois'][i][2]
            max_y = r['rois'][i][3]

            if (min_y - 5) > width and r['class_ids'][i] == 2:
                min_y-=5
            if (max_y + 10) < width and r['class_ids'][i] == 2:
                min_y+=10

            #one change here to resolve flipped coordinates
            region_polygon = [[min_y, min_x], [max_y, min_x], [max_y, max_x], [min_y, max_x]]

            cut_region_polygon = border.intersection(Polygon(region_polygon))

            if cut_region_polygon.is_empty:
                continue
            cut_region_polygon = [j for j in zip(list(cut_region_polygon.exterior.coords.xy[0]),list(cut_region_polygon.exterior.coords.xy[1]))][:-1]

            #checking whether coordinates are flipped

            region_polygon = coordinates_for_segment(cut_region_polygon, page_image, page_xywh)
            region_points = points_from_polygon(region_polygon)

            read_order = reading_order.index((min_y, min_x, max_y, max_x))

            # this can be tested, provided whether we need previous comments or not?
            #resolving overlapping problem

            region_img = img_array[min_x:max_x,min_y:max_y] #extract from points and img_array

            region_img = ocrolib.array2pil(region_img)

            file_id = input_file.ID.replace(self.input_file_grp, self.image_grp)
            if file_id == input_file.ID:
                file_id = concat_padded(self.image_grp, n)

            file_path = self.workspace.save_image_file(region_img,
                                   file_id+"_"+str(i),
                                   page_id=page_id,
                                   file_grp=self.image_grp,
                                   force=self.parameter['force'])

            # ai = AlternativeImageType(filename=file_path, comments=page_xywh['features'])
            region_id = '%s_region%04d' % (page_id, i)
            coords = CoordsType(region_points)

            #incase of imageRegion
            if r['class_ids'][i] == 15:
                image_region = ImageRegionType(custom='readingOrder {index:'+str(read_order)+';}',id=region_id ,Coords=coords, type_=class_names[r['class_ids'][i]])
                # image_region.add_AlternativeImage(ai)
                page.add_ImageRegion(image_region)
                continue
            if r['class_ids'][i] == 16:
                table_region = TableRegionType(custom='readingOrder {index:'+str(read_order)+';}',id=region_id ,Coords=coords, type_=class_names[r['class_ids'][i]])
                # table_region.add_AlternativeImage(ai)
                page.add_TableRegion(table_region)
                continue
            if r['class_ids'][i] == 17:
                graphic_region = GraphicRegionType(custom='readingOrder {index:'+str(read_order)+';}',id=region_id ,Coords=coords, type_=class_names[r['class_ids'][i]])
                # graphic_region.add_AlternativeImage(ai)
                page.add_GraphicRegion(graphic_region)
                continue

            textregion = TextRegionType(custom='readingOrder {index:'+str(read_order)+';}',id=region_id ,Coords=coords, type_=class_names[r['class_ids'][i]])
            # textregion.add_AlternativeImage(ai)

            #border = page.get_Border()
            #if border:
            #    border.add_TextRegion(textregion)
            #else:
            page.add_TextRegion(textregion)


text:
from numpy import array, array_equal, ndarray
from os import system
from platform import system as os_name
from random import choice

def clear_console():
    if 'Windows' in os_name():
        system('cls')
    else:
        system('clear')

def prep_players():
    def clear_console():
        if 'Windows' in os_name():
            system('cls')
        else:
            system('clear')

    def choose_player_X_or_O():
        player = input("Play as X or O?  (Enter 'X' or 'O'): ")
        if player in ('X', 'x'):
            return 'X'
        elif player in ('O', 'o'):
            return 'O'
        else:
            print("Invalid selection!")
            return choose_player_X_or_O()

    def set_computer_X_or_O(player):
        if player is 'X':
            return 'O'
        elif player is 'O':
            return 'X'

    def who_goes_first():
        first = input("You go first?  (Enter 'Y' or 'N'): ")
        if first in ('Y', 'y', 'Yes', 'yes'):
            return "player"
        elif first in ('N', 'n', 'No', 'no'):
            return "computer"
        else:
            print("Invalid selection!")
            return who_goes_first()

    player = choose_player_X_or_O()
    computer = set_computer_X_or_O(player)
    first = who_goes_first()
    return player, computer, first

def prep_board():
    def init_board():
        board = ndarray((3, 3), dtype=object)
        board[:] = ''
        return board

    def init_possible_moves():
        return [(row, col) for row in range(3) for col in range(3)]

    board = init_board()
    possible_moves = init_possible_moves()
    return board, possible_moves

class Player:
    def __init__(self, player):
        self.player = player

    def convert_int(self, coord):
        try:
            return int(coord)
        except ValueError:
            return -1

    def check_input(self, row, col):
        if 0 <= row <= 2 and 0 <= col <= 2:
            return True
        else:
            return False

    def player_turn(self, board, possible_moves):
        row = self.convert_int(input("Select Row: "))
        col = self.convert_int(input("Select Column: "))
        is_valid_move = self.check_input(row, col)
        if is_valid_move is True and board[row, col] == '':
            board[row, col] = self.player
            del possible_moves[possible_moves.index((row, col))]
        elif is_valid_move is False or board[row, col] != '':
            print("Invalid Selection!")
            return self.player_turn(board, possible_moves)
        return board, possible_moves

class Computer:
    def __init__(self, computer):
        self.computer = computer

    def evaluate(board, score):
        if check_win(board, tuple(computer.computer * 3), False) is True:
            score += 10
        elif check_win(board, tuple(player.player * 3), True) is True:
            score -= 10
        return score

    # def minimax(self, depth, possible_moves, isComputer):
    #     for move in possible_moves:
    #         score = self.minimax(state, depth - 1, not isComputer)
    #     return depth

    def computer_turn(self, board, possible_moves):
        # depth = len(board[board == ''])
        # if depth == 9:
        row, col = choice(possible_moves)
        # else:
        # self.minimax(board, depth, possible_moves, True)
        board[row, col] = self.computer
        del possible_moves[possible_moves.index((row, col))]
        return board, possible_moves

def gen_win_conditions(board, letters):
    return array([[
        array_equal(board[:, i], letters),
        array_equal(board[i, :], letters),
        array_equal(board.diagonal(), letters),
        array_equal(board[:, ::-1].diagonal(), letters)
    ] for i in range(3)])

def check_win(board, letters, isPlayer):
    if True in gen_win_conditions(board, letters):
        print(board)
        if isPlayer is True:
            print("You win!")
        elif isPlayer is False:
            print("You lose!")
        return True
    elif '' not in board:
        print(board)
        print("Draw!")
        return True

'''Prep the game'''
# Init params
player, computer, first = prep_players()
board, possible_moves = prep_board()

# Init objects
player = Player(player)
computer = Computer(computer)

# Play the game
clear_console()
if first is "computer":
    board, possible_moves = computer.computer_turn(board, possible_moves)
while '' in board:
    print(board)
    board, possible_moves = player.player_turn(board, possible_moves)
    if check_win(board, tuple(player.player * 3), True) is True:
        break
    board, possible_moves = computer.computer_turn(board, possible_moves)
    if check_win(board, tuple(computer.computer * 3), False) is True:
        break
    clear_console()

max_stars_repo_name: summonholmes/minimax-tictactoe

max_issues_repo_name: summonholmes/minimax-tictactoe

max_forks_repo_name: summonholmes/minimax-tictactoe

reason: from numpy


text:
from __future__ import print_function
import numpy as np
from AutoEncodingTopoFactors import AutoEncodingTopoFactor
from tfa_utils import plot, coordinate_tensor
from datetools import addDateTime
import sys
from hotspot_init import hot_spot_init
from TopoFactorAnalysisTF import TFA

from optparse import OptionParser
import pickle
import os

cur_date = addDateTime()
parser = OptionParser()

# Hyperparameters for the model
k = 25
#lr = 1e-5
#Epochs = 300
#batch_size = 50

parser.add_option("--rltdir", dest='rltdir', default='Experiment')
parser.add_option("--lr", dest='learning_rate', default=lr, type='float')
parser.add_option("--maxepochs", dest='maxepochs', default=Epochs, type='int')
parser.add_option("--batchsize", dest='batchsize', default=batch_size, type='int')

args = sys.argv
(options, args) = parser.parse_args(args)

#local_rlt_root = './rslts/NYU_Rest_30_(300)/'
local_rlt_root = './rslts/TFA/GRF_25/'
local_rlt_dir = local_rlt_root + options.rltdir + cur_date + '/'
RLT_DIR = local_rlt_dir

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

#subdir = '/Users/antoniomoretti/Documents/Research/peerlab/14.10.17_SpatialFactorAnalysis-master-3c97fd13aab276d6f7221dea986695ad6cb2e43b/'

subdir = os.path.join(os.getcwd(), 'data')
#'/Users/antoniomoretti/Documents/Research/peerlab/Spring18/AETF/data'

data = np.load(subdir + '/Sim03.npy')
train_data = data[0:5]
test_data = data[170:]

'''
train_data = np.load(subdir + "/NYU_Functional_AB.npy")
test_data = np.load(subdir + "/NYU_Functional_C.npy")

train_data = (train_data - np.mean(train_data)) / np.std(train_data)
test_data = (test_data - np.mean(test_data)) / np.std(test_data)

dim_data = list(train_data.shape[1:])
model = AutoEncodingTopoFactor(dim_data, k, options, RLT_DIR)
print("initialized")

scan_centers, scan_widths, scan_weights, test_error, test_centers, \
test_widths, test_weights, train_loss, train_mse = model.train(train_data, test_data)

#import pdb
#pdb.set_trace()

#total_error, train_mse = model.train_loss(train_data)

model_params = {
    'nsources' : k,
    'learning_rate' : lr,
    'epochs' : Epochs,
    'batch_size' : batch_size,
    'centers' : scan_centers,
    'widths'  : scan_widths,
    'weigts'  : scan_weights,
    'train_error' : train_loss,
    'train_mse' : train_mse,
    'test_error' : test_error,
    'test_centers' : test_centers,
    'test_widths' : test_widths,
    'test_weights' : test_weights,
    'Ytrain' : train_data,
    'Ytest' : test_data
}

pickle.dump( model_params, open(RLT_DIR + 'model_params.p', 'wb'))
'''

y = train_data

theta = {
    'width_log_pre': -1.0986122886681098,
    'mu_var': np.array([177.03779675, 181.25900742]),
    'weight_log_pre': -0.6931471805599453,
    'y_var': 0.01,
    'center_log_pre': np.array([-7.47894834, -7.50251208]),
    'width_mean': 1.0,
    'center_mean': np.array([23.46954512, 21.94749435]),
    'weight_mean': 0.0
}

centers, widths = hot_spot_init(y, k)
print("Hotspot Init completed. Centers:\n", centers)
tfa = TFA(y, k, theta, center_init=centers, width_init=widths, save_dir=RLT_DIR)
print("fitting model 1...")
tfa.train()

#import pdb
#pdb.set_trace()

print('All done!')