def intersect(a, b): """Get the intersect set of set1 and set2. Parameters : a : sequence[n_a, ] or np.ndarray[n_a, ] : The first set. b : sequence[n_b, ] or np.ndarray[n_b, ] : The second set. Returns : intersect : np.ndarray[n_intersect, ] : The intersect of set a and set b. iA : nd.array[n_variables1, ] : a[iA] == intersect. iB : nd.array[n_variables2, ] : b[iB] == intersect. Raises : None Notes : intersect, iA, iB = intersect(a, b) """ a = np.array(a) b = np.array(b) intersect = np.intersect1d(b, a) dummy, iA, alliA = ismember(intersect, a) dummy, iB, alliB = ismember(intersect, b) return intersect, iA, iB
def setstate(pot, vars, state, val): #FIXME: data format needed to be unified #FIXME: works only for 1-D vars = np.array([vars]) state = np.array([state]) print type(vars) print "original vars:", vars print "input state:", state p = copy.copy(pot) a, tmp = ismember(vars, pot.variables) print "tmp=", tmp vars = vars[tmp] state = state[tmp] print "effective vars:", vars dum, iperm = ismember(vars, pot.variables) print "original vars item in pot:", pot.variables print "original table in pot", pot.table print "effective vars' index in pot:", iperm #FIXME: not consistent with former definition (need fn_size) nstates = pot.table.shape #FIXME: arbitrary setting nstates = np.array([2]) print "effective vars in pot NSTATES:", nstates #NOTE: use NAN as initial state in Python instead of 0. (Different from MATLAB) permstates = np.empty((1, np.size(nstates))) #FIXME: arbitrary setting permstates = np.empty(1) permstates[:] = np.nan print "initial effective vars states: \n", permstates permstates[iperm] = state print "set effective vars states: \n", permstates # set effective var-states to val print "permstates=", permstates print "permstates.all()=", permstates.all() # MATLAB: if all(permstates>0) % if the state is unique allcondition = np.logical_not(np.isnan(permstates)).all() print "allcondition=", allcondition if allcondition: # if the state is unique print "Before setstate: p.table= \n", p.table watch_ndx = np.asarray([subv2ind(nstates, permstates)]) watch_ndx = np.int8(watch_ndx) print "Callback watch_ndx = subv2ind(nstates,permstates)=", watch_ndx #FIXME: data format need unified #p.table[subv2ind(nstates,permstates)] = val #p.table = p.table.reshape(1,2) #print "reshaped p.table: \n", p.table print "val= ", val p.table[watch_ndx] = val #p.table = p.table.reshape(2,1) print "After setstate: p.table= \n", p.table #FIXME: not implemented ELSE case # else : # set all states that match the given substate to the given value # sub=find(permstates>0); # st=ind2subv(nstates,1:prod(nstates)); # p.table(all(st(:,sub)==repmat(permstates(sub),prod(nstates),1),2))=val; return p
def setstate(pot, vars, state, val): # FIXME: data format needed to be unified # FIXME: works only for 1-D vars = np.array([vars]) state = np.array([state]) print type(vars) print "original vars:", vars print "input state:", state p = copy.copy(pot) a, tmp = ismember(vars, pot.variables) print "tmp=", tmp vars = vars[tmp] state = state[tmp] print "effective vars:", vars dum, iperm = ismember(vars, pot.variables) print "original vars item in pot:", pot.variables print "original table in pot", pot.table print "effective vars' index in pot:", iperm # FIXME: not consistent with former definition (need fn_size) nstates = pot.table.shape # FIXME: arbitrary setting nstates = np.array([2]) print "effective vars in pot NSTATES:", nstates # NOTE: use NAN as initial state in Python instead of 0. (Different from MATLAB) permstates = np.empty((1, np.size(nstates))) # FIXME: arbitrary setting permstates = np.empty(1) permstates[:] = np.nan print "initial effective vars states: \n", permstates permstates[iperm] = state print "set effective vars states: \n", permstates # set effective var-states to val print "permstates=", permstates print "permstates.all()=", permstates.all() # MATLAB: if all(permstates>0) % if the state is unique allcondition = np.logical_not(np.isnan(permstates)).all() print "allcondition=", allcondition if allcondition: # if the state is unique print "Before setstate: p.table= \n", p.table watch_ndx = np.asarray([subv2ind(nstates, permstates)]) watch_ndx = np.int8(watch_ndx) print "Callback watch_ndx = subv2ind(nstates,permstates)=", watch_ndx # FIXME: data format need unified # p.table[subv2ind(nstates,permstates)] = val # p.table = p.table.reshape(1,2) # print "reshaped p.table: \n", p.table print "val= ", val p.table[watch_ndx] = val # p.table = p.table.reshape(2,1) print "After setstate: p.table= \n", p.table # FIXME: not implemented ELSE case # else : # set all states that match the given substate to the given value # sub=find(permstates>0); # st=ind2subv(nstates,1:prod(nstates)); # p.table(all(st(:,sub)==repmat(permstates(sub),prod(nstates),1),2))=val; return p
def compare_networks(adjmat_true, adjmat_pred, pos=None, showfig=True, width=15, height=8, verbose=3): # Make sure columns and indices to match [IArow,IBrow]=ismember(adjmat_true.index.values, adjmat_pred.index.values) [IAcol,IBcol]=ismember(adjmat_true.columns.values, adjmat_pred.columns.values) adjmat_true = adjmat_true.loc[IArow,IAcol] adjmat_pred = adjmat_pred.iloc[IBrow,IBcol] # Make sure it is boolean adjmat adjmat_true = adjmat_true>0 adjmat_pred = adjmat_pred>0 # Check whether order is correct assert np.all(adjmat_true.columns.values==adjmat_pred.columns.values), 'Column order of both input values could not be matched' assert np.all(adjmat_true.index.values==adjmat_pred.index.values), 'Row order of both input values could not be matched' # Get edges y_true = adjmat_true.stack().reset_index()[0].values y_pred = adjmat_pred.stack().reset_index()[0].values # Confusion matrix scores=confmatrix.twoclass(y_true, y_pred, threshold=0.5, classnames=['Disconnected','Connected'], title='', cmap=plt.cm.Blues, showfig=1, verbose=0) #bayes.plot(out_bayes['adjmat'], pos=G['pos']) # Setup graph adjmat_diff = adjmat_true.astype(int) adjmat_diff[(adjmat_true.astype(int) - adjmat_pred.astype(int))<0]=2 adjmat_diff[(adjmat_true.astype(int) - adjmat_pred.astype(int))>0]=-1 if showfig: # Setup graph # G_true = adjmat2graph(adjmat_true) G_diff = adjmat2graph(adjmat_diff) # Graph layout pos = graphlayout(G_diff, pos=pos, scale=1, layout='fruchterman_reingold') # Bootup figure plt.figure(figsize=(width,height)) # nodes nx.draw_networkx_nodes(G_diff, pos, node_size=700) # edges colors = [G_diff[u][v]['color'] for u,v in G_diff.edges()] #weights = [G_diff[u][v]['weight'] for u,v in G_diff.edges()] nx.draw_networkx_edges(G_diff, pos, arrowstyle='->', edge_color=colors, width=1) # Labels nx.draw_networkx_labels(G_diff, pos, font_size=20, font_family='sans-serif') # Get labels of weights #labels = nx.get_edge_attributes(G,'weight') # Plot weights nx.draw_networkx_edge_labels(G_diff, pos, edge_labels=nx.get_edge_attributes(G_diff,'weight')) # Making figure nice #plt.legend(['Nodes','TN','FP','test']) ax = plt.gca() ax.set_axis_off() plt.show() # Return return(scores, adjmat_diff)
def intersect(a,b): a = np.array(a) b = np.array(b) #print "intersect-a:", a.size #print "intersect-b:", b.size #print "intersecting..... \n", intersect = np.intersect1d(b,a) dummy, iA = ismember(intersect, a) dummy, iB = ismember(intersect, b) #print "intersect", intersect return intersect, iA, iB
def intersect(a, b): a = np.array(a) b = np.array(b) #print "intersect-a:", a.size #print "intersect-b:", b.size #print "intersecting..... \n", intersect = np.intersect1d(b, a) dummy, iA = ismember(intersect, a) dummy, iB = ismember(intersect, b) #print "intersect", intersect return intersect, iA, iB
def set_diff(a, b): """Get the difference between SET a and SET b. Parameters : a : sequence[n_a, ] or nd.ndarray[n_a, ]. The first set. b : sequence[n_b, ] or nd.ndarray[n_b, ]. The second set. Returns : diff : nd.ndarray[n_diff, ] : All the elements in set a but not in set b. diff_index_in_a : nd.ndarray[n_a, ] of boolean : a[diff_index_in_a] == diff. Raises : None Notes : diff, diff_index_in_a = set_diff(a, b) """ a = np.array(a) b = np.array(b) diff = np.setdiff1d(a, b) dummy, iA, alliA = ismember(diff, a) return diff, iA
def vec2adjmat(source, target, weight=None, symmetric=True): """Convert source and target into adjacency matrix. Parameters ---------- source : list The source node. target : list The target node. weight : list of int The Weights between the source-target values symmetric : bool, optional Make the adjacency matrix symmetric with the same number of rows as columns. The default is True. Returns ------- pd.DataFrame adjacency matrix. Examples -------- >>> source=['Cloudy','Cloudy','Sprinkler','Rain'] >>> target=['Sprinkler','Rain','Wet_Grass','Wet_Grass'] >>> vec2adjmat(source, target) >>> >>> weight=[1,2,1,3] >>> vec2adjmat(source, target, weight=weight) """ if len(source)!=len(target): raise Exception('[hnet] >Source and Target should have equal elements.') if weight is None: weight = [1]*len(source) df = pd.DataFrame(np.c_[source, target], columns=['source','target']) # Make adjacency matrix adjmat = pd.crosstab(df['source'], df['target'], values=weight, aggfunc='sum').fillna(0) # Get all unique nodes nodes = np.unique(list(adjmat.columns.values)+list(adjmat.index.values)) # nodes = np.unique(np.c_[adjmat.columns.values, adjmat.index.values].flatten()) # Make the adjacency matrix symmetric if symmetric: # Add missing columns node_columns = np.setdiff1d(nodes, adjmat.columns.values) for node in node_columns: adjmat[node]=0 # Add missing rows node_rows = np.setdiff1d(nodes, adjmat.index.values) adjmat=adjmat.T for node in node_rows: adjmat[node]=0 adjmat=adjmat.T # Sort to make ordering of columns and rows similar [IA, IB] = ismember(adjmat.columns.values, adjmat.index.values) adjmat = adjmat.iloc[IB,:] adjmat.index.name='source' adjmat.columns.name='target' return(adjmat)
def set_minus(a, b): """Get the elements in the first set but not in the second set. Parameters : a : sequence[n_a, ] or nd.ndarray[n_a, ]. The first set. b : sequence[n_b, ] or nd.ndarray[n_b, ]. The second set. Returns : c : nd.ndarray[n_c, ] : All the elements in set a but not in set b. Raises : None Notes : c = set_minus(a, b) c is the set A, without the elements B. C preserves the ordering of A. """ a = np.array(a) b = np.array(b) dummy, a_in_inter, all_a_in_inter = ismember(a, b) diff = a[dummy == False] return diff
def pathFinder(adjMX, adjMY, Mweight, roi, szImgNew): #Mweight can be either MW or MmW findroiImg = (np.nonzero(np.ravel(roi, order='F')))[0] includeX = iM.ismember(adjMX, findroiImg) includeY = iM.ismember(adjMY, findroiImg) keepInd = np.logical_and(includeX, includeY) newLen = np.count_nonzero(keepInd) (RealadjMW, RealadjMX, RealadjMY) = (np.zeros(newLen), np.zeros(newLen), np.zeros(newLen)) q = 0 for r in range(0, (keepInd.size)): if keepInd[r]: RealadjMW[q] = Mweight[r] RealadjMX[q] = adjMX[r] RealadjMY[q] = adjMY[r] q = q + 1 strmin = str(np.amin(RealadjMX)) strmax = str(np.amax(RealadjMX)) #finding the 'shortest path' on the light to dark boundary (MW) #or dark to light boundary (MmW) wGraph = sparseToDict(RealadjMX, RealadjMY, RealadjMW) #convert graph to hashtable pathW = dj.shortest_path(wGraph, strmin, strmax) fullPathLen = len(pathW) pathX, pathY = np.zeros(fullPathLen), np.zeros(fullPathLen) for a in range(0, fullPathLen): (pathY[a], pathX[a]) = np.unravel_index(int(float(pathW[a])), szImgNew, order='F') xmin = np.amin(pathX) xmax = np.amax(pathX) truepathX1 = [] truepathY1 = [] #Removes the first and lost points (through the padded zeros) z = 0 for xele in pathX: if xele != xmin and xele != xmax: truepathX1.append(xele - 1) #because you removed the first column truepathY1.append(pathY[z]) z = z + 1 return truepathX1, truepathY1
def _post_processing(simmat_padj, nr_succes_pop_n, simmat_labx, alpha, multtest, fillna, dropna, verbose=3): # Clean label names by chaning X.0 into X simmat_padj.columns = list( map(lambda x: x[:-2] if x[-2:] == '.0' else x, simmat_padj.columns)) simmat_padj.index = list( map(lambda x: x[:-2] if x[-2:] == '.0' else x, simmat_padj.index.values)) nr_succes_pop_n = np.array(nr_succes_pop_n) nr_succes_pop_n[:, 0] = list( map(lambda x: x[:-2] if x[-2:] == '.0' else x, nr_succes_pop_n[:, 0])) if verbose >= 5: print(simmat_padj) # Multiple test correction simmat_padj = _multipletestcorrectionAdjmat(simmat_padj, multtest, verbose=verbose) # Remove variables for which both rows and columns are empty if dropna: [simmat_padj, simmat_labx] = _drop_empty(simmat_padj, simmat_labx, verbose=verbose) # Fill empty fields if fillna: simmat_padj.fillna(1, inplace=True) # Remove those with P>alpha, to prevent unnecesarilly edges simmat_padj[simmat_padj > alpha] = 1 # Convert P-values to -log10 scale adjmatLog = _logscale(simmat_padj) # Set zeros on diagonal but make sure it is correctly ordered if np.all(adjmatLog.index.values == adjmatLog.columns.values): np.fill_diagonal(adjmatLog.values, 0) if np.all(simmat_padj.index.values == simmat_padj.columns.values): np.fill_diagonal(simmat_padj.values, 1) # Remove edges from matrix if dropna: idx1 = np.where((simmat_padj == 1).sum( axis=1) == simmat_padj.shape[0])[0] idx2 = np.where((simmat_padj == 1).sum( axis=0) == simmat_padj.shape[0])[0] keepidx = np.setdiff1d(np.arange(simmat_padj.shape[0]), np.intersect1d(idx1, idx2)) simmat_padj = simmat_padj.iloc[keepidx, keepidx] adjmatLog = adjmatLog.iloc[keepidx, keepidx] simmat_labx = simmat_labx[keepidx] IA, _ = ismember(nr_succes_pop_n[:, 0], simmat_padj.columns.values) nr_succes_pop_n = nr_succes_pop_n[IA, :] return (simmat_padj, adjmatLog, simmat_labx, nr_succes_pop_n)
def _get_hexcolor(label, cmap='Paired'): label = label.astype(str) if label[0][0]!='#': label = label.astype(dtype='U7') uinode = np.unique(label) tmpcolors = np.array(sns.color_palette(cmap, len(uinode)).as_hex()) [IA,IB] = ismember(label,uinode) label[IA] = tmpcolors[IB] return(label)
def pathFinder (adjMX, adjMY, Mweight, roi, szImgNew): #Mweight can be either MW or MmW findroiImg = (np.nonzero(np.ravel(roi, order='F')))[0] includeX = iM.ismember(adjMX, findroiImg) includeY = iM.ismember(adjMY, findroiImg) keepInd = np.logical_and (includeX, includeY) newLen = np.count_nonzero(keepInd) (RealadjMW, RealadjMX, RealadjMY) = (np.zeros(newLen), np.zeros(newLen), np.zeros(newLen)) q = 0 for r in range (0, (keepInd.size)): if keepInd[r]: RealadjMW[q] = Mweight[r] RealadjMX[q] = adjMX[r] RealadjMY[q] = adjMY[r] q = q + 1 strmin = str(np.amin(RealadjMX)) strmax = str(np.amax(RealadjMX)) #finding the 'shortest path' on the light to dark boundary (MW) #or dark to light boundary (MmW) wGraph = sparseToDict(RealadjMX, RealadjMY, RealadjMW) #convert graph to hashtable pathW = dj.shortest_path(wGraph, strmin, strmax) fullPathLen = len(pathW) pathX, pathY = np.zeros(fullPathLen), np.zeros(fullPathLen) for a in range (0,fullPathLen): (pathY[a],pathX[a]) = np.unravel_index(int(float(pathW[a])), szImgNew, order='F') xmin = np.amin(pathX) xmax = np.amax(pathX) truepathX1 = [] truepathY1 = [] #Removes the first and lost points (through the padded zeros) z = 0 for xele in pathX: if xele != xmin and xele != xmax: truepathX1.append(xele - 1) #because you removed the first column truepathY1.append(pathY[z]) z = z + 1 return truepathX1, truepathY1
def read_pp_raptis(): countries = {'BGD': 'Bangladesh', 'KHM': 'Cambodia', 'CHN': 'China' \ , 'IND': 'India', 'IDN': 'Indonesia', 'JPN': 'Japan' \ , 'MYS': 'Malaysia', 'MNG': 'Mongolia', 'MMR': 'Myanmar' \ , 'PAK': 'Pakistan', 'PHL': 'Philippines' \ , 'KOR': 'South Korea', 'TWN': 'Taiwan' \ , 'THA': 'Thailand', 'VNM': 'Vietnam'} # Read the power plants df = pd.read_excel(os.path.join(os.path.realpath('..') \ , 'IIASAPP_AISA_COAL.xlsx'), 'Sheet1', 0) # Subset to operating coal-fired power plants in the above Asian countries, # (removed: built after 2010 to reflect the up-to-date technology) subset = np.asarray(ismember(df.ISO, countries.keys())) \ & np.asarray(ismember(df.msg_combo, ['coal_st', 'coal_cc'])) \ & (df.STATUS.values.astype(unicode) == 'OPR') # Note: Tech2 has the information whether it is ST or coal-CC; Tech only # tells whether the turbine is sub-, super-, or ultrasuper-critical # Fills the missing values in the country column with the de-coded ISO code # and in this process replaces Hongkong (China) by China df1 = pd.DataFrame(data = collections.OrderedDict( \ {'Unit': df['UNIT'][subset].astype(unicode) \ , 'Plant': df['PLANT'][subset].astype(unicode) \ , 'Country': [countries[x] for x in df['ISO'][subset].astype(unicode)] \ , 'Capacity': pd.to_numeric(df['MW_x'][subset], errors = coerce) \ , 'Year': pd.to_numeric(df['YEAR'][subset].astype(unicode), errors = coerce) \ , 'Tech': df['STYPE'][subset].astype(unicode) \ , 'Tech2': df['msg_combo'][subset].astype(unicode) \ , 'Cooling': df['cool_group_msg'][subset].astype(unicode) , 'Latitude': pd.to_numeric(df['latC'][subset], errors = coerce) \ , 'Longitude': pd.to_numeric(df['lonC'][subset], errors = coerce) \ , 'Net_Eff': pd.to_numeric(df['mean_annual_cycle_efficiency'][subset], errors = coerce) \ })) # Drop the unknown lat & lon's df1 = df1.dropna(subset=('Latitude', 'Longitude')).reset_index(drop=True) return df1
def vec2adjmat(source, target, symmetric=True): """Convert source and target into adjacency matrix. Parameters ---------- source : list The source node. target : list The target node. symmetric : bool, optional Make the adjacency matrix symmetric with the same number of rows as columns. The default is True. Returns ------- pd.DataFrame adjacency matrix. Examples -------- >>> source=['Cloudy','Cloudy','Sprinkler','Rain'] >>> target=['Sprinkler','Rain','Wet_Grass','Wet_Grass'] >>> vec2adjmat(source, target) """ df = pd.DataFrame(np.c_[source, target], columns=['source','target']) # Make adjacency matrix adjmat = pd.crosstab(df['source'], df['target']) # Get all unique nodes # nodes = np.unique(np.c_[adjmat.columns.values, adjmat.index.values].flatten()) nodes = np.unique(list(adjmat.columns.values)+list(adjmat.index.values)) # Make the adjacency matrix symmetric if symmetric: # Add missing columns node_columns = np.setdiff1d(nodes, adjmat.columns.values) for node in node_columns: adjmat[node]=0 # Add missing rows node_rows = np.setdiff1d(nodes, adjmat.index.values) adjmat=adjmat.T for node in node_rows: adjmat[node]=0 adjmat=adjmat.T # Sort to make ordering of columns and rows similar [IA, IB] = ismember(adjmat.columns.values, adjmat.index.values) adjmat = adjmat.iloc[IB,:] adjmat.index.name='source' adjmat.columns.name='target' return(adjmat)
def test_ismember(): # Test 1 a_vec = [1, 2, 3, None] b_vec = [4, 1, 2] [I, idx] = ismember(a_vec, b_vec) assert np.all(np.array(a_vec)[I] == np.array(b_vec)[idx]) # Test 2 a_vec = pd.DataFrame([ 'aap', 'None', 'mies', 'aap', 'boom', 'mies', None, 'mies', 'mies', 'pies', None ]) b_vec = pd.DataFrame([None, 'mies', 'mies', 'pies', None]) [I, idx] = ismember(a_vec, b_vec) assert np.all(a_vec.values[I] == b_vec.values[idx].flatten()) # Test 3 a_vec = np.array([1, 2, 3, None]) b_vec = np.array([1, 2, 4]) [I, idx] = ismember(a_vec, b_vec) assert np.all(a_vec[I] == b_vec[idx]) # Test 4 a_vec = np.array(['boom', 'aap', 'mies', 'aap']) b_vec = np.array(['aap', 'boom', 'aap']) [I, idx] = ismember(a_vec, b_vec) assert np.all(a_vec[I] == b_vec[idx]) # Test 5: elements matrices a_vec = np.random.randint(0, 10, (5, 8)).astype(str) b_vec = np.random.randint(0, 10, (5, 10)).astype(str) Iloc, idx = ismember(a_vec, b_vec, 'elementwise') for i in np.arange(0, a_vec.shape[0]): assert np.all(a_vec[i, Iloc[i]] == b_vec[i, idx[i]]) # Test 5: elements matrices a_vec = np.random.randint(0, 10, (5, 8)).astype(str) b_vec = np.random.randint(0, 10, (5, 10)).astype(str) Iloc, idx = ismember(a_vec, b_vec, 'elementwise') for i in np.arange(0, a_vec.shape[0]): assert np.all(a_vec[i, Iloc[i]] == b_vec[i, idx[i]]) # Test rows a_vec = np.array( ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12), (4, 5, 6))) b_vec = np.array(((4, 5, 6), (7, 8, 0), (10, 11, 12))) Lia, Locb = ismember(a_vec, b_vec, 'rows') assert np.all(a_vec[Lia] == b_vec[Locb])
def vec2adjmat(source, target, symmetric=True): """Convert source and target into adjacency matrix. Parameters ---------- source : list The source node. target : list The target node. symmetric : bool, optional Make the adjacency matrix symmetric with the same number of rows as columns. The default is True. Returns ------- adjacency matrix : pd.DataFrame(). """ # Make adjacency matrix adjmat = pd.crosstab(source, target) # Get all unique nodes nodes = np.unique(np.c_[adjmat.columns.values, adjmat.index.values].flatten()) # Make the adjacency matrix symmetric if symmetric: # Add missing columns node_columns = np.setdiff1d(nodes, adjmat.columns.values) for node in node_columns: adjmat[node] = 0 # Add missing rows node_rows = np.setdiff1d(nodes, adjmat.index.values) adjmat = adjmat.T for node in node_rows: adjmat[node] = 0 adjmat = adjmat.T # Sort to make ordering of columns and rows similar [IA, IB] = ismember(adjmat.columns.values, adjmat.index.values) adjmat = adjmat.iloc[IB, :] adjmat.index.name = 'source' adjmat.columns.name = 'target' return (adjmat)
def setpot(pot, evvariables, evidstates): #FIXME: data format needed to be unified vars = pot.variables #vars = np.array(pot.variables) # convert to ndarray format #evariables = np.array(evvariables) # convert to ndarray format #evidstates = np.array(evidstates) # convert to ndarray format #print "variables:", vars table = pot.table nstates = pot.card #print "number of states:", nstates #print "vars:", vars #print "evvariables:", evvariables intersection, iv, iev = intersect(vars, evvariables) #iv = np.array(iv) #iev = np.array(iev) #print "intersection:", intersection #print "iv:", iv #print "iev:", iev #print "iv type:", type(iv) #print "number of intersection:", intersection.size if intersection.size == 0: newpot = copy.copy(pot) else: newvar = setminus(vars, intersection) dummy, idx = ismember(newvar, vars) newns = nstates[idx] newpot = potential() newpot.variables = newvar newpot.card = newns newpot.table = np.zeros(newns) #print "idx:", idx #print "iv:", iv for i in range(np.prod(newns)): newassign = IndexToAssignment(i, newns) oldassign = np.zeros(nstates.size, 'int8') oldassign[idx] = newassign oldassign[iv] = evidstates #print "newpot.table.shape:", newpot.table.shape #print "newassign:", newassign #print "newassign type:", type(newassign) newpot.table[tuple(newassign)] = pot.table[tuple(oldassign)] return newpot
def adjmat2G(adjmat, df, edge_distance_min=None, edge_distance_max=None, edge_width=None): """Convert adjacency matrix to graph. Parameters ---------- adjmat : dataframe Dataframe for which the nodes are the columns and index and edges are the values in the array. df : dataframe Dataframe that is connected to adjmat. edge_distance_min : int, (default: None) Scale the weights with a minimum value. edge_distance_max : int, (default: None) Scale the weights with a maximum value. edge_width : int, (default: None) Width of edge, scale between [1-10] if there is are more then 2 diferent weights. The default is None. Returns ------- dict containing Graph G and dataframe. """ # Convert adjmat node_names = adjmat.index.values adjmat.reset_index(drop=True, inplace=True) adjmat.index = adjmat.index.values.astype(str) adjmat.columns = np.arange(0, adjmat.shape[1]).astype(str) adjmat = adjmat.stack().reset_index() adjmat.columns = ['source', 'target', 'weight'] # Width of edge, scale between [1-10] if there is are more then 2 diferent weights if isinstance(edge_width, type(None)): if len(np.unique(adjmat['weight'].values.reshape(-1, 1)))>2: adjmat['edge_width'] = _normalize_size(adjmat['weight'].values.reshape(-1, 1), 1, 20) else: edge_width=1 if not isinstance(edge_width, type(None)): adjmat['edge_width'] = np.ones(adjmat.shape[0]) * edge_width # Scale the weights towards an edge weight if not isinstance(edge_distance_min, type(None)): adjmat['edge_weight'] = _normalize_size(adjmat['weight'].values.reshape(-1, 1), edge_distance_min, edge_distance_max) else: adjmat['edge_weight'] = adjmat['weight'].values.reshape(-1, 1) # Keep only edges with weight edge_weight_original = adjmat['weight'].values.reshape(-1, 1).flatten() adjmat = adjmat.loc[edge_weight_original > 0, :].reset_index(drop=True) # Remove self-loops Iloc = adjmat['source'] != adjmat['target'] adjmat = adjmat.loc[Iloc, :].reset_index(drop=True) # Include source-target label source_label = np.repeat('', adjmat.shape[0]).astype('O') target_label = np.repeat('', adjmat.shape[0]).astype('O') for i in range(0, len(node_names)): source_label[adjmat['source']==str(i)] = node_names[i] for i in range(0, len(node_names)): target_label[adjmat['target']==str(i)] = node_names[i] adjmat['source_label'] = source_label adjmat['target_label'] = target_label # Make sure indexing of nodes is correct with the edges. uilabels = np.unique(np.append(adjmat['source'], adjmat['target'])) tmplabels=adjmat[['source', 'target']] adjmat['source']=None adjmat['target']=None for i in range(0, len(uilabels)): I1 = tmplabels['source']==uilabels[i] I2 = tmplabels['target']==uilabels[i] adjmat['source'].loc[I1] = str(i) adjmat['target'].loc[I2] = str(i) # adjmat['source']=ismember(tmplabels['source'],uilabels)[1].astype(str) # adjmat['target']=ismember(tmplabels['target'],uilabels)[1].astype(str) G = nx.Graph() # G.add_nodes_from(np.unique(uilabels)) try: G = nx.from_pandas_edgelist(adjmat, 'source', 'target', ['weight', 'edge_weight','edge_width','source_label','target_label','source', 'target']) except: # Version of networkx<2 G = nx.from_pandas_dataframe(adjmat, 'source', 'target', edge_attr=['weight', 'edge_weight','edge_width','source_label','target_label','source', 'target']) # Add node information A = pd.concat([pd.DataFrame(adjmat[['target', 'target_label']].values),pd.DataFrame(adjmat[['source', 'source_label']].values)], axis=0) A = A.groupby([0, 1]).size().reset_index(name='Freq') [IA,IB] = ismember(df['node_name'], A[1]) df = df.loc[IA,:] df.index = A[0].loc[IB].values.astype(str) if not df.empty: getnodes = np.array([*G.nodes]) for col in df.columns: for i in range(0, df.shape[0]): idx = df.index.values[i] if np.any(np.isin(getnodes, df.index.values[i])): G.nodes[idx][col] = str(df[col][idx]) else: print('[d3graph] >not found') return(G, df)
def compare_networks(adjmat_true, adjmat_pred, pos=None, showfig=True, width=15, height=8, verbose=3): """Compare two networks. Parameters ---------- adjmat_true : TYPE Adjacency matrix 1. adjmat_pred : TYPE Adjacency matrix 2. pos : list, optional list with coordinates to orientate the nodes. showfig : bool, optional Plot figure to screen. The default is True. width : int, optional Width of the figure. The default is 15. height : int, optional Height of the figure. The default is 8. verbose : int, optional Verbosity. The default is 3. Returns ------- dict. scores adjmat_diff """ # Make sure columns and indices to match IArow, IBrow = ismember(adjmat_true.index.values, adjmat_pred.index.values) IAcol, IBcol = ismember(adjmat_true.columns.values, adjmat_pred.columns.values) adjmat_true = adjmat_true.loc[IArow, IAcol] adjmat_pred = adjmat_pred.iloc[IBrow, IBcol] # Check whether order is correct if not np.all(adjmat_true.columns.values == adjmat_pred.columns.values): raise ValueError( 'Column order of both input values could not be matched') if not np.all(adjmat_true.index.values == adjmat_pred.index.values): raise ValueError('Row order of both input values could not be matched') # Make sure it is boolean adjmat adjmat_true = adjmat_true > 0 adjmat_pred = adjmat_pred > 0 # Get edges y_true = adjmat_true.stack().reset_index()[0].values y_pred = adjmat_pred.stack().reset_index()[0].values # Compute score scores = clf.confmatrix.eval(y_true, y_pred, verbose=verbose) # bayes.plot(out_bayes['adjmat'], pos=G['pos']) # Setup graph # adjmat_diff = adjmat_true - adjmat_pred adjmat_diff = adjmat_true.astype(int) adjmat_diff[(adjmat_true.astype(int) - adjmat_pred.astype(int)) < 0] = 2 adjmat_diff[(adjmat_true.astype(int) - adjmat_pred.astype(int)) > 0] = -1 if showfig: # Setup graph # G_true = adjmat2graph(adjmat_true) G_diff = adjmat2graph(adjmat_diff) # Graph layout pos = graphlayout(G_diff, pos=pos, scale=1, layout='fruchterman_reingold') # Bootup figure plt.figure(figsize=(width, height)) # nodes nx.draw_networkx_nodes(G_diff, pos, node_size=700, with_labels=True) # edges colors = [G_diff[u][v]['color'] for u, v in G_diff.edges()] #weights = [G_diff[u][v]['weight'] for u,v in G_diff.edges()] nx.draw_networkx_edges(G_diff, pos, arrowstyle='->', edge_color=colors, width=1) # Labels nx.draw_networkx_labels(G_diff, pos, font_size=20, font_family='sans-serif') # Get labels of weights #labels = nx.get_edge_attributes(G,'weight') # Plot weights nx.draw_networkx_edge_labels(G_diff, pos, edge_labels=nx.get_edge_attributes( G_diff, 'weight')) # Making figure nice #plt.legend(['Nodes','TN','FP','test']) ax = plt.gca() ax.set_axis_off() plt.show() # Return return (scores, adjmat_diff)
groups2 = coal_plants.groupby('Tech') techName = {'SUPERC': 'Supercritical', 'SUBCR': 'Subcritical' \ , 'ULTRSC': 'Ultra-super', 'nan': 'Unknown'} techList = ['SUBCR', 'SUPERC'] cooName = {'ot_saline': 'OT Saline', 'cl_fresh': 'CT Fresh' \ , 'ot_fresh': 'OT Fresh', 'air': 'Air', 'CHP': 'CHP'} ##cooList = coal_plants.Cooling.unique() cooList = ['cl_fresh','ot_fresh','air'] # Fit curves for the different combustion types # ---- do not separate between cooling system types because the differences # are not clear, but limit to ot_fresh, cl_fresh, and air params = [] for tt in range(len(techList)): subset = ismember(techList, ['ot_fresh','cl_fresh','air']) #try: group = groups2.get_group(techList[tt]) #except KeyError: # continue no_nans = ~(np.isnan(group['Net_Eff'].values) \ | np.isnan(group['Capacity'].values)) xdata = pd.to_numeric(group.Capacity[no_nans]) ydata = pd.to_numeric(group.Net_Eff[no_nans]) # Log/Sigmoid/Linear fits if techList[tt]=='SUBCR': prms, pcovs = curve_fit(logcurve, xdata, ydata) else: prms, pcovs = curve_fit(straightline, xdata, ydata) params.append(tuple(prms))
# Example # ------- import numpy as np from ismember import ismember import pandas as pd # %% Row wise a_vec = np.array(((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12), (4, 5, 6))) b_vec = np.array(((4, 5, 6), (7, 8, 0), (10, 11, 12))) Lia, Locb = ismember(a_vec, b_vec, 'rows') print(a_vec[Lia]) print(b_vec[Locb]) # %% Example 1 a_vec = [1, 2, 3, None] b_vec = [4, 1, 2] I, idx = ismember(a_vec, b_vec) print(np.array(a_vec)[I]) print(np.array(b_vec)[idx]) # %% Example 2 a_vec = pd.DataFrame([ 'aap', 'None', 'mies', 'aap', 'boom', 'mies', None, 'mies', 'mies', 'pies', None ])
# Example # ------- import numpy as np from ismember import ismember import pandas as pd # %% Example 1 a_vec = [1, 2, 3, None] b_vec = [4, 1, 2] [I, idx] = ismember(a_vec, b_vec) np.array(a_vec)[I] np.array(b_vec)[idx] # %% Example 2 a_vec = pd.DataFrame([ 'aap', 'None', 'mies', 'aap', 'boom', 'mies', None, 'mies', 'mies', 'pies', None ]) b_vec = pd.DataFrame([None, 'mies', 'mies', 'pies', None]) [I, idx] = ismember(a_vec, b_vec) a_vec.values[I] b_vec.values[idx].flatten() # %% Example 3 a_vec = np.array([1, 2, 3, None]) b_vec = np.array([1, 2, 4]) [I, idx] = ismember(a_vec, b_vec)