def eliminate_ilr_s0(m,n): arr_non_term = elel.init_range(1,m+1,1) arr_non_term = elel.mapiv(arr_non_term,lambda i,v:"Aa"+str(i)) arr_term = elel.init_range(1,n+1,1) arr_term = elel.mapiv(arr_term,lambda i,v:"B"+str(i)) arr = arr_non_term + arr_term s = elel.join(arr,"|") s = "A -> " + s return(s)
def rplc_blk(ndarr, top, left, bot, right, blk): rowseqs = elel.init_range(top, bot + 1, 1) rows_ndarr = copy.deepcopy(ndarr[rowseqs, :]) colseqs = elel.init_range(left, right + 1, 1) #cols = blk.transpose() cols = blk rows_ndarr[:, colseqs] = cols ndarr[rowseqs, :] = rows_ndarr return (ndarr)
def _rmrow(df,rowname,*args,**kwargs): df = copy.deepcopy(df) rlocs = _rn2rlocs(rowname,**kwargs) if(args.__len__()==0): whiches = elel.init_range(0,rlocs.__len__(),1) else: whiches = list(args) rlocs = elel.select_seqs(rlocs,whiches) all_rlocs = elel.init_range(0,df.index.__len__(),1) lefted_rlocs = elel.select_seqs_not(all_rlocs,rlocs) return(df.iloc[lefted_rlocs])
def _rmcol(df,colname,*args,**kwargs): df = copy.deepcopy(df) clocs = _cn2clocs(colname,**kwargs) if(args.__len__()==0): whiches = elel.init_range(0,clocs.__len__(),1) else: whiches = list(args) clocs = elel.select_seqs(clocs,whiches) all_clocs = elel.init_range(0,df.columns.__len__(),1) lefted_clocs = elel.select_seqs_not(all_clocs,clocs) return(df.iloc[:,lefted_clocs])
def get_children(plnd): ''' 这一步会填充ele.pbreadth,因为get_children之后,父节点就脱离了,完全依赖pbreadth 这一步会填充ele.ftag,因为当前层的key(dict) seq(list,tuple) dummy_seq(set)会在这一层决定 这一步会填充ele.sibseq, 在兄弟中是第几个 这一步会填充ele.depth, 在兄弟中是第几个 这一步会填充ele.fpl, 在兄弟中是第几个 这一步会填充ele['type'] 这个spec info ''' fpl = plnd.ele.fpl #current fpl will be used as children pfpl jobj = plnd.jobj #current jobj if (isinstance(jobj, dict)): ftagl, _ = eded.d2kvlist(jobj) #get keys-list (ftagl) elif (isinstance(jobj, list) or isinstance(jobj, tuple) or isinstance(jobj, set)): ftagl = elel.init_range(0, len(jobj), 1) else: ftagl = [] #others all leaf-plnds ##### pfpl = fpl chfpls = elel.mapv(ftagl, plfunc.ppl2pl, [pfpl]) #get children-full-pathlist ####### pjobj = jobj pele = plnd.ele children = elel.mapiv( chfpls, init_plnd, [pele, pjobj] ) #get all-children,init_plnd-will-fill-pbreadth=ftag=sibseq,fill-parent-ele-children return (children)
def index2permutation(index, lngth, *args): ''' >>> index2permutation(62,5) [2, 3, 1, 0, 4] >>> >>> index2permutation(62,5,['a','b','c','d','e']) ['c', 'd', 'b', 'a', 'e'] >>> ''' rel_indexes = [] n = lngth - 1 while (index > 0): q = index // math.factorial(n) r = index % math.factorial(n) rel_indexes.append(q) n = n - 1 index = r if (len(args) == 0): arr = elel.init_range(0, lngth, 1) else: arr = args[0] rel_indexes = elel.padding(rel_indexes, lngth, 0) abs_indexes = _relindex2absindex(rel_indexes) rslt = elel.select_seqs(arr, abs_indexes) return (rslt)
def _repl_col(df,pos,*args,**kwargs): df = copy.deepcopy(df) if(isinstance(pos,int)): pos = pos + 1 else: clocs = _cn2clocs(pos,**kwargs) if('which' in kwargs): which = kwargs['which'] else: which = 0 pos = clocs[which] + 1 args = list(args) if(args.__len__() == 1): colname = list(args[0].keys())[0] values = list(args[0].values())[0] else: colname = args[0] if(isinstance(args[1],list)): values = args[1] else: values = args[1:] df.insert(pos,colname,values,kwargs['allow_duplicates']) pos = pos -1 all_clocs = elel.init_range(0,df.columns.__len__(),1) all_clocs.remove(pos) return(df.iloc[:,all_clocs])
def _relindex2absindex(indexes): ''' >>> _relindex2absindex([2,2,1,1,0]) [2, 3, 1, 4, 0] >>> [2, 3, 1, 4, 0] [2,2,1,1,0] 2 arr=[0,1,2,3,4] 2=arr[2] arr.pop(2) 3 arr=[0,1,3,4] 3=arr[2] arr.pop(2) 1 arr=[0,1,4] 1=arr[1] arr.pop(1) 4 arr=[0,4] 4=arr[1] arr.pop(1) 0 arr=[0] 0=arr[0] ''' lngth = len(indexes) arr = elel.init_range(0, lngth, 1) rslt = [] for i in range(lngth): ri = indexes[i] index = arr[ri] ai = arr.pop(ri) rslt.append(ai) return (rslt)
def _repl_cols(df,poses,*args,**kwargs): df = copy.deepcopy(df) args = list(args) if(isinstance(args[0],dict)): kl,vl = eded.d2kvlist(args[0]) else: if(isinstance(args[1],list)): kl = elel.select_evens(args) vl = elel.select_odds(args) else: kl,vl = elel.brkl2kvlist(args,df.index.__len__()+1) if(isinstance(poses[0],int)): pass else: colnames = poses clocs_array = [] for i in range(colnames.__len__()): clocs = _cn2clocs(colnames[i],**kwargs) clocs_array.append((clocs,i)) if("whiches" in kwargs): whiches = kwargs['whiches'] clocs_array = elel.mapv(clocs_array,lambda ele:ele[0]) clocs = elel.batexec(lambda clocs,which:clocs[which],clocs_array,whiches) poses = clocs else: #by default replace all nkl = [] nvl = [] nclocs = [] for i in range(clocs_array.__len__()): clocs = clocs_array[i][0] index = clocs_array[i][1] tmpkl = elel.init(clocs.__len__(),kl[i]) tmpvl = elel.init(clocs.__len__(),vl[i]) nkl = elel.concat(nkl,tmpkl) nvl = elel.concat(nvl,tmpvl) nclocs = elel.concat(nclocs,clocs) #batsort poses = nclocs kl,vl = elel.batsorted(nclocs,nkl,nvl) poses = elel.mapv(poses,lambda pos:pos+1) poses.sort() for i in range(0,poses.__len__()): pos = poses[i] df.insert(pos,kl[i],vl[i],kwargs['allow_duplicates']) pos = pos -1 all_clocs = elel.init_range(0,df.columns.__len__(),1) all_clocs.remove(pos) df = df.iloc[:,all_clocs] return(df)
def dtb2qtbl(dtb): ''' dtb2qtbl(dtb) qtbl >>> dtb2qtbl(dtb) one two three four 0 3 1 2 4 1 8 6 7 9 2 13 11 12 14 3 18 16 17 19 4 23 21 22 24 >>> ''' columns = get_cnl(dtb) index = elel.init_range(0,len(dtb),1) m = dtb2mat(dtb) qtbl = Qtable(mat=np.array(m),index=index,columns=columns) return(qtbl)
def _swaprow(df,rowname1,rowname2,*args,**kwargs): df = copy.deepcopy(df) rlocs1 = _rn2rlocs(rowname1,**kwargs) rlocs2 = _rn2rlocs(rowname2,**kwargs) args = list(args) if(args.__len__()==0): which1 = 0 which2 = 0 elif(args.__len__()==1): which1 = args[0] which2 = 0 else: which1 = args[0] which2 = args[1] rloc1 = rlocs1[which1] rloc2 = rlocs2[which2] rlocs = elel.init_range(0,df.columns.__len__(),1) rlocs = elel.iswap(rlocs,rloc1,rloc2) return(df.iloc[rlocs])
def _swapcol(df,colname1,colname2,*args,**kwargs): df = copy.deepcopy(df) clocs1 = _cn2clocs(colname1,**kwargs) clocs2 = _cn2clocs(colname2,**kwargs) args = list(args) if(args.__len__()==0): which1 = 0 which2 = 0 elif(args.__len__()==1): which1 = args[0] which2 = 0 else: which1 = args[0] which2 = args[1] cloc1 = clocs1[which1] cloc2 = clocs2[which2] clocs = elel.init_range(0,df.columns.__len__(),1) clocs = elel.iswap(clocs,cloc1,cloc2) return(df.iloc[:,clocs])
def _rmrows(df,*rownames,**kwargs): df = copy.deepcopy(df) rownames = list(rownames) if(isinstance(rownames[0],list)): rownames = rownames[0] else: pass rlocs_array = [] for i in range(rownames.__len__()): rlocs = _rn2rlocs(rownames[i],**kwargs) rlocs_array.append(rlocs) if("whiches" in kwargs): whiches = kwargs['whiches'] rlocs = elel.batexec(lambda rlocs,which:rlocs[which],rlocs_array,whiches) else: #by default remove all rlocs = elel.concat(*rlocs_array) all_rlocs = elel.init_range(0,df.index.__len__(),1) lefted_rlocs = elel.select_seqs_not(all_rlocs,rlocs) return(df.iloc[lefted_rlocs])
def _rmcols(df,*colnames,**kwargs): df = copy.deepcopy(df) colnames = list(colnames) if(isinstance(colnames[0],list)): colnames = colnames[0] else: pass clocs_array = [] for i in range(colnames.__len__()): clocs = _cn2clocs(colnames[i],**kwargs) clocs_array.append(clocs) if("whiches" in kwargs): whiches = kwargs['whiches'] clocs = elel.batexec(lambda clocs,which:clocs[which],clocs_array,whiches) else: #by default remove all clocs = elel.concat(*clocs_array) all_clocs = elel.init_range(0,df.columns.__len__(),1) lefted_clocs = elel.select_seqs_not(all_clocs,clocs) return(df.iloc[:,lefted_clocs])
def get_all_easciis(): return (elel.join(elel.mapv(elel.init_range(0, 256, 1), chr), ''))
def eliminate_ilr_s1(n): arr = elel.init_range(1,n+1,1) arr = elel.mapiv(arr,lambda i,v:"b"+str(i)+"A'") s = elel.join(arr,"|") s = "A -> " + s return(s)
def eliminate_ilr_s2(n): arr = elel.init_range(1,n+1,1) arr = elel.mapiv(arr,lambda i,v:"a"+str(i)+"A'") s = elel.join(arr,"|") s = "A' -> " + s + "|" + "epsilon" return(s)