Changeset 1102 for framspy


Ignore:
Timestamp:
03/10/21 21:13:26 (4 years ago)
Author:
Maciej Komosinski
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/FramsticksLib.py

    r1101 r1102  
    138138                for i in range(n):
    139139                        assert square_matrix[i][i] == 0, "Not a correct dissimilarity matrix, diagonal expected to be 0"
    140                 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
     140                non_symmetric_diff = square_matrix - square_matrix.T
     141                non_symmetric_count = np.count_nonzero(non_symmetric_diff)
     142                if non_symmetric_count > 0:
     143                        non_symmetric_diff_abs = np.abs(non_symmetric_diff)
     144                        max_pos1d = np.argmax(non_symmetric_diff_abs)  # location of largest discrepancy
     145                        max_pos2d_XY = np.unravel_index(max_pos1d, non_symmetric_diff_abs.shape)  # 2D coordinates of largest discrepancy
     146                        max_pos2d_YX = max_pos2d_XY[1], max_pos2d_XY[0]  # 2D coordinates of largest discrepancy mirror
     147                        worst_guy_XY = square_matrix[max_pos2d_XY]  # this distance and the other below (its mirror) are most different
     148                        worst_guy_YX = square_matrix[max_pos2d_YX]
     149                        print("[WARN] Dissimilarity matrix: expecting symmetry, but %g out of %d pairs were asymmetrical, max difference was %g (%g %%)" %
     150                              (non_symmetric_count / 2,
     151                               n * (n - 1) / 2,
     152                               non_symmetric_diff_abs[max_pos2d_XY],
     153                               non_symmetric_diff_abs[max_pos2d_XY] * 100 / ((worst_guy_XY + worst_guy_YX) / 2)))  # max diff is not necessarily max %
    141154                return square_matrix
    142155
     
    147160                for g in genotype_list:
    148161                        valid.append(frams.Geno.newFromString(g).is_valid._int() == 1)
    149                 assert len(genotype_list) == len(valid), "Submitted %d genotypes, received %d validity values" % (len(genotype_list), len(valid))
     162                assert len(genotype_list) == len(valid), "Tested %d genotypes, received %d validity values" % (len(genotype_list), len(valid))
    150163                return valid
    151164
     
    175188
    176189        parsed_args = parseArguments()
    177         framsDLL = FramsticksLib(parsed_args.path, parsed_args.lib, parsed_args.simsettings)
     190        framsLib = FramsticksLib(parsed_args.path, parsed_args.lib, parsed_args.simsettings)
    178191
    179192        print("Sending a direct command to Framsticks library that calculates \"4\"+2 yields", frams.Simulator.eval("return \"4\"+2;"))
    180193
    181         simplest = framsDLL.getSimplest('1' if parsed_args.genformat is None else parsed_args.genformat)
     194        simplest = framsLib.getSimplest('1' if parsed_args.genformat is None else parsed_args.genformat)
    182195        print("\tSimplest genotype:", simplest)
    183         parent1 = framsDLL.mutate([simplest])[0]
     196        parent1 = framsLib.mutate([simplest])[0]
    184197        parent2 = parent1
    185198        MUTATE_COUNT = 10
    186199        for x in range(MUTATE_COUNT):  # example of a chain of 10 mutations
    187                 parent2 = framsDLL.mutate([parent2])[0]
     200                parent2 = framsLib.mutate([parent2])[0]
    188201        print("\tParent1 (mutated simplest):", parent1)
    189202        print("\tParent2 (Parent1 mutated %d times):" % MUTATE_COUNT, parent2)
    190         offspring = framsDLL.crossOver(parent1, parent2)
     203        offspring = framsLib.crossOver(parent1, parent2)
    191204        print("\tCrossover (Offspring):", offspring)
    192         print('\tDissimilarity of Parent1 and Offspring:', framsDLL.dissimilarity([parent1, offspring])[0, 1])
    193         print('\tPerformance of Offspring:', framsDLL.evaluate([offspring]))
    194         print('\tValidity of Parent1, Parent 2, and Offspring:', framsDLL.isValid([parent1, parent2, offspring]))
     205        print('\tDissimilarity of Parent1 and Offspring:', framsLib.dissimilarity([parent1, offspring])[0, 1])
     206        print('\tPerformance of Offspring:', framsLib.evaluate([offspring]))
     207        print('\tValidity of Parent1, Parent 2, and Offspring:', framsLib.isValid([parent1, parent2, offspring]))
Note: See TracChangeset for help on using the changeset viewer.