# Changeset 1102

Ignore:
Timestamp:
03/10/21 21:13:26 (3 years ago)
Message:

Do not fail when the dissimilarity matrix is not symmetric, print a warning message instead

File:
1 edited

Unmodified
Added
Removed
• ## framspy/FramsticksLib.py

 r1101 for i in range(n): assert square_matrix[i][i] == 0, "Not a correct dissimilarity matrix, diagonal expected to be 0" assert (square_matrix == square_matrix.T).all(), "Probably not a correct dissimilarity matrix, expecting symmetry, verify this"  # could introduce tolerance in comparison (e.g. class field DISSIMIL_DIFF_TOLERANCE=10^-5) so that miniscule differences do not fail here non_symmetric_diff = square_matrix - square_matrix.T non_symmetric_count = np.count_nonzero(non_symmetric_diff) if non_symmetric_count > 0: non_symmetric_diff_abs = np.abs(non_symmetric_diff) max_pos1d = np.argmax(non_symmetric_diff_abs)  # location of largest discrepancy max_pos2d_XY = np.unravel_index(max_pos1d, non_symmetric_diff_abs.shape)  # 2D coordinates of largest discrepancy max_pos2d_YX = max_pos2d_XY[1], max_pos2d_XY[0]  # 2D coordinates of largest discrepancy mirror worst_guy_XY = square_matrix[max_pos2d_XY]  # this distance and the other below (its mirror) are most different worst_guy_YX = square_matrix[max_pos2d_YX] print("[WARN] Dissimilarity matrix: expecting symmetry, but %g out of %d pairs were asymmetrical, max difference was %g (%g %%)" % (non_symmetric_count / 2, n * (n - 1) / 2, non_symmetric_diff_abs[max_pos2d_XY], non_symmetric_diff_abs[max_pos2d_XY] * 100 / ((worst_guy_XY + worst_guy_YX) / 2)))  # max diff is not necessarily max % return square_matrix for g in genotype_list: valid.append(frams.Geno.newFromString(g).is_valid._int() == 1) assert len(genotype_list) == len(valid), "Submitted %d genotypes, received %d validity values" % (len(genotype_list), len(valid)) assert len(genotype_list) == len(valid), "Tested %d genotypes, received %d validity values" % (len(genotype_list), len(valid)) return valid parsed_args = parseArguments() framsDLL = FramsticksLib(parsed_args.path, parsed_args.lib, parsed_args.simsettings) framsLib = FramsticksLib(parsed_args.path, parsed_args.lib, parsed_args.simsettings) print("Sending a direct command to Framsticks library that calculates \"4\"+2 yields", frams.Simulator.eval("return \"4\"+2;")) simplest = framsDLL.getSimplest('1' if parsed_args.genformat is None else parsed_args.genformat) simplest = framsLib.getSimplest('1' if parsed_args.genformat is None else parsed_args.genformat) print("\tSimplest genotype:", simplest) parent1 = framsDLL.mutate([simplest])[0] parent1 = framsLib.mutate([simplest])[0] parent2 = parent1 MUTATE_COUNT = 10 for x in range(MUTATE_COUNT):  # example of a chain of 10 mutations parent2 = framsDLL.mutate([parent2])[0] parent2 = framsLib.mutate([parent2])[0] print("\tParent1 (mutated simplest):", parent1) print("\tParent2 (Parent1 mutated %d times):" % MUTATE_COUNT, parent2) offspring = framsDLL.crossOver(parent1, parent2) offspring = framsLib.crossOver(parent1, parent2) print("\tCrossover (Offspring):", offspring) print('\tDissimilarity of Parent1 and Offspring:', framsDLL.dissimilarity([parent1, offspring])[0, 1]) print('\tPerformance of Offspring:', framsDLL.evaluate([offspring])) print('\tValidity of Parent1, Parent 2, and Offspring:', framsDLL.isValid([parent1, parent2, offspring])) print('\tDissimilarity of Parent1 and Offspring:', framsLib.dissimilarity([parent1, offspring])[0, 1]) print('\tPerformance of Offspring:', framsLib.evaluate([offspring])) print('\tValidity of Parent1, Parent 2, and Offspring:', framsLib.isValid([parent1, parent2, offspring]))
Note: See TracChangeset for help on using the changeset viewer.