Changeset 971


Ignore:
Timestamp:
06/30/20 14:42:27 (4 years ago)
Author:
Maciej Komosinski
Message:

Added support for calculation of dissimilarity of more than two genotypes at once (returns a full square matrix)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/FramsticksCLI.py

    r962 r971  
     1from math import sqrt
    12from subprocess import Popen, PIPE, check_output
    23from enum import Enum
    3 from typing import List
     4from typing import List  # to be able to specify a type hint of list(something)
    45from itertools import count  # for tracking multiple instances
    56import json
     
    216217
    217218
    218         def dissimilarity(self, genotype1: str, genotype2: str) -> float:
    219                 files = self.__runCommand(self.DISSIMIL_CMD, [genotype1, genotype2], self.DISSIMIL_FILE, self.GENO_SAVE_FILE_FORMAT["NATIVEFRAMS"])
     219        def dissimilarity(self, genotype_list: List[str]) -> np.ndarray:
     220                """
     221                Returns:
     222                        A square array with dissimilarities of each pair of genotypes.
     223                """
     224                files = self.__runCommand(self.DISSIMIL_CMD, genotype_list, self.DISSIMIL_FILE, self.GENO_SAVE_FILE_FORMAT["NATIVEFRAMS"])
    220225                with open(files[-1]) as f:
    221226                        dissimilarity_matrix = np.genfromtxt(f, dtype=np.float64, comments='#', encoding=None, delimiter='\t')
    222227                # We would like to skip column #1 while reading and read everything else, but... https://stackoverflow.com/questions/36091686/exclude-columns-from-genfromtxt-with-numpy
    223228                # This would be too complicated, so strings (names) in column #1 become NaN as floats (unless they accidentally are valid numbers) - not great, not terrible
    224                 EXPECTED_SHAPE = (2, 4)
    225                 assert dissimilarity_matrix.shape == EXPECTED_SHAPE, f"Not a correct dissimilarity matrix, expected {EXPECTED_SHAPE} "
    226                 for i in range(len(dissimilarity_matrix)):
    227                         assert dissimilarity_matrix[i][i + 2] == 0, "Not a correct dissimilarity matrix, diagonal expected to be 0"
    228                 assert dissimilarity_matrix[0][3] == dissimilarity_matrix[1][2], "Probably not a correct dissimilarity matrix, expecting symmetry, verify this"
    229                 self.__cleanUpCommandResults(files)
    230                 return dissimilarity_matrix[0][3]
     229                square_matrix = dissimilarity_matrix[:, 2:]  # get rid of two first columns (fitness and name)
     230                EXPECTED_SHAPE = (len(genotype_list), len(genotype_list))
     231                # print(square_matrix)
     232                assert square_matrix.shape == EXPECTED_SHAPE, f"Not a correct dissimilarity matrix, expected {EXPECTED_SHAPE} "
     233                for i in range(len(square_matrix)):
     234                        assert square_matrix[i][i] == 0, "Not a correct dissimilarity matrix, diagonal expected to be 0"
     235                assert (square_matrix == square_matrix.T).all(), "Probably not a correct dissimilarity matrix, expecting symmetry, verify this"
     236                self.__cleanUpCommandResults(files)
     237                return square_matrix
    231238
    232239
     
    281288        offspring = framsCLI.crossOver(parent1, parent2)
    282289        print("\tCrossover (Offspring):", offspring)
    283         print('\tDissimilarity of Parent1 and Offspring:', framsCLI.dissimilarity(offspring, parent1))
     290        print('\tDissimilarity of Parent1 and Offspring:', framsCLI.dissimilarity([parent1, offspring])[0, 1])
    284291        print('\tPerformance of Offspring:', framsCLI.evaluate(offspring))
    285292        print('\tValidity of Offspring:', framsCLI.isValid(offspring))
Note: See TracChangeset for help on using the changeset viewer.