def find_Match_Attrib(Netz_1, CompName_1, AttribName_1, Netz_2, CompName_2, AttribName_2, SearchOption='single', CountryOn=False, AddInWord=0, String2Lower=False): """ Finds a vector containing the positions of which EntsoG component should be linked with which point from Netz class instance. The following attributes are currently implemented: name, lat, and long. Input to method are **EntsoGCompName**, **Netz** instance, **NetzCompName**, **multDist**, **testRun**, **powerVal**. Return are the position lists for the EntsoG instance, the Netz instance, and the Goodness value (ranging 0..1). \n.. comments: Input: Netz_1: Netz Class instance CompName_1: string, of Netz_1 component name, to be used. Netz_2: instance of class Netz CompName_2: string, of Netz_2 component name, to be used. SearchOption string indicating if entries from Netz_2 are allowed to be used multiple times,... 'multi', 'single', CountryOn [False], compares only locations in the same contry AddInWord [0], adds value if name of one set is in name of other set. String2Lower [False], if strings shall be converted to lower Return: posEntsoG: List of ints, of positions from EntsoG posNetz: List of ints, of positions from Netz GoodnessVal: list of floats, of goodness values """ # Selecting the dat based on Component from EntsoG Comp_1 = Netz_1.__dict__[CompName_1] # Selecting the dat based on Component from Netze Comp_2 = Netz_2.__dict__[CompName_2] # Initialization of variables pos_1 = [] pos_2 = [] GoodnessVal = [] posLeft_1 = [s for s in range(len(Comp_1))] posLeft_2 = [s for s in range(len(Comp_2))] Run_1 = True Run_2 = True # Dealing with country subset if CountryOn: Country_1 = M_Helfer.get_NotPos(Comp_1, pos_1, 'country_code') Country_2 = M_Helfer.get_NotPos(Comp_2, pos_2, 'country_code') Country_Matrix_Orig = M_Helfer.get_NameMatrix_Fuzzy( Country_1, Country_2) for xx in range(len(Country_Matrix_Orig)): for yy in range(len(Country_Matrix_Orig[0])): if Country_Matrix_Orig[xx][yy] >= 100: Country_Matrix_Orig[xx][yy] = 1 elif Country_1[xx] == None or Country_2[yy] == None: Country_Matrix_Orig[xx][yy] = 1 else: Country_Matrix_Orig[xx][yy] = 0 else: Country_1 = M_Helfer.get_NotPos(Comp_1, pos_1, 'country_code') Country_2 = M_Helfer.get_NotPos(Comp_2, pos_2, 'country_code') Country_Matrix_Orig = M_Helfer.get_NameMatrix_Fuzzy( Country_1, Country_2) for xx in range(len(Country_Matrix_Orig)): for yy in range(len(Country_Matrix_Orig[0])): Country_Matrix_Orig[xx][yy] = 1 if String2Lower: print('change code') # Running through data set for first time, to catch all locations, where name is totally same Name_1 = M_Helfer.get_NotPos(Comp_1, pos_1, AttribName_1) Name_2 = M_Helfer.get_NotPos(Comp_2, pos_2, AttribName_2) # Getting matching location names [New_pos_1, New_pos_2] = M_Helfer.get_NameMatch(Name_1, Name_2) # Generating un-shrunk data for later Orig_Name_1 = M_Helfer.get_NotPos(Comp_1, [], AttribName_1) Orig_Name_2 = M_Helfer.get_NotPos(Comp_2, [], AttribName_2) Name_Matrix_Orig = M_Helfer.get_NameMatrix_Fuzzy(Orig_Name_1, Orig_Name_2, AddInWord) Name_Matrix_Orig = M_MatLab.multi_2Matrix(Name_Matrix_Orig, Country_Matrix_Orig) # Combining matrixes GoodnessMatrix_Orig = Name_Matrix_Orig # Now going through the rest of the data set while Run_2 and Run_1: GoodnessMatrix_Shrunk = M_MatLab.shrink_Matrix(GoodnessMatrix_Orig, posLeft_1, posLeft_2) # determin popsitions in shrunk data sets [pos_Shrunk_1, pos_Shrunk_2 ] = M_FindPos.find_pos_ConditionInMatrix(GoodnessMatrix_Shrunk, 'max') GoodnessVal.append(GoodnessMatrix_Shrunk[pos_Shrunk_1][pos_Shrunk_2]) # dtermin position in original data sets pos_Orig_1 = posLeft_1[pos_Shrunk_1] pos_Orig_2 = posLeft_2[pos_Shrunk_2] pos_1.append(pos_Orig_1) posLeft_1.remove(pos_Orig_1) pos_2.append(pos_Orig_2) if 'single' in SearchOption: posLeft_2.remove(pos_Orig_2) # Check if need to stop if len(pos_1) == len(Comp_1): Run_1 = False if len(pos_2) == len(Comp_2): Run_2 = False return [pos_1, pos_2, GoodnessVal]
def find_MatchNetzPoint(Netz_1, CompName_1, Netz_2, CompName_2, multDist=1, testRun=False, powerVal=1): """ Finds a vector containing the positions of which EntsoG component should be linked with which point from Netz class instance. The following attributes are currently implemented: name, lat, and long. Input to method are **EntsoGCompName**, **Netz** instance, **NetzCompName**, **multDist**, **testRun**, **powerVal**. Return are the position lists for the EntsoG instance, the Netz instance, and the Goodness value (ranging 0..1). \n.. comments: Input: Netz_1: Netz Class instance CompName_1: string, of Netz_1 component name, to be used. Netz_2: instance of class Netz CompName_2: string, of Netz_2 component name, to be used. multDist: (Optional = 1) testRun: (Optional = False), for: True = will NOT carry out the long while loop False = will carry out the long while loop [] = will carry out the long while loop Return: posEntsoG: List of ints, of positions from EntsoG posNetz: List of ints, of positions from Netz GoodnessVal: list of floats, of goodness values """ # Selecting the dat based on Component from EntsoG Comp_1 = Netz_1.__dict__[CompName_1] # Selecting the dat based on Component from Netze Comp_2 = Netz_2.__dict__[CompName_2] # Initialization of variables pos_1 = [] pos_2 = [] GoodnessVal = [] posLeft_1 = [s for s in range(len(Comp_1))] posLeft_2 = [s for s in range(len(Comp_2))] Run_1 = True Run_2 = True # So that Test Runs with shorter time can be executed if testRun: Run_1 = False #script_dir = path.dirname(__file__) #logFileName = path.join(script_dir, '../Ausgabe/log_' + str(multDist) + '.csv') logFileName = '../Ausgabe/log_' + str(multDist) + '.csv' Name_Orig_1 = M_Helfer.get_NotPos(Comp_1, pos_1, 'name') Name_Orig_2 = M_Helfer.get_NotPos(Comp_2, pos_2, 'name') # Running through data set for first time, to catch all locations, where name is totally same [Name_1, lat_1, long_1] = M_Helfer.get_NotPos3(Comp_1, pos_1) [Name_2, lat_2, long_2] = M_Helfer.get_NotPos3(Comp_2, pos_2) # Getting matching location names [New_pos_1, New_pos_2] = M_Helfer.get_NameMatch(Name_1, Name_2) # Getting Distances = M_Projection.LatLong2DistanceMatrix(lat_1, long_1, lat_2, long_2) InvDistReal2 = M_MatLab.pow_Matrix(Distances, powerVal) InvDistReal = M_MatLab.multi_MatrixConst(InvDistReal2, multDist) # Schreiben von Ergebnissen in eine CSV Datei if os.path.isfile(logFileName): os.remove(logFileName) M_Helfer.txt2File( logFileName, 'EntsoG_Name;Netz_Name;NameSim;Distance;Goodness;EntsoG_pos;Netz_pos') for ii in range(len(New_pos_1)): # adding new positoins to vec of positions found pos_1.append(New_pos_1[ii]) pos_2.append(New_pos_2[ii]) GoodnessVal.append(100 - InvDistReal[New_pos_1[ii]][New_pos_2[ii]]) # removing positions that are found from vector of Pos to be found try: posLeft_1.remove(New_pos_1[ii]) except: pass try: posLeft_2.remove(New_pos_2[ii]) except: pass # writing to log file strstr = Name_Orig_1[New_pos_1[ii]] + ';' + \ Name_Orig_2[New_pos_2[ii]] + ';' + \ '100;' + \ str(Distances[New_pos_1[ii]][New_pos_2[ii]]) + ';' + \ str(100 - InvDistReal[New_pos_1[ii]][New_pos_2[ii]]) + ';' + \ str(New_pos_1[ii]) + ';' + str(New_pos_2[ii]) M_Helfer.txt2File(logFileName, strstr) # Generating un-shrunk data for later [Orig_Name_1, Orig_lat_1, Orig_long_1] = M_Helfer.get_NotPos3(Comp_1, []) [Orig_Name_2, Orig_lat_2, Orig_long_2] = M_Helfer.get_NotPos3(Comp_2, []) # Forming matrixes Name_Matrix_Orig = M_Helfer.get_NameMatrix_Fuzzy(Orig_Name_1, Orig_Name_2) Dist_Matrix_Orig = M_Projection.LatLong2DistanceMatrix( Orig_lat_1, Orig_long_1, Orig_lat_2, Orig_long_2) Dist_Matrix_Orig2 = M_MatLab.pow_Matrix(Dist_Matrix_Orig, powerVal) Dist_Matrix_Orig3 = M_MatLab.multi_MatrixConst(Dist_Matrix_Orig2, multDist) # Combining matrixes GoodnessMatrix_Orig = M_MatLab.sub_2Matrix(Name_Matrix_Orig, Dist_Matrix_Orig3) # Now going through the rest of the data set while Run_2 and Run_1: GoodnessMatrix_Shrunk = M_MatLab.shrink_Matrix(GoodnessMatrix_Orig, posLeft_1, posLeft_2) Name_Matrix_Shrunk = M_MatLab.shrink_Matrix(Name_Matrix_Orig, posLeft_1, posLeft_2) Dist_Matrix_Shrunk = M_MatLab.shrink_Matrix(Dist_Matrix_Orig, posLeft_1, posLeft_2) # determin popsitions in shrunk data sets [pos_Shrunk_1, pos_Shrunk_2 ] = M_FindPos.find_pos_ConditionInMatrix(GoodnessMatrix_Shrunk, 'max') nam = Name_Matrix_Shrunk[pos_Shrunk_1][pos_Shrunk_2] dis = Dist_Matrix_Shrunk[pos_Shrunk_1][pos_Shrunk_2] GoodnessVal.append(GoodnessMatrix_Shrunk[pos_Shrunk_1][pos_Shrunk_2]) # dtermin position in original data sets pos_Orig_1 = posLeft_1[pos_Shrunk_1] pos_Orig_2 = posLeft_2[pos_Shrunk_2] pos_1.append(pos_Orig_1) pos_2.append(pos_Orig_2) posLeft_1.remove(pos_Orig_1) posLeft_2.remove(pos_Orig_2) # For Log file strstr = Name_Orig_1[pos_Orig_1] + ';' + Name_Orig_2[pos_Orig_2] + \ ';' + str(nam) + ';' + str(dis) + ';' + \ str(GoodnessMatrix_Shrunk[pos_Shrunk_1][pos_Shrunk_2]) + ';' + \ str(pos_Orig_1) + ';' + str(pos_Orig_2) M_Helfer.txt2File(logFileName, strstr) # Check if need to stop if len(pos_1) == len(Comp_1): Run_1 = False if len(pos_2) == len(Comp_2): Run_2 = False return pos_1, pos_2, GoodnessVal
def match(Netz_0, Netz_1, compName, threshold, multiSelect, funcs, numFuncs=2): """ Main function that matches positions of component from different sources/.. to each other, by using different functions that the user specifies. \n.. comments: Input: Netz_0 Instance of first network netz Netz_1 Instance of second network netz compName string containing compnent lable threshold overall threshold for selection between points, value between 0 and 100 funcs functions, that the user wants to use to find the match Return: pos_match_Netz_0 ordered list of positions, in respect of Netz_0, that have been linked with positions from Netz_1 pos_match_Netz_1 ordered list of positions, in respect of Netz_1, that have been linked with positions from Netz_0 pos_add_Netz_1 list of positions of Netz_1, that need to be added to Netz_0 pos_add_Netz_0 list of positions of Netz_0, for which a corresponding element was not found in Netz_1 Example: [pos_match_Netz_0, pos_match_Netz_1, pos_add_Netz_1, pos_add_Netz_0] = M_Matching.match( Netz_0, Netz_1, compName = 'LNGs', threshold = 80, funcs = (lambda comp_0, comp_1: M_Matching.getMatch_Names(comp_0, comp_1), lambda comp_0, comp_1: M_Matching.getMatch_LatLong(comp_0, comp_1, 50000) )) """ # Initialization pos_match_Netz_0 = [] pos_match_Netz_1 = [] pos_add_Netz_1 = [] pos_add_Netz_0 = [] num_comp_Netz_0 = len(Netz_0.__dict__[compName]) num_comp_Netz_1 = len(Netz_1.__dict__[compName]) # creating of a dummy matrix of size of the number of elements from both data set. goodness_Matrix = M_Helfer.get_NameMatrix_Fuzzy( [str(x) for x in range(len(Netz_0.__dict__[compName]))], [str(y) for y in range(len(Netz_1.__dict__[compName]))]) if numFuncs == 1: goodness_Matrix_2 = copy.deepcopy(goodness_Matrix) else: goodness_Matrix_2 = [ copy.deepcopy(goodness_Matrix), copy.deepcopy(goodness_Matrix) ] # loops that goes through each combination of component pairs from both data # sets and calculating a goodness value, with values between 0 and 100, # based on the sum of the functions supplied by the user if numFuncs == 1: for ii in range(len(Netz_0.__dict__[compName])): # going through each element of Netz_0 comp_0 = Netz_0.__dict__[compName][ii] # going through each element of Netz_1 for jj in range(len(Netz_1.__dict__[compName])): comp_1 = Netz_1.__dict__[compName][jj] # getting goodness over all functions goodness = funcs(comp_0, comp_1) goodness_Matrix_2[ii][jj] = goodness goodness_Matrix[ii][jj] = goodness + goodness_Matrix_2[ii][jj] else: for ii in range(len(Netz_0.__dict__[compName])): # going through each element of Netz_0 comp_0 = Netz_0.__dict__[compName][ii] # going through each element of Netz_1 for jj in range(len(Netz_1.__dict__[compName])): comp_1 = Netz_1.__dict__[compName][jj] # getting goodness over all functions goodness = 0 ff = 0 for func in funcs: goodness_Matrix_2[ff][ii][jj] = func(comp_0, comp_1) goodness = goodness + goodness_Matrix_2[ff][ii][jj] ff = ff + 1 goodness = goodness / len(funcs) # goodness = sum(func(comp_0, comp_1) for func in funcs) / len(funcs)` # writing goodness value to matrix. goodness_Matrix[ii][jj] = goodness #goodness_Matrix_Const = copy.deepcopy(goodness_Matrix) # now finding the best matching pairs, so looking at the goodness_Matrix and # starting off by selecting the one with the largest goodness, removing this pair # from the options and then looking for the next highest goodness value, and # removing this pair, and then repeating this till the goodness value dropps # below the threshold supplied by user. while True: # finding largest goodness vlue [pos_0, pos_1] = M_FindPos.find_pos_ConditionInMatrix(goodness_Matrix, 'max') # check if gooddness value still above threshold value if goodness_Matrix[pos_0][pos_1] > threshold: # Keeping the positions thare are best pos_match_Netz_0.append(pos_0) pos_match_Netz_1.append(pos_1) # removing pairs from options, by setting values to -inf for xx in range(num_comp_Netz_0): goodness_Matrix[xx][pos_1] = -np.inf if multiSelect != True: for yy in range(num_comp_Netz_1): goodness_Matrix[pos_0][yy] = -np.inf # if next goodnes value is smaller than threshold or none left, then # creating of further return parameters and then leaving function elif (goodness_Matrix[pos_0][pos_1] == -np.inf) or (goodness_Matrix[pos_0][pos_1] <= threshold): temp_1 = [ item for item in range(num_comp_Netz_1) if item not in pos_match_Netz_1 ] temp_0 = [ item for item in range(num_comp_Netz_0) if item not in pos_match_Netz_0 ] for wert in temp_1: pos_add_Netz_1.append(wert) for wert in temp_0: pos_add_Netz_0.append(wert) break return [pos_match_Netz_0, pos_add_Netz_0, pos_match_Netz_1, pos_add_Netz_1]