Changeset 1306 for framspy/FramsticksLib.py
- Timestamp:
- 05/13/24 03:59:10 (12 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksLib.py
r1295 r1306 1 1 from typing import List # to be able to specify a type hint of list(something) 2 from enum import Enum, auto, unique 2 3 import json 3 4 import sys, os … … 5 6 import numpy as np 6 7 import frams 8 9 10 @unique 11 class DissimMethod(Enum): # values assigned to fields are irrelevant, hence auto() 12 GENE_LEVENSHTEIN = auto() # genetic Levenshtein distance 13 PHENE_STRUCT_GREEDY = auto() # phenetic, graph structure, fast but approximate 14 PHENE_STRUCT_OPTIM = auto() # phenetic, graph structure, slower for complex creatures but optimal 15 PHENE_DESCRIPTORS = auto() # phenetic, shape descriptors 16 PHENE_DENSITY_COUNT = auto() # phenetic, density distribution, count of samples 17 PHENE_DENSITY_FREQ = auto() # phenetic, density distribution, frequency of count of samples 18 FITNESS = auto() # fitness value 19 7 20 8 21 … … 67 80 for simfile in self.EVALUATION_SETTINGS_FILE: 68 81 ec = frams.MessageCatcher.new() # catch potential errors, warnings, messages - just to detect if there are ERRORs 69 ec.store = 2 ;# store all, because they are caught by MessageCatcher and will not appear in output (which we want)82 ec.store = 2 # store all, because they are caught by MessageCatcher and will not appear in output (which we want) 70 83 frams.Simulator.ximport(simfile, 4 + 8 + 16) 71 84 ec.close() … … 75 88 76 89 77 def getSimplest(self, genetic_format ) -> str:90 def getSimplest(self, genetic_format: str) -> str: 78 91 return frams.GenMan.getSimplest(genetic_format).genotype._string() 79 92 … … 134 147 if not self.PRINT_FRAMSTICKS_OUTPUT: 135 148 ec = frams.MessageCatcher.new() # mute potential errors, warnings, messages 136 ec.store = 2 ;# store all, because they are caught by MessageCatcher and will not appear in output149 ec.store = 2 # store all, because they are caught by MessageCatcher and will not appear in output 137 150 138 151 frams.GenePools[0].clear() … … 192 205 193 206 194 def dissimilarity(self, genotype_list: List[str], method: int) -> np.ndarray:195 """ 196 :param method : -1 = genetic Levenshtein distance; 0, 1, 2 = phenetic dissimilarity (SimilMeasureGreedy, SimilMeasureHungarian, SimilMeasureDistribution); -2, -3 = phenetic density distribution (count, frequency).207 def dissimilarity(self, genotype_list: List[str], method: DissimMethod) -> np.ndarray: 208 """ 209 :param method, see DissimMethod. 197 210 :return: A square array with dissimilarities of each pair of genotypes. 198 211 """ … … 206 219 square_matrix = np.zeros((n, n)) 207 220 208 if method in ( 0, 1, 2): # Framsticks phenetic dissimilarity methods209 frams.SimilMeasure.simil_type = method221 if method in (DissimMethod.PHENE_STRUCT_GREEDY, DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS): # Framsticks phenetic dissimilarity methods 222 frams.SimilMeasure.simil_type = 0 if method == DissimMethod.PHENE_STRUCT_GREEDY else 1 if method == DissimMethod.PHENE_STRUCT_OPTIM else 2 210 223 genos = [] # prepare an array of Geno objects so that we don't need to convert raw strings to Geno objects all the time in loops 211 224 for g in genotype_list: … … 215 228 for j in range(n): # maybe calculate only one triangle if you really need a 2x speedup 216 229 square_matrix[i][j] = frams_evaluateDistance(genos[i], genos[j])._double() 217 elif method == -1:230 elif method == DissimMethod.GENE_LEVENSHTEIN: 218 231 import Levenshtein 219 232 for i in range(n): 220 233 for j in range(n): # maybe calculate only one triangle if you really need a 2x speedup 221 234 square_matrix[i][j] = Levenshtein.distance(genotype_list[i], genotype_list[j]) 222 elif method in ( -2, -3):235 elif method in (DissimMethod.PHENE_DENSITY_COUNT, DissimMethod.PHENE_DENSITY_FREQ): 223 236 if self.dissim_measure_density_distribution is None: 224 237 from dissimilarity.density_distribution import DensityDistribution 225 238 self.dissim_measure_density_distribution = DensityDistribution(frams) 226 self.dissim_measure_density_distribution.frequency = (method == -3)239 self.dissim_measure_density_distribution.frequency = (method == DissimMethod.PHENE_DENSITY_FREQ) 227 240 square_matrix = self.dissim_measure_density_distribution.getDissimilarityMatrix(genotype_list) 228 241 else: 229 raise ValueError("Don't know what to do with dissimilarity method = % d" % method)242 raise ValueError("Don't know what to do with dissimilarity method = %s" % method) 230 243 231 244 for i in range(n): … … 381 394 offspring = framsLib.crossOver(parent1, parent2) 382 395 print("\tCrossover (Offspring):", offspring) 383 print('\tDissimilarity of Parent1 and Offspring:', framsLib.dissimilarity([parent1, offspring], 1)[0, 1])396 print('\tDissimilarity of Parent1 and Offspring:', framsLib.dissimilarity([parent1, offspring], DissimMethod.PHENE_STRUCT_OPTIM)[0, 1]) 384 397 print('\tPerformance of Offspring:', framsLib.evaluate([offspring])) 385 398 print('\tValidity (genetic) of Parent1, Parent 2, and Offspring:', framsLib.isValid([parent1, parent2, offspring]))
Note: See TracChangeset
for help on using the changeset viewer.