text:
## -*- coding: utf-8 -*-

# from sys import getsizeof
from datetime import date,datetime,timedelta
from calendar import monthrange
from pprint import pprint
from pathlib import Path
import json
import lzma

from xlwt.Utils import rowcol_to_cell
import numpy as np
import xlrd
import xlwt
__version__="1.5.4"
# import loguru
# import enum

module_dir = Path.cwd()/"journals/"
print("Saving jornals here ",module_dir)
module_dir.mkdir(exist_ok=True)

def saveToExcel(array,fn):
    wb = xlwt.Workbook()
    ws = wb.add_sheet("q")
    for i,li in enumerate(array):
        for g,el in enumerate(li):

            ws.write(i,g,el)
    wb.save(f'{fn}.xls')
def loadGroupFromFile():
    fi=None
    groupName=""
    groupList=[]
    try:
        with open(f"group.json","r",encoding="utf-8") as f:
            fi=json.load(f)
        groupName=fi["groupName"]
        groupList=fi["groupList"]

    except Exception:
        with open(f"group.json","w",encoding="utf-8") as f:
            f.write('{"groupName": "","groupList": []}')
    return groupName, groupList

def loadGroupToFile(groupName,groupList):
    try:
        with open("group.json",mode="w", encoding="utf-8") as f:
            json.dump({"groupName":groupName,"groupList":groupList},f, indent=4, ensure_ascii=False)
    except Exception:
        pass

class Page():

    __slots__=("discName","people","dates","arr")
    def __init__(self,discName,people,dates,arr=None):
        self.discName=discName
        self.people=tuple(people)
        if len(dates)>0 and type(dates[0])==str:
            self.dates=[datetime.strptime(i,'%Y%m%d').date() for i in dates]
        else:
            self.dates = dates
        needSize=(len(people),len(dates))
        if arr == None:
            self.arr=np.zeros(shape=needSize,dtype=np.dtype("uint8"))
        elif isinstance(arr,np.ndarray):
            if arr.shape ==needSize:
                self.arr=arr
            else:
                raise ValueError(f"Different shapes,acepted={arr.shape},need to be {needSize}")
        elif isinstance(arr,bytes):
            self.arr = np.reshape(np.frombuffer(arr, dtype=np.uint8),newshape=needSize)
        else:
            arr=np.array(arr,dtype=np.dtype("uint8"))
            if arr.shape ==needSize:
                self.arr=arr
            else:
                raise ValueError(f"Different shapes,acepted={arr.shape},need to be {needSize}")

    def onlyMark(self,id:int,start=None,end=None):
        serList,_,_=self.selection(id,start,end)
        return [i for i in serList if i!=0 and i!=11]

    def average(self,id:int,start=None,end=None):
        li=self.onlyMark(id,start,end)
        try:
            return round(sum(li)/len(li),ndigits=2),(sum(li),len(li))
        except ZeroDivisionError:
            return 0,(0,0)

    def selection(self,id:int,start,end):
        "accept date obj not datetime"
        id=self.people.index(id)
        serList=None
        if start!=None and end!=None:
            a=list(
            set([(start + timedelta(days=x)) for x in range(0, (end-start).days+1)])
            &set(self.dates)
            )
            a.sort()
            if a!=[]:
                start=self.dates.index(a[0])
                end=self.dates.index(a[-1])
                for endid in range(end+1,len(self.dates)):
                    if self.dates[endid]==a[-1]:
                        end=endid
                end+=1
                serList=self.arr[[id], start:end]
            else:
                serList=np.array([[]],dtype=np.dtype("uint8"))
        else:
            serList=self.arr[[id], :]
        return serList[0],start,end

    def percent(self,id):
        serList=self.skiped(id,None,None)
        return round(len(serList)/self.arr.shape[1]*100,2)

    def skiped(self,id:int,start=None,end=None):
        serList,start,end=self.selection(id,start,end)
        return [self.dates[start:end][i] for i in np.where(serList == 11)[0]]

    def toDict(self):
        di={}
        di["discName"]=self.discName
        di["people"]=self.people
        di["dates"]=[i.strftime('%Y%m%d') for i in self.dates]
        di["arr"]=self.arr.tobytes().decode("utf-8")
        return di

    def loadFromSite(self,dates,array):
        dates=[datetime.strptime(i, '%Y-%m-%d').date() for i in dates]
        self.dates=dates
        needSize=(len(self.people),len(self.dates))
        for i,el in enumerate(array):
            for j,el2 in enumerate(el):
                array[i][j]=0 if array[i][j]==' ' else array[i][j]
                array[i][j]=0 if array[i][j]=='' else array[i][j]
                array[i][j]=11 if array[i][j]=='н' else array[i][j]
                array[i][j]=int(array[i][j])
        arr=np.array(array,dtype=np.dtype("uint8"))
        if arr.shape ==needSize:
            self.arr=arr

    def append(self,item:np.ndarray,dat = None):
        if dat == None:
            dat=[date.today()]
        if item.shape[0]==self.arr.shape[0]:
            self.arr=np.append(self.arr,item,axis=1)
            self.dates.extend(dat)
        else:
            raise AttributeError("different shapes")

class Journal:
    __slots__=("pages","groupList","groupname","bordersStyleMassive","style0","styleOnlyTNR","ver")
    monthList=(
    "",
    "Январь",
    "Февраль",
    "Март",
    "Апрель",
    "Май",
    "Июнь",
    "Июль",
    "Август",
    "Сентябрь",
    "Октябрь",
    "Ноябрь",
    "Декабрь"
    )
    """Class represents and operates whole journal"""
    def __init__(self, pages,groupList,grname,ver=__version__):
        self.ver=ver
        if self.ver!=__version__:
            print(f"[Warning] using EduJornal version =={self.ver}, is not safe. Resave Journals with using {__version__} version")
        self.pages = pages
        self.groupList = groupList
        self.groupname = grname
    def __repr__(self):
        return f"Journal({self.groupname})"
    def view(self,ind:int):
        pg=self.pages[ind]
        dates=[i.strftime("%Y-%m-%d") for i in pg.dates]
        di={}
        for i,fi in enumerate(pg.people):
            ar=pg.arr[i].tolist()
            for h in range(len(ar)):
                ar[h]='' if ar[h]==0 else ar[h]
                ar[h]='н' if ar[h]==11 else ar[h]
            di[i+1]=[
                self.groupList[fi],
                ar
            ]
        return dates,di

    def onlyMarkAll(self,month,year):
        start=date(year,month,1)
        dayInMonth=monthrange(year, month)[1]
        end=date(year,month,dayInMonth)
        pgs=[]
        for i in self.pages:
            pg=[]
            for id in range(len(self.groupList)):
                try:
                    pg+=[i.onlyMark(id,start,end)]
                except ValueError:
                    pg+=[[]]
            pgs+=[pg]
        return pgs

    def average(self,id,start=None,end=None):
        sum=0
        num=0
        for i in self.pages:
            try:
                _,(sumT,numT)=i.average(id,start,end)
            except ValueError:
                sumT,numT=0,0
            sum+=sumT
            num+=numT
        try:
            return round(sum/num,ndigits=2)
        except ZeroDivisionError:
            return 0

    def percent(self,id):
        pass

    def skiped(self,id:int,month,year):
        start=date(year,month,1)
        dayInMonth=monthrange(year, month)[1]
        end=date(year,month,dayInMonth)

        skp=[]
        for i in self.pages:
            try:
                s=i.skiped(id,start,end)
                for dat in s:
                    skp+=[[i.discName,dat.day]]
            except ValueError:
                pass

        nDates= set([i[1] for i in skp])
        dateDictNames={x:[]for x in nDates}
        for dateT in nDates:
            for i in skp:
                if i[1]==dateT:
                    dateDictNames[dateT].append(i[0])

        nByMonth=[0 for i in range(dayInMonth)]

        for dateT in nDates:
            nByMonth[dateT-1]=len(dateDictNames[dateT])
        return dateDictNames,nByMonth

    @classmethod
    def fromFile(cls,filepath):
        with lzma.open(filepath, "r") as lzf:
            dum1=lzf.read().decode('utf-8')
        k=json.loads(dum1)
        tmp=[]
        for i in k["pages"]:
            i["arr"]=i["arr"].encode("utf-8")
            tmp.append(Page(**i))
        k["pages"]=tmp
        return cls(**k)

    @classmethod
    def fromBytes(cls,data):
        dum1=lzma.decompress(data).decode('utf-8')
        k=json.loads(dum1)
        tmp=[]
        for i in k["pages"]:
            i["arr"]=i["arr"].encode("utf-8")
            tmp.append(Page(**i))
        k["pages"]=tmp
        return cls(**k)

    def toBytes(self):
        di={}
        di["ver"]=self.ver
        di["grname"]=self.groupname
        di["pages"]=[i.toDict() for i in self.pages]
        di["groupList"]=self.groupList
        return lzma.compress(json.dumps(di).encode('utf-8'))

    @classmethod
    def fromExel(cls,pat):
        ReadedBook = xlrd.open_workbook(pat,formatting_info=True)
        ListNames=ReadedBook.sheet_names()
        sheetsWithData=[[i.row_values(rn)[1:] for rn in range(i.nrows)] for i in ReadedBook.sheets()]

        #del empty rows
        emptyRows=[]
        for i,name in enumerate(sheetsWithData):
            for g,row in enumerate(name[1:]):
                if row[0].strip()=='':
                    emptyRows.append((i,g+1))

        for i in range(len(emptyRows)-1,-1,-1):
            del sheetsWithData[emptyRows[i][0]][emptyRows[i][1]]

        def toUint8(val):
            val=0 if val==' ' else val
            val=0 if val=='' else val
            val=11 if val=='н' else val
            return int(val)
        grName,grlist=loadGroupFromFile()
        jorn=cls([],grlist,grName.upper())
        for i,el in enumerate(sheetsWithData):
            tmp=Page(
                ListNames[i],
                [jorn.groupList.index(rt[0]) for rt in el[1:]],
                [])
            for h,datej in enumerate(el[0][1:]):
                if type(datej)==float:
                    datej=xlrd.xldate_as_datetime(datej,ReadedBook.datemode).date()
                    mamm=np.array(list(map(toUint8,[k[h+1] for k in el[1:]])),dtype=np.uint8)
                    mamm=np.reshape(mamm,(-1,1))
                    tmp.append(mamm,[datej])
            jorn.pages.append(tmp)
        # jorn.saveToFile()
        return jorn

    def saveToFile(self):
        dum0=self.toBytes()

        with open(module_dir/f"{self.groupname}.Jornl", "wb") as lzf:
            lzf.write(dum0)

        with open(module_dir/f"{self.groupname}.Jornl", "rb") as lzf:
            dum1=lzf.read()

        if(dum0==dum1):
            print("file correct writen")
        else:
            print("error writing file")

    def stylesDefenition(self):
        self.bordersStyleMassive=[[],[]]

        self.style0=xlwt.XFStyle()
        self.styleOnlyTNR = xlwt.XFStyle()

        font0 = xlwt.Font()
        font0.name = 'Times New Roman'
        self.styleOnlyTNR.font=font0
        self.style0.font = font0

        alignmentObj=xlwt.Alignment()
        self.style0.alignment = alignmentObj
        self.style0.alignment.horz= alignmentObj.HORZ_CENTER
        self.style0.alignment.vert=alignmentObj.VERT_CENTER

        bord=xlwt.Borders()
        self.style0.borders=bord
        self.style0.borders.left   = bord.MEDIUM
        self.style0.borders.right  = bord.MEDIUM
        self.style0.borders.top    = bord.MEDIUM
        self.style0.borders.bottom = bord.MEDIUM

    def retStyle(self,cube0_r,cube_r,cube0_c,cube_c,actual_r,actual_c):
        tmpBoles=[
            actual_r==cube0_r,
            actual_r==cube_r,
            actual_c==cube0_c,
            actual_c==cube_c
        ]
        if tmpBoles in self.bordersStyleMassive[0]:
            tmpSt=self.bordersStyleMassive[1][self.bordersStyleMassive[0].index(tmpBoles)]
        else:
            tmpSt=xlwt.XFStyle()
            b=xlwt.Borders()
            tmpSt.borders=b
            tmpSt.borders.top = b.MEDIUM if tmpBoles[0] else b.THIN
            tmpSt.borders.bottom = b.MEDIUM if tmpBoles[1] else b.THIN
            tmpSt.borders.left = b.MEDIUM if tmpBoles[2] else b.THIN
            tmpSt.borders.right = b.MEDIUM if tmpBoles[3] else b.THIN

            self.bordersStyleMassive[1].append(tmpSt)
            self.bordersStyleMassive[0].append(tmpBoles)

        return tmpSt

    def makeOtzOtch(self,month,year):
        nums='0123456789'
        ListNames=[i.discName for i in self.pages]
        NEwwListNames=ListNames.copy()
        for i in range(len(NEwwListNames)):
            for j in nums:
                NEwwListNames[i]=NEwwListNames[i].replace(j,'')

        emptyL=[[] for n in self.groupList]
        predm=self.onlyMarkAll(month,year)

        optimizedNames=[]
        optimizedPredm=[]
        m=0
        for i in NEwwListNames:
            if i not in optimizedNames:
                optimizedNames.append(i)
                optimizedPredm.append(predm[m])
            else:
                optimizedNamesIndex=optimizedNames.index(i)
                for peple in range(len(optimizedPredm[optimizedNamesIndex])):
                    optimizedPredm[optimizedNamesIndex][peple].extend(predm[m][peple])
            m+=1

        predm=optimizedPredm
        for i in range(len(predm)):
            if predm[i]==emptyL:
                predm[i].clear()

        for i in range(len(predm)):
            if len(predm[i])!=0:
                max=0
                for j in predm[i]:
                    if len(j)>max:max=len(j)
                if max<3:max=3
                for j in range(len(predm[i])):
                    while len(predm[i][j])<max:
                        predm[i][j].append('')
        return predm,optimizedNames

    def saveaOtzOtchj(self,month,year):
        self.stylesDefenition()
        OtzOtch,optimizedNames=self.makeOtzOtch(month,year)
        monthName = self.monthList[month]

        maxFIOlen=2
        for i in self.groupList:
            if len(i)>maxFIOlen:maxFIOlen=len(i)
        v=f'Успеваемость за {monthName} {year} -  группа {self.groupname}'

        wb = xlwt.Workbook()
        ws = wb.add_sheet(f'отчёт {monthName}')
        totalLen=2
        for i in range(len(OtzOtch)):
            if len(OtzOtch[i])!=0:
                totalLen+=len(OtzOtch[i][0])
        ws.write_merge(0, 0, 0, totalLen, v, self.style0)

        ws.write_merge(1,1,0, 1, "ФИО", self.style0)
        #ws.write(row,coll,data,style)

        for i in range(len(self.groupList)):
            ws.write(i+2,0,i+1,self.retStyle(2,2+len(self.groupList)-1,0,1,i+2,0))
            ws.write(i+2,1,self.groupList[i],self.retStyle(2,2+len(self.groupList)-1,0,1,i+2,1))
        lastRow=2
        lastColl=2
        for i in range(len(OtzOtch)):
            if len(OtzOtch[i])!=0:
                thatColWidth=len(OtzOtch[i][0])
                lastPredmCol=lastColl
                ws.write_merge(1,1,lastPredmCol,lastPredmCol+thatColWidth-1,optimizedNames[i],self.style0)
                for h in range(len(OtzOtch[i])):
                    for b in range(len(OtzOtch[i][h])):
                        boxStyle=self.retStyle(lastRow,lastRow+len(self.groupList)-1,lastPredmCol,lastPredmCol+thatColWidth-1,lastRow+h,lastColl+b)
                        towr=int(OtzOtch[i][h][b]) if OtzOtch[i][h][b]!='' else OtzOtch[i][h][b]
                        ws.write(lastRow+h,lastColl+b,towr,boxStyle)
                lastColl+=thatColWidth

        ws.write(1,lastColl,"Сред.",self.style0)
        for i in range(len(self.groupList)):
            boxStyle=self.retStyle(lastRow,lastRow+len(self.groupList)-1,lastColl,lastColl,lastRow+i,lastColl)
            boxStyle.num_format_str="0.00"
            ws.write(lastRow+i,lastColl,
            xlwt.Formula(f"AVERAGE({rowcol_to_cell(2+i,2)}:{rowcol_to_cell(lastRow+i,lastColl-1)})"),
            boxStyle
            )
        boxStyle=self.retStyle(lastRow+len(self.groupList),lastRow+len(self.groupList),lastColl,lastColl,lastRow+len(self.groupList),lastColl)
        boxStyle.num_format_str="0.00"

        ws.write(lastRow+len(self.groupList),lastColl,
        xlwt.Formula(f"AVERAGE({rowcol_to_cell(2,lastColl)}:{rowcol_to_cell(lastRow+len(self.groupList)-1,lastColl)})"),
        boxStyle
        )
        ws.col(0).width =256*3
        ws.col(1).width =256*maxFIOlen
        for i in range(2,lastColl):
            ws.col(i).width = 256*3
        wb.save(f'{v}.xls')

    def saveNkiOtch(self,month,year,yBchac=None):
        self.stylesDefenition()
        mLen=monthrange(year, month)[1]
        monDays=[i for i in range(1,mLen+1)]
        monthName = self.monthList[month]
        maxFIOlen=2
        for i in self.groupList:
            if len(i)>maxFIOlen:maxFIOlen=len(i)
        v=f'Пропуски за {monthName} {year} -  группа {self.groupname}'

        wb = xlwt.Workbook()
        ws = wb.add_sheet(f'отчёт {monthName}')

        ws.write_merge(0, 0, 0, mLen+2+2, v, self.style0)

        ws.write_merge(1,1,0, 1, "ФИО", self.style0)
        #ws.write(row,coll,data,style)
        lastRow=2
        lastColl=2

        for i in range(len(self.groupList)):
            ws.write(i+2,0,i+1,self.retStyle(2,2+len(self.groupList)-1,0,1,i+2,0))
            ws.write(i+2,1,self.groupList[i],self.retStyle(2,2+len(self.groupList)-1,0,1,i+2,1))

        for i in range(mLen):
            ws.write(1,i+2,monDays[i],self.retStyle(1,1,2,2+mLen-1,1,i+2))

        for i in range(len(self.groupList)):
            toWrite=self.skiped(i,month,year)[1]
            for g in range(mLen):
                boxStyle=self.retStyle(2,2+len(self.groupList)-1,2,2+mLen-1,lastRow+i,lastColl+g)
                wr=toWrite[g]
                wr=None if wr==0 else wr
                ws.write(lastRow+i,lastColl+g,wr,boxStyle)

        ws.write(1,lastColl+mLen,"всего",self.style0)
        ws.write(1,lastColl+mLen+1,"уваж.",self.style0)
        ws.write(1,lastColl+mLen+2,"неуваж.",self.style0)
        allAll=[]
        if yBchac==None:
            yBchac=[0 for i in self.groupList]

        for i in range(len(self.groupList)):
            ws.write(lastRow+i,lastColl+mLen,
            xlwt.Formula(f"sum({rowcol_to_cell(2+i,2)}:{rowcol_to_cell(lastRow+i,lastColl+mLen-1)})"),
            self.retStyle(2,2+len(self.groupList)-1,lastColl+mLen,lastColl+mLen,lastRow+i,lastColl+mLen))

        ws.write(lastRow+len(self.groupList),lastColl+mLen,
        xlwt.Formula(f"sum({rowcol_to_cell(lastRow,lastColl+mLen)}:{rowcol_to_cell(lastRow+len(self.groupList)-1,lastColl+mLen)})"),
        self.retStyle(lastRow+len(self.groupList),lastRow+len(self.groupList),lastColl+mLen,lastColl+mLen,lastRow+len(self.groupList),lastColl+mLen)
        )

        for i in range(len(self.groupList)):
            ws.write(lastRow+i,lastColl+mLen+1,yBchac[i],self.retStyle(2,2+len(self.groupList)-1,lastColl+mLen+1,lastColl+mLen+1,lastRow+i,lastColl+mLen+1))
        ws.write(lastRow+len(self.groupList),lastColl+mLen+1,
        xlwt.Formula(f"sum({rowcol_to_cell(lastRow,lastColl+mLen+1)}:{rowcol_to_cell(lastRow+len(self.groupList)-1,lastColl+mLen+1)})"),
        self.retStyle(lastRow+len(self.groupList),lastRow+len(self.groupList),lastColl+mLen+1,lastColl+mLen+1,lastRow+len(self.groupList),lastColl+mLen+1)
        )

        for i in range(len(self.groupList)):
            ws.write(lastRow+i,lastColl+mLen+2,
            xlwt.Formula(f"{rowcol_to_cell(lastRow+i,lastColl+mLen)}-{rowcol_to_cell(lastRow+i,lastColl+mLen+1)})"),
            self.retStyle(2,2+len(self.groupList)-1,lastColl+mLen+2,lastColl+mLen+2,lastRow+i,lastColl+mLen+2))
        ws.write(lastRow+len(self.groupList),lastColl+mLen+2,
        xlwt.Formula(f"sum({rowcol_to_cell(lastRow,lastColl+mLen+2)}:{rowcol_to_cell(lastRow+len(self.groupList)-1,lastColl+mLen+2)})"),
        self.retStyle(lastRow+len(self.groupList),lastRow+len(self.groupList),lastColl+mLen+2,lastColl+mLen+2,lastRow+len(self.groupList),lastColl+mLen+2)
        )
        ws.col(0).width =256*3
        ws.col(1).width =256*maxFIOlen
        for i in range(2,mLen+2):
            ws.col(i).width = 256*3
        wb.save(f'{v}.xls')

if __name__ == '__main__':
    b=Journal.fromFile(module_dir/"23-П.Jornl")
    # b.pages[4].percent(20)
    print(b.groupList[0])
    # print(b.skiped(0,11,2021))
    b.saveaOtzOtchj(11,2021)
    b.saveNkiOtch(11,2021)
    # print(b.pages[0].onlyMark(7))
    # print(b.pages[0].average(7))
    # print(b.skiped(1,11,2021))
    # print(b.onlyMarkAll(11,2021))
    #IDGAF

max_stars_repo_name: RFfront/EduJornal

max_issues_repo_name: RFfront/EduJornal

max_forks_repo_name: RFfront/EduJornal


text:
import numpy as np
import pandas as pd
from abc import abstractmethod

from inferelator import utils

ISTS_COLUMN_NAME = 'isTs'
PREV_COLUMN_NAME = 'prevCol'
DELT_COLUMN_NAME = 'del.t'
COND_COLUMN_NAME = 'condName'

GROUP_COLUMN_NAME = "strain"
TIME_COLUMN_NAME = "time"

DEFAULT_STRICT_CHECKING_FOR_METADATA = False
DEFAULT_STRICT_CHECKING_FOR_DUPLICATES = True

class MetadataParser(object):

    cond_col = COND_COLUMN_NAME
    req_cols = tuple(COND_COLUMN_NAME)

    @classmethod
    @abstractmethod
    def process_groups(cls, meta_data):
        raise NotImplementedError

    @classmethod
    @abstractmethod
    def check_for_dupes(cls, exp_data, meta_data, steady_idx,
                        strict_checking_for_metadata=DEFAULT_STRICT_CHECKING_FOR_METADATA,
                        strict_checking_for_duplicates=DEFAULT_STRICT_CHECKING_FOR_DUPLICATES):
        raise NotImplementedError

    @classmethod
    @abstractmethod
    def validate_metadata(cls, exp_data, meta_data):
        raise NotImplementedError

    @classmethod
    @abstractmethod
    def create_default_meta_data(cls, metadata_index):
        raise NotImplementedError

    @classmethod
    def check_loaded_meta_data(cls, meta_data):
        if cls.cond_col in meta_data:
            meta_data.index = meta_data[cls.cond_col]
            meta_data.index.name = None
        return meta_data

    @classmethod
    def validate_metadata_columns(cls, meta_data):
        """
        Make sure metadata columns that are required for 'process_groups' exist
        :param meta_data: pd.DataFrame
        :return: True if the metadata is valid, False otherwise
        """
        cls.create_sample_name_column(meta_data)

        invalid_cols = pd.Index(cls.req_cols).difference(meta_data.columns)
        if len(invalid_cols) > 0:
            _msg = "Skipping metadata because parsing requires missing columns: {c}".format(c=invalid_cols)
            utils.Debug.vprint(_msg, level=0)
            return False
        else:
            return True

    @classmethod
    def create_sample_name_column(cls, meta_data):
        """
        Create a meta_data sample_name column from the index if necessary
        """
        if cls.cond_col not in meta_data:
            meta_data[cls.cond_col] = meta_data.index.astype(str)
        elif (meta_data[cls.cond_col] != meta_data.index.astype(str)).any():
            utils.Debug.vprint("Meta data sample name column and meta_data index are not equal", level=2)

    @staticmethod
    def fix_NAs(data_frame):
        """
        Replace the string NA with a np.nan
        :param data_frame: pd.DataFrame
        :return: pd.DataFrame
        """
        return data_frame.replace('NA', np.nan, regex=False)

class MetadataParserBranching(MetadataParser):
    """
    Metadata parser that handles prev_col & del_t metadata (which allows for Branching Time Courses)
    """

    ists_col = ISTS_COLUMN_NAME  # Column of booleans for "Is a time-series experiment"
    cond_col = COND_COLUMN_NAME  # Column of sample names (matching expression data column names)
    prev_col = PREV_COLUMN_NAME  # Column that identifies the previous timepoint sample name
    delt_col = DELT_COLUMN_NAME  # Column that identifies the delta time between this sample and the previous
    req_cols = (ISTS_COLUMN_NAME, COND_COLUMN_NAME, PREV_COLUMN_NAME, DELT_COLUMN_NAME)

    @classmethod
    def process_groups(cls, meta_data):
        """
        Parse the metadata to identify steady-state experiments and link timecourse experiments
        :param meta_data: pd.DataFrame
        :return steady_idx, ts_group: dict, dict
            steady_idx:  Dict keyed by condition, value is boolean (True if the condition is a steady-state experiment)
            ts_group: Dict keyed by condition. [(Previous_condition_name, Previous_delt),
                                                (Following_condition_name, Following_delt)]
        """
        time_series = meta_data[cls.ists_col].values.astype(bool)
        steadies = dict(zip(meta_data[cls.cond_col].astype(str), np.logical_not(time_series)))

        ts_data = meta_data[time_series].fillna(False)
        ts_dict = dict(zip(ts_data[cls.cond_col].astype(str).tolist(),
                           zip(ts_data[cls.prev_col].tolist(),
                               ts_data[cls.delt_col].tolist())))
        ts_group = {}
        for cond, (prev, delt) in ts_dict.items():
            if prev is False or delt is False:
                prev, delt = None, None
            try:
                op, od = ts_group[cond]
                ts_group[cond] = [(prev, delt), od]
            except KeyError:
                ts_group[cond] = [(prev, delt), (None, None)]
            if prev is not None:
                try:
                    op, od = ts_group[prev]
                    ts_group[prev] = [op, (cond, delt)]
                except KeyError:
                    ts_group[prev] = [(None, None), (cond, delt)]

        return steadies, ts_group

    @classmethod
    def check_for_dupes(cls, exp_data, meta_data, steady_idx,
                        strict_checking_for_metadata=DEFAULT_STRICT_CHECKING_FOR_METADATA,
                        strict_checking_for_duplicates=DEFAULT_STRICT_CHECKING_FOR_DUPLICATES):
        """
        Sanity check the metadata and experimental data to ensure that they can be linked. Try to guess as much as
        possible unless strict checking is on
        """
        # Check to make sure that the conditions in the expression data are matched with conditions in the metadata
        exp_conds = exp_data.columns.astype(str)
        meta_conds = meta_data[cls.cond_col].astype(str)

        # Check to find out if there are any conditions which don't have associated metadata
        # If there are, raise an exception if strict_checking_for_metadata is True
        # Otherwise just assume they're steady-state data and move on
        in_exp_not_meta = exp_conds.difference(meta_conds).tolist()
        if len(in_exp_not_meta) != 0:
            utils.Debug.vprint("{n} conditions cannot be properly matched to metadata".format(n=len(in_exp_not_meta)),
                               level=1)
            utils.Debug.vprint(" ".join(in_exp_not_meta), level=2)
            if strict_checking_for_metadata:
                raise ConditionDoesNotExistError("Conditions exist without associated metadata")
            else:
                for condition in in_exp_not_meta:
                    steady_idx[condition] = True

        # Check to find out if the conditions in the expression data are all unique
        # It's not a problem if they're not, we just assume the metadata applies to all conditions with the same name
        duplicate_in_exp = exp_conds.duplicated()
        n_dup_in_exp = np.sum(duplicate_in_exp)
        if n_dup_in_exp > 0:
            c_dup = exp_conds[duplicate_in_exp].tolist()
            utils.Debug.vprint("The expression data has {n} non-unique condition indexes".format(n=n_dup_in_exp),
                               level=1)
            utils.Debug.vprint(" ".join(c_dup), level=2)

        # Check to find out if the conditions in the meta data are all unique
        # If there are repeats, check and see if the associated information is identical (if it is, just ignore it)
        # If there are repeated conditions with different characteristics, the outcome may be unexpected
        # (The parser will just overwrite the first ones it comes to with the characteristics of the last one)
        duplicate_in_meta = meta_conds.duplicated()
        n_dup_in_meta = np.sum(duplicate_in_meta)
        if n_dup_in_meta > 0:
            meta_dup = meta_conds[duplicate_in_meta].tolist()
            if np.sum(np.logical_xor(meta_data.duplicated(), duplicate_in_meta)) > 0:
                utils.Debug.vprint("The metadata has non-unique conditions with different characteristics:", level=0)
                utils.Debug.vprint(" ".join(meta_dup), level=0)
                if strict_checking_for_duplicates:
                    raise MultipleConditionsError("Identical conditions have non-identical characteristics")
            else:
                utils.Debug.vprint("The metadata contains {n} duplicate rows".format(n=n_dup_in_meta), level=1)
                utils.Debug.vprint(" ".join(meta_dup), level=2)

        return steady_idx

    @classmethod
    def validate_metadata(cls, exp_data, meta_data):
        """
        Make sure that meta_data and expression_data are compatible
        """

        # Check and make sure that there's a name column and create it if needed
        cls.create_sample_name_column(meta_data)

        # Check the alignment of the expression data and the meta_data
        sample_names_expr = exp_data.columns.astype(str)
        sample_names_meta = meta_data[cls.cond_col].astype(str)
        align_count = len(sample_names_expr.intersection(sample_names_meta))

        if align_count == 0:
            raise ConditionDoesNotExistError("Unable to align metadata to expression data")
        elif align_count < min(exp_data.shape[1], meta_data.shape[0]):
            utils.Debug.vprint("Metadata ({me}) and expression data ({ex}) alignment off".format(me=meta_data.shape,
                                                                                ex=exp_data.shape),
                               level=0)

    @classmethod
    def create_sample_name_column(cls, meta_data):
        """
        Create a meta_data sample_name column from the index if necessary
        """
        if cls.cond_col not in meta_data:
            meta_data[cls.cond_col] = meta_data.index.astype(str)
        elif (meta_data[cls.cond_col] != meta_data.index.astype(str)).any():
            utils.Debug.vprint("Meta data sample name column and meta_data index are not equal", level=2)

    @classmethod
    def create_default_meta_data(cls, metadata_index):
        """
        Create a meta_data dataframe from basic defaults
        """

        # Create an empty dataframe with index equal to sample names from expression data
        meta_data = pd.DataFrame(index=metadata_index.astype(str))

        # Create a name column
        cls.create_sample_name_column(meta_data)

        # Assign default values to information columns
        meta_data[cls.ists_col] = True
        meta_data[cls.prev_col] = "NA"
        meta_data[cls.delt_col] = "NA"
        return meta_data

class MetadataParserNonbranching(MetadataParserBranching):
    group_col = GROUP_COLUMN_NAME
    time_col = TIME_COLUMN_NAME
    cond_col = COND_COLUMN_NAME
    req_cols = (GROUP_COLUMN_NAME, TIME_COLUMN_NAME, COND_COLUMN_NAME)

    @classmethod
    def process_groups(cls, meta_data):
        ts_dict = dict(zip(meta_data[cls.cond_col].astype(str).tolist(),
                           zip(meta_data[cls.group_col].tolist(),
                               meta_data[cls.time_col].tolist())))

        times_per_group = {k: [] for k in meta_data[cls.group_col].unique()}
        group_time_cond = {k: {} for k in meta_data[cls.group_col].unique()}
        ts_group = {cond: [(None, None), (None, None)] for cond in ts_dict.keys()}
        steady_idx = {cond: False for cond in ts_dict.keys()}

        # Walk through metadata to find the experiment times for each strain
        # And the sample IDs associated with each strain/time combo
        for cond, (group, time) in ts_dict.items():
            times_per_group[group].append(time)
            group_time_cond[group][time] = cond
            if np.isnan(time):
                steady_idx[cond] = True

        # Process into a dict, keyed by sample ID, of [(previous sample, del.t), (next sample, del.t)]
        for cond, (group, time) in ts_dict.items():
            group_times = times_per_group[group]

            if (min(group_times) == time and max(group_times) == time) or steady_idx[cond]:
                steady_idx[cond] = True
                ts_group.pop(cond)
                continue

            if min(group_times) == time:
                prev = (None, None)
            else:
                prev_time = max([i for (i, v) in zip(group_times, map(lambda x: x < time, group_times)) if v])
                prev_cond = group_time_cond[group][prev_time]
                prev = (prev_cond, time - prev_time)

            if max(group_times) == time:
                nex = (None, None)
            else:
                nex_time = min([i for (i, v) in zip(group_times, map(lambda x: x > time, group_times)) if v])
                nex_cond = group_time_cond[group][nex_time]
                nex = (nex_cond, nex_time - time)

            ts_group[cond] = [prev, nex]

        return steady_idx, ts_group

    @classmethod
    def create_default_meta_data(cls, metadata_index):
        """
        Create a meta_data dataframe from basic defaults
        """
        meta_data = pd.DataFrame(index=metadata_index.astype(str))
        meta_data[cls.cond_col] = metadata_index.astype(str)
        meta_data[cls.group_col] = list(range(len(metadata_index)))
        meta_data[cls.time_col] = 0
        return meta_data

class MetadataHandler(object):
    """
    This keeps track of how to process metadata
    """

    @classmethod
    def get_handler(cls, handler_ref):
        """
        This wrappers a metadata reference so that strings can be used instead of python imports
        Will either return a metadata handling class or will raise an error
        :param handler_ref: str / MetadataParser
            String or subclass of MetadataParser
        :return: MetadataParser
            The metadata parser that corresponds to the string, or the MetadataParser object will be passed through
        """
        if utils.is_string(handler_ref):
            if handler_ref == "branching":
                return MetadataParserBranching
            elif handler_ref == "nonbranching":
                return MetadataParserNonbranching
            else:
                raise ValueError("Parser {parser_str} unknown".format(parser_str=handler_ref))
        elif issubclass(handler_ref, MetadataParser):
            return handler_ref
        else:
            raise ValueError("Handler must be a string or a MetadataParser class")

class ConditionDoesNotExistError(IndexError):
    pass

class MultipleConditionsError(ValueError):
    pass

ext: py

max_stars_repo_name: Xparx/inferelator

max_issues_repo_name: Xparx/inferelator

max_forks_repo_name: Xparx/inferelator

reason: import numpy


text:
"""
This is the core file in the `gradio` package, and defines the Interface class, including methods for constructing the
interface using the input and output types.
"""

import copy
import csv
import getpass
import inspect
import markdown2
import numpy as np
import os
import pkg_resources
import requests
import random
import sys
import time
import warnings
import webbrowser
import weakref
from gradio import networking, strings, utils, encryptor, queue
from gradio.inputs import get_input_instance
from gradio.outputs import get_output_instance
from gradio.interpretation import quantify_difference_in_label, get_regression_or_classification_value
from gradio.external import load_interface, load_from_pipeline

class Interface:
    """
    Interfaces are created with Gradio by constructing a `gradio.Interface()` object or by calling `gradio.Interface.load()`.
    """

    instances = weakref.WeakSet()  # stores references to all currently existing Interface instances

    @classmethod
    def get_instances(cls):
        """
        :return: list of all current instances.
        """
        return list(Interface.instances)

    @classmethod
    def load(cls, name, src=None, api_key=None, alias=None, **kwargs):
        """
        Class method to construct an Interface from an external source repository, such as huggingface.
        Parameters:
        name (str): the name of the model (e.g. "gpt2"), can include the `src` as prefix (e.g. "huggingface/gpt2")
        src (str): the source of the model: `huggingface` or `gradio` (or empty if source is provided as a prefix in `name`)
        api_key (str): optional api key for use with Hugging Face Model Hub
        alias (str): optional, used as the name of the loaded model instead of the default name
        Returns:
        (gradio.Interface): a Gradio Interface object for the given model
        """
        interface_info = load_interface(name, src, api_key, alias)
         # create a dictionary of kwargs without overwriting the original interface_info dict because it is mutable
         # and that can cause some issues since the internal prediction function may rely on the original interface_info dict
        kwargs = dict(interface_info, **kwargs)
        interface = cls(**kwargs)
        interface.api_mode = True  # set api mode to true so that the interface will not preprocess/postprocess
        return interface

    @classmethod
    def from_pipeline(cls, pipeline, **kwargs):
        """
        Class method to construct an Interface from a Hugging Face transformers.Pipeline.
        pipeline (transformers.Pipeline):
        Returns:
        (gradio.Interface): a Gradio Interface object from the given Pipeline
        """
        interface_info = load_from_pipeline(pipeline)
        kwargs = dict(interface_info, **kwargs)
        interface = cls(**kwargs)
        return interface

    def __init__(self, fn, inputs=None, outputs=None, verbose=None, examples=None,
                 examples_per_page=10, live=False, layout="unaligned", show_input=True, show_output=True,
                 capture_session=None, interpretation=None, num_shap=2.0, theme=None, repeat_outputs_per_model=True,
                 title=None, description=None, article=None, thumbnail=None,
                 css=None, height=500, width=900, allow_screenshot=True, allow_flagging=None, flagging_options=None,
                 encrypt=False, show_tips=None, flagging_dir="flagged", analytics_enabled=None, enable_queue=None, api_mode=None):
        """
        Parameters:
        fn (Callable): the function to wrap an interface around.
        inputs (Union[str, List[Union[str, InputComponent]]]): a single Gradio input component, or list of Gradio input components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of input components should match the number of parameters in fn.
        outputs (Union[str, List[Union[str, OutputComponent]]]): a single Gradio output component, or list of Gradio output components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of output components should match the number of values returned by fn.
        verbose (bool): DEPRECATED. Whether to print detailed information during launch.
        examples (Union[List[List[Any]], str]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided. If there are multiple input components and a directory is provided, a log.csv file must be present in the directory to link corresponding inputs.
        examples_per_page (int): If examples are provided, how many to display per page.
        live (bool): whether the interface should automatically reload on change.
        layout (str): Layout of input and output panels. "horizontal" arranges them as two columns of equal height, "unaligned" arranges them as two columns of unequal height, and "vertical" arranges them vertically.
        capture_session (bool): DEPRECATED. If True, captures the default graph and session (needed for Tensorflow 1.x)
        interpretation (Union[Callable, str]): function that provides interpretation explaining prediction output. Pass "default" to use simple built-in interpreter, "shap" to use a built-in shapley-based interpreter, or your own custom interpretation function.
        num_shap (float): a multiplier that determines how many examples are computed for shap-based interpretation. Increasing this value will increase shap runtime, but improve results. Only applies if interpretation is "shap".
        title (str): a title for the interface; if provided, appears above the input and output components.
        description (str): a description for the interface; if provided, appears above the input and output components.
        article (str): an expanded article explaining the interface; if provided, appears below the input and output components. Accepts Markdown and HTML content.
        thumbnail (str): path to image or src to use as display picture for models listed in gradio.app/hub
        theme (str): Theme to use - one of "default", "huggingface", "grass", "peach". Add "dark" prefix, e.g. "darkpeach" or "darkdefault" for darktheme.
        css (str): custom css or path to custom css file to use with interface.
        allow_screenshot (bool): if False, users will not see a button to take a screenshot of the interface.
        allow_flagging (bool): if False, users will not see a button to flag an input and output.
        flagging_options (List[str]): if not None, provides options a user must select when flagging.
        encrypt (bool): If True, flagged data will be encrypted by key provided by creator at launch
        flagging_dir (str): what to name the dir where flagged data is stored.
        show_tips (bool): DEPRECATED. if True, will occasionally show tips about new Gradio features
        enable_queue (bool): DEPRECATED. if True, inference requests will be served through a queue instead of with parallel threads. Required for longer inference times (> 1min) to prevent timeout.
        api_mode (bool): DEPRECATED. If True, will skip preprocessing steps when the Interface is called() as a function (should remain False unless the Interface is loaded from an external repo)
        """
        if not isinstance(fn, list):
            fn = [fn]
        if not isinstance(inputs, list):
            inputs = [inputs]
        if not isinstance(outputs, list):
            outputs = [outputs]

        self.input_components = [get_input_instance(i) for i in inputs]
        self.output_components = [get_output_instance(o) for o in outputs]
        if repeat_outputs_per_model:
            self.output_components *= len(fn)

        if interpretation is None or isinstance(interpretation, list) or callable(interpretation):
            self.interpretation = interpretation
        elif isinstance(interpretation, str):
            self.interpretation = [interpretation.lower() for _ in self.input_components]
        else:
            raise ValueError("Invalid value for parameter: interpretation")

        self.predict = fn
        self.predict_durations = [[0, 0]] * len(fn)
        self.function_names = [func.__name__ for func in fn]
        self.__name__ = ", ".join(self.function_names)

        if verbose is not None:
            warnings.warn("The `verbose` parameter in the `Interface` is deprecated and has no effect.")

        self.status = "OFF"
        self.live = live
        self.layout = layout
        self.show_input = show_input
        self.show_output = show_output
        self.flag_hash = random.getrandbits(32)
        self.capture_session = capture_session

        if capture_session is not None:
            warnings.warn("The `capture_session` parameter in the `Interface` will be deprecated in the near future.")

        self.session = None
        self.title = title
        self.description = description
        if article is not None:
            article = utils.readme_to_html(article)
            article = markdown2.markdown(
                article, extras=["fenced-code-blocks"])

        self.article = article
        self.thumbnail = thumbnail
        theme = theme if theme is not None else os.getenv("GRADIO_THEME", "default")
        if theme not in ("default", "huggingface", "grass", "peach", "darkdefault", "darkhuggingface", "darkgrass", "darkpeach"):
            raise ValueError("Invalid theme name.")
        self.theme = theme
        self.height = height
        self.width = width
        if css is not None and os.path.exists(css):
            with open(css) as css_file:
                self.css = css_file.read()
        else:
            self.css = css
        if examples is None or isinstance(examples, str) or (isinstance(examples, list) and (len(examples) == 0 or isinstance(examples[0], list))):
            self.examples = examples
        elif isinstance(examples, list) and len(self.input_components) == 1:  # If there is only one input component, examples can be provided as a regular list instead of a list of lists
            self.examples = [[e] for e in examples]
        else:
            raise ValueError(
                "Examples argument must either be a directory or a nested list, where each sublist represents a set of inputs.")
        self.num_shap = num_shap
        self.examples_per_page = examples_per_page

        self.simple_server = None
        self.allow_screenshot = allow_screenshot
        # For allow_flagging and analytics_enabled: (1) first check for parameter, (2) check for environment variable, (3) default to True
        self.allow_flagging = allow_flagging if allow_flagging is not None else os.getenv("GRADIO_ALLOW_FLAGGING", "True")=="True"
        self.analytics_enabled = analytics_enabled if analytics_enabled is not None else os.getenv("GRADIO_ANALYTICS_ENABLED", "True")=="True"
        self.flagging_options = flagging_options
        self.flagging_dir = flagging_dir
        self.encrypt = encrypt
        self.save_to = None
        self.share = None
        self.share_url = None
        self.local_url = None
        self.ip_address = networking.get_local_ip_address()

        if show_tips is not None:
            warnings.warn("The `show_tips` parameter in the `Interface` is deprecated. Please use the `show_tips` parameter in `launch()` instead")

        self.requires_permissions = any(
            [component.requires_permissions for component in self.input_components])

        self.enable_queue = enable_queue
        if self.enable_queue is not None:
            warnings.warn("The `enable_queue` parameter in the `Interface` will be deprecated. Please use the `enable_queue` parameter in `launch()` instead")

        if api_mode is not None:
            warnings.warn("The `api_mode` parameter in the `Interface` is deprecated.")
        self.api_mode = False

        if self.capture_session:
            try:
                import tensorflow as tf
                self.session = tf.get_default_graph(), \
                    tf.keras.backend.get_session()
            except (ImportError, AttributeError):
                # If they are using TF >= 2.0 or don't have TF,
                # just ignore this.
                pass

        if self.allow_flagging:
            os.makedirs(self.flagging_dir, exist_ok=True)

        data = {'fn': fn,
                'inputs': inputs,
                'outputs': outputs,
                'live': live,
                'capture_session': capture_session,
                'ip_address': self.ip_address,
                'interpretation': interpretation,
                'allow_flagging': allow_flagging,
                'allow_screenshot': allow_screenshot,
                'custom_css': self.css is not None,
                'theme': self.theme
                }

        if self.analytics_enabled:
            utils.initiated_analytics(data)

        # Alert user if a more recent version of the library exists
        utils.version_check()
        Interface.instances.add(self)

    def __call__(self, *params):
        if self.api_mode:  # skip the preprocessing/postprocessing if sending to a remote API
            output = self.run_prediction(params, called_directly=True)
        else:
            output, _ = self.process(params)
        return output[0] if len(output) == 1 else output

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        repr = "Gradio Interface for: {}".format(
            ", ".join(fn.__name__ for fn in self.predict))
        repr += "\n" + "-"*len(repr)
        repr += "\ninputs:"
        for component in self.input_components:
            repr += "\n|-{}".format(str(component))
        repr += "\noutputs:"
        for component in self.output_components:
            repr += "\n|-{}".format(str(component))
        return repr

    def get_config_file(self):
        config = {
            "input_components": [
                iface.get_template_context()
                for iface in self.input_components],
            "output_components": [
                iface.get_template_context()
                for iface in self.output_components],
            "function_count": len(self.predict),
            "live": self.live,
            "examples_per_page": self.examples_per_page,
            "layout": self.layout,
            "show_input": self.show_input,
            "show_output": self.show_output,
            "title": self.title,
            "description": self.description,
            "article": self.article,
            "theme": self.theme,
            "css": self.css,
            "thumbnail": self.thumbnail,
            "allow_screenshot": self.allow_screenshot,
            "allow_flagging": self.allow_flagging,
            "flagging_options": self.flagging_options,
            "allow_interpretation": self.interpretation is not None,
            "queue": self.enable_queue,
            "version": pkg_resources.require("gradio")[0].version
        }
        try:
            param_names = inspect.getfullargspec(self.predict[0])[0]
            for iface, param in zip(config["input_components"], param_names):
                if not iface["label"]:
                    iface["label"] = param.replace("_", " ")
            for i, iface in enumerate(config["output_components"]):
                outputs_per_function = int(
                    len(self.output_components) / len(self.predict))
                function_index = i // outputs_per_function
                component_index = i - function_index * outputs_per_function
                ret_name = "Output " + \
                    str(component_index + 1) if outputs_per_function > 1 else "Output"
                if iface["label"] is None:
                    iface["label"] = ret_name
                if len(self.predict) > 1:
                    iface["label"] = self.function_names[function_index].replace(
                        "_", " ") + ": " + iface["label"]

        except ValueError:
            pass
        if self.examples is not None:
            if isinstance(self.examples, str):
                if not os.path.exists(self.examples):
                    raise FileNotFoundError(
                        "Could not find examples directory: " + self.examples)
                log_file = os.path.join(self.examples, "log.csv")
                if not os.path.exists(log_file):
                    if len(self.input_components) == 1:
                        examples = [[os.path.join(self.examples, item)]
                                    for item in os.listdir(self.examples)]
                    else:
                        raise FileNotFoundError(
                            "Could not find log file (required for multiple inputs): " + log_file)
                else:
                    with open(log_file) as logs:
                        examples = list(csv.reader(logs))
                        examples = examples[1:]  # remove header
                for i, example in enumerate(examples):
                    for j, (interface, cell) in enumerate(zip(self.input_components + self.output_components, example)):
                        examples[i][j] = interface.restore_flagged(cell)
                config["examples"] = examples
                config["examples_dir"] = self.examples
            else:
                config["examples"] = self.examples
        return config

    def run_prediction(self, processed_input, return_duration=False, called_directly=False):
        """
        This is the method that actually runs the prediction function with the given (processed) inputs.
        Parameters:
        processed_input (list): A list of processed inputs.
        return_duration (bool): Whether to return the duration of the prediction.
        called_directly (bool): Whether the prediction is being called directly (i.e. as a function, not through the GUI).
        Returns:
        predictions (list): A list of predictions (not post-processed).
        durations (list): A list of durations for each prediction (only if `return_duration` is True).
        """
        if self.api_mode:  # Serialize the input
            processed_input = [input_component.serialize(processed_input[i], called_directly)
                            for i, input_component in enumerate(self.input_components)]
        predictions = []
        durations = []
        output_component_counter = 0

        for predict_fn in self.predict:
            start = time.time()
            if self.capture_session and self.session is not None:
                graph, sess = self.session
                with graph.as_default(), sess.as_default():
                    prediction = predict_fn(*processed_input)
            else:
                try:
                    prediction = predict_fn(*processed_input)
                except ValueError as exception:
                    if str(exception).endswith("is not an element of this graph."):
                        raise ValueError(strings.en["TF1_ERROR"])
                    else:
                        raise exception
            duration = time.time() - start

            if len(self.output_components) == len(self.predict):
                prediction = [prediction]

            if self.api_mode:  # Serialize the input
                prediction_ = copy.deepcopy(prediction)
                prediction = []
                for pred in prediction_:  # Done this way to handle both single interfaces with multiple outputs and Parallel() interfaces
                    prediction.append(self.output_components[output_component_counter].deserialize(pred))
                    output_component_counter += 1

            durations.append(duration)
            predictions.extend(prediction)

        if return_duration:
            return predictions, durations
        else:
            return predictions

    def process(self, raw_input):
        """
        Parameters:
        raw_input: a list of raw inputs to process and apply the prediction(s) on.
        Returns:
        processed output: a list of processed  outputs to return as the prediction(s).
        duration: a list of time deltas measuring inference time for each prediction fn.
        """
        processed_input = [input_component.preprocess(raw_input[i])
                           for i, input_component in enumerate(self.input_components)]
        predictions, durations = self.run_prediction(
            processed_input, return_duration=True)
        processed_output = [output_component.postprocess(predictions[i]) if predictions[i] is not None else None
                            for i, output_component in enumerate(self.output_components)]
        return processed_output, durations

    def interpret(self, raw_input):
        """
        Runs the interpretation command for the machine learning model. Handles both the "default" out-of-the-box
        interpretation for a certain set of UI component types, as well as the custom interpretation case.
        Parameters:
        raw_input: a list of raw inputs to apply the interpretation(s) on.
        """
        if isinstance(self.interpretation, list):  # Either "default" or "shap"
            processed_input = [input_component.preprocess(raw_input[i])
                               for i, input_component in enumerate(self.input_components)]
            original_output = self.run_prediction(processed_input)
            scores, alternative_outputs = [], []
            for i, (x, interp) in enumerate(zip(raw_input, self.interpretation)):
                if interp == "default":
                    input_component = self.input_components[i]
                    neighbor_raw_input = list(raw_input)
                    if input_component.interpret_by_tokens:
                        tokens, neighbor_values, masks = input_component.tokenize(
                            x)
                        interface_scores = []
                        alternative_output = []
                        for neighbor_input in neighbor_values:
                            neighbor_raw_input[i] = neighbor_input
                            processed_neighbor_input = [input_component.preprocess(neighbor_raw_input[i])
                                                        for i, input_component in enumerate(self.input_components)]
                            neighbor_output = self.run_prediction(
                                processed_neighbor_input)
                            processed_neighbor_output = [output_component.postprocess(
                                neighbor_output[i]) for i, output_component in enumerate(self.output_components)]

                            alternative_output.append(
                                processed_neighbor_output)
                            interface_scores.append(quantify_difference_in_label(
                                self, original_output, neighbor_output))
                        alternative_outputs.append(alternative_output)
                        scores.append(
                            input_component.get_interpretation_scores(
                                raw_input[i], neighbor_values, interface_scores, masks=masks, tokens=tokens))
                    else:
                        neighbor_values, interpret_kwargs = input_component.get_interpretation_neighbors(
                            x)
                        interface_scores = []
                        alternative_output = []
                        for neighbor_input in neighbor_values:
                            neighbor_raw_input[i] = neighbor_input
                            processed_neighbor_input = [input_component.preprocess(neighbor_raw_input[i])
                                                        for i, input_component in enumerate(self.input_components)]
                            neighbor_output = self.run_prediction(
                                processed_neighbor_input)
                            processed_neighbor_output = [output_component.postprocess(
                                neighbor_output[i]) for i, output_component in enumerate(self.output_components)]

                            alternative_output.append(
                                processed_neighbor_output)
                            interface_scores.append(quantify_difference_in_label(
                                self, original_output, neighbor_output))
                        alternative_outputs.append(alternative_output)
                        interface_scores = [-score for score in interface_scores]
                        scores.append(
                            input_component.get_interpretation_scores(
                                raw_input[i], neighbor_values, interface_scores, **interpret_kwargs))
                elif interp == "shap" or interp == "shapley":
                    try:
                        import shap
                    except (ImportError, ModuleNotFoundError):
                        raise ValueError(
                            "The package `shap` is required for this interpretation method. Try: `pip install shap`")
                    input_component = self.input_components[i]
                    if not(input_component.interpret_by_tokens):
                        raise ValueError(
                            "Input component {} does not support `shap` interpretation".format(input_component))

                    tokens, _, masks = input_component.tokenize(x)

                    # construct a masked version of the input
                    def get_masked_prediction(binary_mask):
                        masked_xs = input_component.get_masked_inputs(
                            tokens, binary_mask)
                        preds = []
                        for masked_x in masked_xs:
                            processed_masked_input = copy.deepcopy(
                                processed_input)
                            processed_masked_input[i] = input_component.preprocess(
                                masked_x)
                            new_output = self.run_prediction(
                                processed_masked_input)
                            pred = get_regression_or_classification_value(
                                self, original_output, new_output)
                            preds.append(pred)
                        return np.array(preds)

                    num_total_segments = len(tokens)
                    explainer = shap.KernelExplainer(
                        get_masked_prediction, np.zeros((1, num_total_segments)))
                    shap_values = explainer.shap_values(np.ones((1, num_total_segments)), nsamples=int(
                        self.num_shap*num_total_segments), silent=True)
                    scores.append(input_component.get_interpretation_scores(
                        raw_input[i], None, shap_values[0], masks=masks, tokens=tokens))
                    alternative_outputs.append([])
                elif interp is None:
                    scores.append(None)
                    alternative_outputs.append([])
                else:
                    raise ValueError(
                        "Uknown intepretation method: {}".format(interp))
            return scores, alternative_outputs
        else:  # custom interpretation function
            processed_input = [input_component.preprocess(raw_input[i])
                               for i, input_component in enumerate(self.input_components)]
            interpreter = self.interpretation

            if self.capture_session and self.session is not None:
                graph, sess = self.session
                with graph.as_default(), sess.as_default():
                    interpretation = interpreter(*processed_input)
            else:
                try:
                    interpretation = interpreter(*processed_input)
                except ValueError as exception:
                    if str(exception).endswith("is not an element of this graph."):
                        raise ValueError(strings.en["TF1_ERROR"])
                    else:
                        raise exception
            if len(raw_input) == 1:
                interpretation = [interpretation]
            return interpretation, []

    def run_until_interrupted(self, thread, path_to_local_server):
        try:
            while True:
                time.sleep(0.5)
        except (KeyboardInterrupt, OSError):
            print("Keyboard interruption in main thread... closing server.")
            thread.keep_running = False
            # Hit the server one more time to close it
            networking.url_ok(path_to_local_server)
            if self.enable_queue:
                queue.close()

    def test_launch(self):
        for predict_fn in self.predict:
            print("Test launch: {}()...".format(predict_fn.__name__), end=' ')

            raw_input = []
            for input_component in self.input_components:
                if input_component.test_input is None:  # If no test input is defined for that input interface
                    print("SKIPPED")
                    break
                else:  # If a test input is defined for each interface object
                    raw_input.append(input_component.test_input)
            else:
                self.process(raw_input)
                print("PASSED")
                continue

    def launch(self, inline=None, inbrowser=None, share=False, debug=False,
               auth=None, auth_message=None, private_endpoint=None,
               prevent_thread_lock=False, show_error=True, server_name=None,
               server_port=None, show_tips=False, enable_queue=False):
        """
        Launches the webserver that serves the UI for the interface.
        Parameters:
        inline (bool): whether to display in the interface inline on python notebooks.
        inbrowser (bool): whether to automatically launch the interface in a new tab on the default browser.
        share (bool): whether to create a publicly shareable link from your computer for the interface.
        debug (bool): if True, and the interface was launched from Google Colab, prints the errors in the cell output.
        auth (Callable, Union[Tuple[str, str], List[Tuple[str, str]]]): If provided, username and password (or list of username-password tuples) required to access interface. Can also provide function that takes username and password and returns True if valid login.
        auth_message (str): If provided, HTML message provided on login page.
        private_endpoint (str): If provided, the public URL of the interface will be this endpoint (should generally be unchanged).
        prevent_thread_lock (bool): If True, the interface will block the main thread while the server is running.
        show_error (bool): If True, any errors in the interface will be printed in the browser console log
        server_port (int): will start gradio app on this port (if available)
        server_name (str): to make app accessible on local network, set this to "0.0.0.0".
        show_tips (bool): if True, will occasionally show tips about new Gradio features
        enable_queue (bool): if True, inference requests will be served through a queue instead of with parallel threads. Required for longer inference times (> 1min) to prevent timeout.
        Returns:
        app (flask.Flask): Flask app object
        path_to_local_server (str): Locally accessible link
        share_url (str): Publicly accessible link (if share=True)
        """
        # Set up local flask server
        config = self.get_config_file()
        self.config = config
        if auth and not callable(auth) and not isinstance(auth[0], tuple) and not isinstance(auth[0], list):
            auth = [auth]
        self.auth = auth
        self.auth_message = auth_message
        self.show_tips = show_tips
        self.show_error = show_error

        # Request key for encryption
        if self.encrypt:
            self.encryption_key = encryptor.get_key(
                getpass.getpass("Enter key for encryption: "))

        # Store parameters
        if self.enable_queue is None:
            self.enable_queue = enable_queue

        # Launch local flask server
        server_port, path_to_local_server, app, thread, server = networking.start_server(
            self, server_name, server_port, self.auth)
        self.local_url = path_to_local_server
        self.server_port = server_port
        self.status = "RUNNING"
        self.server = server
        self.server_app = app
        self.server_thread = thread

        # Count number of launches
        utils.launch_counter()

        # If running in a colab or not able to access localhost, automatically create a shareable link
        is_colab = utils.colab_check()
        if is_colab or not(networking.url_ok(path_to_local_server)):
            share = True
            if is_colab:
                if debug:
                    print(strings.en["COLAB_DEBUG_TRUE"])
                else:
                    print(strings.en["COLAB_DEBUG_FALSE"])
        else:
            print(strings.en["RUNNING_LOCALLY"].format(path_to_local_server))
        if is_colab and self.requires_permissions:
            print(strings.en["MEDIA_PERMISSIONS_IN_COLAB"])

        if private_endpoint is not None:
            share = True
        # Set up shareable link
        self.share = share

        if share:
            try:
                share_url = networking.setup_tunnel(
                    server_port, private_endpoint)
                self.share_url = share_url
                print(strings.en["SHARE_LINK_DISPLAY"].format(share_url))
                if private_endpoint:
                    print(strings.en["PRIVATE_LINK_MESSAGE"])
                else:
                    print(strings.en["SHARE_LINK_MESSAGE"])
            except RuntimeError:
                if self.analytics_enabled:
                    utils.error_analytics("RuntimeError")
                share_url = None
        else:
            print(strings.en["PUBLIC_SHARE_TRUE"])
            share_url = None

        # Open a browser tab with the interface.
        if inbrowser:
            if share:
                webbrowser.open(share_url)
            else:
                webbrowser.open(path_to_local_server)

        # Check if running in a Python notebook in which case, display inline
        if inline is None:
            inline = utils.ipython_check()
        if inline:
            try:
                from IPython.display import IFrame, display
                # Embed the remote interface page if on google colab; otherwise, embed the local page.
                if share:
                    while not networking.url_ok(share_url):
                        time.sleep(1)
                    display(IFrame(share_url, width=self.width, height=self.height))
                else:
                    display(IFrame(path_to_local_server,
                            width=self.width, height=self.height))
            except ImportError:
                pass  # IPython is not available so does not print inline.

        data = {
            'launch_method': 'browser' if inbrowser else 'inline',
            'is_google_colab': is_colab,
            'is_sharing_on': share,
            'share_url': share_url,
            'ip_address': self.ip_address,
            'enable_queue': self.enable_queue,
            'show_tips': self.show_tips,
            'api_mode': self.api_mode,
            'server_name': server_name,
            'server_port': server_port,
        }
        if self.analytics_enabled:
            utils.launch_analytics(data)

        utils.show_tip(self)

        # Run server perpetually under certain circumstances
        if debug or int(os.getenv('GRADIO_DEBUG', 0)) == 1:
            while True:
                sys.stdout.flush()
                time.sleep(0.1)
        is_in_interactive_mode = bool(
            getattr(sys, 'ps1', sys.flags.interactive))
        if not prevent_thread_lock and not is_in_interactive_mode:
            self.run_until_interrupted(thread, path_to_local_server)

        return app, path_to_local_server, share_url

    def close(self, verbose=True):
        """
        Closes the Interface that was launched. This will close the server and free the port.
        """
        try:
            self.server.shutdown()
            self.server_thread.join()
            print("Closing server running on port: {}".format(self.server_port))
        except AttributeError: # can't close if not running
            pass
        except OSError: # sometimes OSError is thrown when shutting down
            pass

    def integrate(self, comet_ml=None, wandb=None, mlflow=None):
        """
        A catch-all method for integrating with other libraries. Should be run after launch()
        Parameters:
            comet_ml (Experiment): If a comet_ml Experiment object is provided, will integrate with the experiment and appear on Comet dashboard
            wandb (module): If the wandb module is provided, will integrate with it and appear on WandB dashboard
            mlflow (module): If the mlflow module  is provided, will integrate with the experiment and appear on ML Flow dashboard
        """
        analytics_integration = ""
        if comet_ml is not None:
            analytics_integration = "CometML"
            comet_ml.log_other("Created from", "Gradio")
            if self.share_url is not None:
                comet_ml.log_text("gradio: " + self.share_url)
                comet_ml.end()
            else:
                comet_ml.log_text("gradio: " + self.local_url)
                comet_ml.end()
        if wandb is not None:
            analytics_integration = "WandB"
            if self.share_url is not None:
                wandb.log({"Gradio panel": wandb.Html('<iframe src="' + self.share_url + '" width="' +
                          str(self.width) + '" height="' + str(self.height) + '" frameBorder="0"></iframe>')})
            else:
                print(
                    "The WandB integration requires you to `launch(share=True)` first.")
        if mlflow is not None:
            analytics_integration = "MLFlow"
            if self.share_url is not None:
                mlflow.log_param("Gradio Interface Share Link",
                                 self.share_url)
            else:
                mlflow.log_param("Gradio Interface Local Link",
                                 self.local_url)
        if self.analytics_enabled and analytics_integration:
                data = {'integration': analytics_integration}
                utils.integration_analytics(data)

def close_all(verbose=True):
    # Tries to close all running interfaces, but method is a little flaky.
    for io in Interface.get_instances():
        io.close(verbose)

def reset_all():
    warnings.warn("The `reset_all()` method has been renamed to `close_all()` "
    "and will be deprecated. Please use `close_all()` instead.")
    close_all()

max_stars_repo_name: Un-index/gradio

max_issues_repo_name: Un-index/gradio

max_forks_repo_name: Un-index/gradio


text:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import csv
import numpy as np
import sklearn.utils as su
import sklearn.ensemble as se
import sklearn.metrics as sm
import matplotlib.pyplot as mp
with open('../../data/bike_day.csv', 'r') as f:
    reader = csv.reader(f)
    x, y = [], []
    for row in reader:
        x.append(row[2:13])
        y.append(row[-1])
fn_dy = np.array(x[0])
x = np.array(x[1:], dtype=float)
y = np.array(y[1:], dtype=float)
x, y = su.shuffle(x, y, random_state=7)
train_size = int(len(x) * 0.9)
train_x, test_x, train_y, test_y = \
    x[:train_size], x[train_size:], \
    y[:train_size], y[train_size:]
model = se.RandomForestRegressor(
    max_depth=10, n_estimators=1000,
    min_samples_split=2)
model.fit(train_x, train_y)
fi_dy = model.feature_importances_
pred_test_y = model.predict(test_x)
print(sm.r2_score(test_y, pred_test_y))
with open('../../data/bike_hour.csv', 'r') as f:
    reader = csv.reader(f)
    x, y = [], []
    for row in reader:
        x.append(row[2:14])
        y.append(row[-1])
fn_hr = np.array(x[0])
x = np.array(x[1:], dtype=float)
y = np.array(y[1:], dtype=float)
x, y = su.shuffle(x, y, random_state=7)
train_size = int(len(x) * 0.9)
train_x, test_x, train_y, test_y = \
    x[:train_size], x[train_size:], \
    y[:train_size], y[train_size:]
model = se.RandomForestRegressor(
    max_depth=10, n_estimators=1000,
    min_samples_split=2)
model.fit(train_x, train_y)
fi_hr = model.feature_importances_
pred_test_y = model.predict(test_x)
print(sm.r2_score(test_y, pred_test_y))
mp.figure('Bike', facecolor='lightgray')
mp.subplot(211)
mp.title('Day', fontsize=16)
mp.ylabel('Importance', fontsize=12)
mp.tick_params(labelsize=10)
mp.grid(axis='y', linestyle=':')
sorted_indices = fi_dy.argsort()[::-1]
pos = np.arange(len(sorted_indices))
mp.bar(pos, fi_dy[sorted_indices],
       facecolor='deepskyblue', edgecolor='steelblue')
mp.xticks(pos, fn_dy[sorted_indices], rotation=30)
mp.subplot(212)
mp.title('Hour', fontsize=16)
mp.xlabel('Feature', fontsize=12)
mp.ylabel('Importance', fontsize=12)
mp.tick_params(labelsize=10)
mp.grid(axis='y', linestyle=':')
sorted_indices = fi_hr.argsort()[::-1]
pos = np.arange(len(sorted_indices))
mp.bar(pos, fi_hr[sorted_indices],
       facecolor='lightcoral', edgecolor='indianred')
mp.xticks(pos, fn_hr[sorted_indices], rotation=30)
mp.tight_layout()
mp.show()

max_stars_repo_name: mheanng/PythonNote

max_issues_repo_name: mheanng/PythonNote

max_forks_repo_name: mheanng/PythonNote


text:
import pandas as pd
import glob
import os
from datetime import date
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

sns.set_theme(style="ticks")

#---------------------------------------------
#            DATOS DE VACUNAS
#---------------------------------------------

#Import datasets
path = r'data' # use your path
all_files = glob.glob(os.path.join(path, "*.csv"))

#Join datasets in unique dataframe
df = pd.concat((pd.read_csv(f) for f in all_files), ignore_index=True)

#Obtain difference in values
df["descarga"] = pd.to_datetime(df["descarga"], format="%d-%b-%Y (%H:%M:%S)")
df             = df.sort_values("descarga", ascending=True, na_position = "last")
df['diff']     = df.groupby(['Estado'])['vacunados'].diff().fillna(0)

#Get the initial values
df_initial     = df[df["descarga"] == min(df["descarga"])]
df_last        = df[df["descarga"] == max(df["descarga"])]

#Get the subsequent values that haven't changed
unique_downloads = pd.unique(df[df["diff"] != 0]["descarga"])
df_subsequent    = df[df["descarga"].isin(unique_downloads)]

df_final = pd.concat([df_initial, df_subsequent],  ignore_index=True)
df_final = df_final.sort_values("descarga", ascending=True, na_position = "last")

#---------------------------------------------
#            DATOS DE POBLACIÓN INEGI
#---------------------------------------------

#Lectura del archivo
individuos = pd.read_csv("inegi/INEGI_Exporta_20210528154428.csv", encoding='latin1')
edades     = individuos.columns[2:20]

#Limpiamos las commas
for col in edades:
    individuos[col] = individuos[col].str.replace(",", "").astype(int)

#Sumamos las edades de interés
individuos["Poblacion_INEGI"] = individuos[edades].sum(axis = 1)

#Cambio de los nombres para homologar
individuos.loc[individuos["Estado"] == "Veracruz de Ignacio de la Llave", ["Estado"]] = "Veracruz"
individuos.loc[individuos["Estado"] == "Michoacán de Ocampo", ["Estado"]] = "Michoacán"
individuos.loc[individuos["Estado"] == "Ciudad de México", ["Estado"]] = "CDMX"
individuos.loc[individuos["Estado"] == "Coahuila de Zaragoza", ["Estado"]] = "Coahuila"
individuos.loc[individuos["Estado"] == "México", ["Estado"]] = "Estado de México"
individuos = individuos[["Estado","Poblacion_INEGI"]]

#Juntamos ambas bases
dosis_per_capita = pd.merge(df_final, individuos, on = "Estado")
dosis_per_capita["dosis_per_capita"] = dosis_per_capita["vacunados"] / dosis_per_capita["Poblacion_INEGI"]

#Renombrar columna vacunados por dosis
dosis_per_capita.rename(columns={'vacunados':'dosis'}, inplace=True)
dosis_per_capita = dosis_per_capita.drop(columns=['diff'])
dosis_per_capita = dosis_per_capita.sort_values(by = ["descarga","Estado"], ascending=True, na_position = "last")

#Guardar en csv
download_name           = "Vacunados-" + date.today().strftime('%Y-%m-%d') + ".csv"
dosis_per_capita["Dia"] = dosis_per_capita.descarga.dt.date
dosis_per_capita.to_csv(os.path.join("processed", download_name), index = False)

max_stars_repo_name: RodrigoZepeda/VacunadosMX

max_issues_repo_name: RodrigoZepeda/VacunadosMX

max_forks_repo_name: RodrigoZepeda/VacunadosMX


text:
#!/usr/bin/env python

# Copyright (c) 2020 NVIDIA Corporation. All rights reserved.
# This work is licensed under the NVIDIA Source Code License - Non-commercial. Full
# text can be found in LICENSE.md

"""Test UCN on ros images"""

import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.utils.data
import tf
import rosnode
import message_filters
import cv2
import torch.nn as nn
import threading
import argparse
import pprint
import time, os, sys
import os.path as osp
import numpy as np
import _init_paths
import networks
import rospy
import copy

from sensor_msgs.msg import Image, CameraInfo
from cv_bridge import CvBridge, CvBridgeError
from fcn.config import cfg, cfg_from_file, get_output_dir
from fcn.test_dataset import test_sample
from utils.mask import visualize_segmentation
lock = threading.Lock()

def compute_xyz(depth_img, fx, fy, px, py, height, width):
    indices = np.indices((height, width), dtype=np.float32).transpose(1,2,0)
    z_e = depth_img
    x_e = (indices[..., 1] - px) * z_e / fx
    y_e = (indices[..., 0] - py) * z_e / fy
    xyz_img = np.stack([x_e, y_e, z_e], axis=-1) # Shape: [H x W x 3]
    return xyz_img

class ImageListener:

    def __init__(self, network, network_crop):

        self.network = network
        self.network_crop = network_crop
        self.cv_bridge = CvBridge()

        self.im = None
        self.depth = None
        self.rgb_frame_id = None
        self.rgb_frame_stamp = None

        # initialize a node
        rospy.init_node("seg_rgb")
        self.label_pub = rospy.Publisher('seg_label', Image, queue_size=10)
        self.label_refined_pub = rospy.Publisher('seg_label_refined', Image, queue_size=10)
        self.image_pub = rospy.Publisher('seg_image', Image, queue_size=10)
        self.image_refined_pub = rospy.Publisher('seg_image_refined', Image, queue_size=10)
        self.feature_pub = rospy.Publisher('seg_feature', Image, queue_size=10)

        if cfg.TEST.ROS_CAMERA == 'D415':
            # use RealSense D435
            self.base_frame = 'measured/base_link'
            rgb_sub = message_filters.Subscriber('/camera/color/image_raw', Image, queue_size=10)
            depth_sub = message_filters.Subscriber('/camera/aligned_depth_to_color/image_raw', Image, queue_size=10)
            msg = rospy.wait_for_message('/camera/color/camera_info', CameraInfo)
            self.camera_frame = 'measured/camera_color_optical_frame'
            self.target_frame = self.base_frame
        elif cfg.TEST.ROS_CAMERA == 'Azure':
            self.base_frame = 'measured/base_link'
            rgb_sub = message_filters.Subscriber('/k4a/rgb/image_raw', Image, queue_size=10)
            depth_sub = message_filters.Subscriber('/k4a/depth_to_rgb/image_raw', Image, queue_size=10)
            msg = rospy.wait_for_message('/k4a/rgb/camera_info', CameraInfo)
            self.camera_frame = 'rgb_camera_link'
            self.target_frame = self.base_frame
        else:
            # use kinect
            self.base_frame = '%s_rgb_optical_frame' % (cfg.TEST.ROS_CAMERA)
            rgb_sub = message_filters.Subscriber('/%s/rgb/image_color' % (cfg.TEST.ROS_CAMERA), Image, queue_size=10)
            depth_sub = message_filters.Subscriber('/%s/depth_registered/image' % (cfg.TEST.ROS_CAMERA), Image, queue_size=10)
            msg = rospy.wait_for_message('/%s/rgb/camera_info' % (cfg.TEST.ROS_CAMERA), CameraInfo)
            self.camera_frame = '%s_rgb_optical_frame' % (cfg.TEST.ROS_CAMERA)
            self.target_frame = self.base_frame

        # update camera intrinsics
        intrinsics = np.array(msg.K).reshape(3, 3)
        self.fx = intrinsics[0, 0]
        self.fy = intrinsics[1, 1]
        self.px = intrinsics[0, 2]
        self.py = intrinsics[1, 2]
        print(intrinsics)

        queue_size = 1
        slop_seconds = 0.1
        ts = message_filters.ApproximateTimeSynchronizer([rgb_sub, depth_sub], queue_size, slop_seconds)
        ts.registerCallback(self.callback_rgbd)

    def callback_rgbd(self, rgb, depth):

        if depth.encoding == '32FC1':
            depth_cv = self.cv_bridge.imgmsg_to_cv2(depth)
        elif depth.encoding == '16UC1':
            depth_cv = self.cv_bridge.imgmsg_to_cv2(depth).copy().astype(np.float32)
            depth_cv /= 1000.0
        else:
            rospy.logerr_throttle(
                1, 'Unsupported depth type. Expected 16UC1 or 32FC1, got {}'.format(
                    depth.encoding))
            return

        im = self.cv_bridge.imgmsg_to_cv2(rgb, 'bgr8')

        # rescale image if necessary
        if cfg.TEST.SCALES_BASE[0] != 1:
            im_scale = cfg.TEST.SCALES_BASE[0]
            im = pad_im(cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR), 16)
            depth_cv = pad_im(cv2.resize(depth_cv, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_NEAREST), 16)

        with lock:
            self.im = im.copy()
            self.depth = depth_cv.copy()
            self.rgb_frame_id = rgb.header.frame_id
            self.rgb_frame_stamp = rgb.header.stamp

    def run_network(self):

        with lock:
            if listener.im is None:
              return
            im_color = self.im.copy()
            depth_img = self.depth.copy()
            rgb_frame_id = self.rgb_frame_id
            rgb_frame_stamp = self.rgb_frame_stamp

        print('===========================================')

        # bgr image
        im = im_color.astype(np.float32)
        im_tensor = torch.from_numpy(im) / 255.0
        pixel_mean = torch.tensor(cfg.PIXEL_MEANS / 255.0).float()
        im_tensor -= pixel_mean
        image_blob = im_tensor.permute(2, 0, 1)
        sample = {'image_color': image_blob.unsqueeze(0)}

        if cfg.INPUT == 'DEPTH' or cfg.INPUT == 'RGBD':
            height = im_color.shape[0]
            width = im_color.shape[1]
            xyz_img = compute_xyz(depth_img, self.fx, self.fy, self.px, self.py, height, width)
            depth_blob = torch.from_numpy(xyz_img).permute(2, 0, 1)
            sample['depth'] = depth_blob.unsqueeze(0)

        out_label, out_label_refined = test_sample(sample, self.network, self.network_crop)

        # publish segmentation mask
        label = out_label[0].cpu().numpy()
        label_msg = self.cv_bridge.cv2_to_imgmsg(label.astype(np.uint8))
        label_msg.header.stamp = rgb_frame_stamp
        label_msg.header.frame_id = rgb_frame_id
        label_msg.encoding = 'mono8'
        self.label_pub.publish(label_msg)

        num_object = len(np.unique(label)) - 1
        print('%d objects' % (num_object))

        if out_label_refined is not None:
            label_refined = out_label_refined[0].cpu().numpy()
            label_msg_refined = self.cv_bridge.cv2_to_imgmsg(label_refined.astype(np.uint8))
            label_msg_refined.header.stamp = rgb_frame_stamp
            label_msg_refined.header.frame_id = rgb_frame_id
            label_msg_refined.encoding = 'mono8'
            self.label_refined_pub.publish(label_msg_refined)

        # publish segmentation images
        im_label = visualize_segmentation(im_color[:, :, (2, 1, 0)], label, return_rgb=True)
        rgb_msg = self.cv_bridge.cv2_to_imgmsg(im_label, 'rgb8')
        rgb_msg.header.stamp = rgb_frame_stamp
        rgb_msg.header.frame_id = rgb_frame_id
        self.image_pub.publish(rgb_msg)

        if out_label_refined is not None:
            im_label_refined = visualize_segmentation(im_color[:, :, (2, 1, 0)], label_refined, return_rgb=True)
            rgb_msg_refined = self.cv_bridge.cv2_to_imgmsg(im_label_refined, 'rgb8')
            rgb_msg_refined.header.stamp = rgb_frame_stamp
            rgb_msg_refined.header.frame_id = rgb_frame_id
            self.image_refined_pub.publish(rgb_msg_refined)

def parse_args():
    """
    Parse input arguments
    """
    parser = argparse.ArgumentParser(description='Test a PoseCNN network')
    parser.add_argument('--gpu', dest='gpu_id', help='GPU id to use',
                        default=0, type=int)
    parser.add_argument('--instance', dest='instance_id', help='PoseCNN instance id to use',
                        default=0, type=int)
    parser.add_argument('--pretrained', dest='pretrained',
                        help='initialize with pretrained checkpoint',
                        default=None, type=str)
    parser.add_argument('--pretrained_crop', dest='pretrained_crop',
                        help='initialize with pretrained checkpoint for crops',
                        default=None, type=str)
    parser.add_argument('--cfg', dest='cfg_file',
                        help='optional config file', default=None, type=str)
    parser.add_argument('--dataset', dest='dataset_name',
                        help='dataset to train on',
                        default='shapenet_scene_train', type=str)
    parser.add_argument('--rand', dest='randomize',
                        help='randomize (do not use a fixed seed)',
                        action='store_true')
    parser.add_argument('--network', dest='network_name',
                        help='name of the network',
                        default=None, type=str)
    parser.add_argument('--background', dest='background_name',
                        help='name of the background file',
                        default=None, type=str)

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)

    print('Using config:')
    pprint.pprint(cfg)

    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(cfg.RNG_SEED)

    # device
    cfg.gpu_id = 0
    cfg.device = torch.device('cuda:{:d}'.format(cfg.gpu_id))
    cfg.instance_id = args.instance_id
    num_classes = 2
    cfg.MODE = 'TEST'
    cfg.TEST.VISUALIZE = False
    print('GPU device {:d}'.format(args.gpu_id))

    # prepare network
    if args.pretrained:
        network_data = torch.load(args.pretrained)
        print("=> using pre-trained network '{}'".format(args.pretrained))
    else:
        network_data = None
        print("no pretrained network specified")
        sys.exit()

    network = networks.__dict__[args.network_name](num_classes, cfg.TRAIN.NUM_UNITS, network_data).cuda(device=cfg.device)
    network = torch.nn.DataParallel(network, device_ids=[0]).cuda(device=cfg.device)
    cudnn.benchmark = True
    network.eval()

    if args.pretrained_crop:
        network_data_crop = torch.load(args.pretrained_crop)
        network_crop = networks.__dict__[args.network_name](num_classes, cfg.TRAIN.NUM_UNITS, network_data_crop).cuda(device=cfg.device)
        network_crop = torch.nn.DataParallel(network_crop, device_ids=[cfg.gpu_id]).cuda(device=cfg.device)
        network_crop.eval()
    else:
        network_crop = None

    # image listener
    listener = ImageListener(network, network_crop)
    while not rospy.is_shutdown():
       listener.run_network()

max_stars_repo_name: LeiYangJustin/UnseenObjectClustering

max_issues_repo_name: LeiYangJustin/UnseenObjectClustering

max_forks_repo_name: LeiYangJustin/UnseenObjectClustering


text:
import numpy as np

from mushroom_rl.algorithms.value.dqn import AbstractDQN

class DQN(AbstractDQN):
    """
    Deep Q-Network algorithm.
    "Human-Level Control Through Deep Reinforcement Learning".
    Mnih V. et al.. 2015.

    """
    def _next_q(self, next_state, absorbing):
        q = self.target_approximator.predict(next_state, **self._predict_params)
        if np.any(absorbing):
            q *= 1 - absorbing.reshape(-1, 1)

        return np.max(q, axis=1)

class QT_OPT(AbstractDQN):
    """
    Deep Q-Network algorithm.
    "Human-Level Control Through Deep Reinforcement Learning".
    Mnih V. et al.. 2015.

    """
    def _next_q(self, next_state, absorbing):
        q = self.target_approximator.predict(next_state, **self._predict_params)
        if np.any(absorbing):
            q *= 1 - absorbing.reshape(-1, 1)

        return np.max(q, axis=1)

max_stars_repo_name: TheCamusean/mushroom-rl

max_issues_repo_name: TheCamusean/mushroom-rl

max_forks_repo_name: TheCamusean/mushroom-rl