Changeset 971 for framspy/FramsticksCLI.py
 Timestamp:
 06/30/20 14:42:27 (3 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

framspy/FramsticksCLI.py
r962 r971 1 from math import sqrt 1 2 from subprocess import Popen, PIPE, check_output 2 3 from enum import Enum 3 from typing import List 4 from typing import List # to be able to specify a type hint of list(something) 4 5 from itertools import count # for tracking multiple instances 5 6 import json … … 216 217 217 218 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"]) 220 225 with open(files[1]) as f: 221 226 dissimilarity_matrix = np.genfromtxt(f, dtype=np.float64, comments='#', encoding=None, delimiter='\t') 222 227 # We would like to skip column #1 while reading and read everything else, but... https://stackoverflow.com/questions/36091686/excludecolumnsfromgenfromtxtwithnumpy 223 228 # 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 231 238 232 239 … … 281 288 offspring = framsCLI.crossOver(parent1, parent2) 282 289 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]) 284 291 print('\tPerformance of Offspring:', framsCLI.evaluate(offspring)) 285 292 print('\tValidity of Offspring:', framsCLI.isValid(offspring))
Note: See TracChangeset
for help on using the changeset viewer.