Skip to content

Commit

Permalink
Merge pull request #286 from ezCGP/feature/simgan-support
Browse files Browse the repository at this point in the history
Feature/simgan support
  • Loading branch information
roddtalebi committed May 1, 2022
2 parents 446be48 + 312cb05 commit 3290b2d
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 383 deletions.
6 changes: 3 additions & 3 deletions codes/block_definitions/utilities/operators_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ def feature_extraction(input_shapes, *args):
NOTE: an idea was to make the output of this method to be a 2nd input into the
discriminator block instead of a primitive of that block
'''
from misc import features
class FeatureExtractor(MimicPyTorchModule, features.FeatureExtractor):
from codes.utilities import simgan_feature_eval
class FeatureExtractor(MimicPyTorchModule, simgan_feature_eval.FeatureExtractor):
def __init__(self, input_shapes, *args):
'''
if input_shapes[0] doesn't 92 features then the code will
Expand All @@ -563,7 +563,7 @@ def __init__(self, input_shapes, *args):
fake_shape = input_shapes[0]
fake_shape = fake_shape[:-1] + (92,)
MimicPyTorchModule.__init__(self, [fake_shape], ftn=None)
features.FeatureExtractor.__init__(self, *args)
simgan_feature_eval.FeatureExtractor.__init__(self, *args)

def get_out_shape(self):
return (self.input_shapes[0][0], self.num_features)
Expand Down
4 changes: 2 additions & 2 deletions codes/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def build_population(self,
'''
my_population = PopulationDefinition()

if (node_rank==0) and (problem.hall_of_fame_size is not None):
my_population.setup_hall_of_fame(problem.hall_of_fame_size)
if (node_rank==0) and (problem.hall_of_fame_flag):
my_population.setup_hall_of_fame()

if len(problem.genome_seeds) == 0:
genome_seeds = []
Expand Down
25 changes: 19 additions & 6 deletions codes/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self):
#self.pop_size = population_size #moved to problem.pop_size
self.population = []
self.hall_of_fame = None
self.hof_ids = []


def __setitem__(self, node_index, value):
Expand Down Expand Up @@ -93,9 +94,13 @@ def merge_subpopulations(self, subpops):
ezLogging.info("Combined %i sub populations into a single population" % (len(subpops)))


def setup_hall_of_fame(self, maxsize):
def setup_hall_of_fame(self):
'''
https://deap.readthedocs.io/en/master/api/tools.html#deap.tools.HallOfFame
https://deap.readthedocs.io/en/master/api/tools.html#deap.tools.ParetoFront
https://github.com/DEAP/deap/blob/master/deap/tools/support.py#L591
NOTE using ParetoFront which inherits from HallOfFame. See Issue #285.
But tldr is that HallOfFame only works on single objective and ParetoFront
works for multiobjective and no maxsize which should also addess Issue #280.
letting hall_of_fame use to be optional
'''
Expand All @@ -110,12 +115,13 @@ def similarity_equation(a, b):
'''
if a.id == b.id:
return True
elif a.fitness.values == b.fitness_values:
return True
else:
return False

self.hall_of_fame = deap.tools.HallOfFame(maxsize=maxsize,
similar=similarity_equation)
ezLogging.debug("Established 'Hall of Fame' with maxsize %i" % maxsize)
self.hall_of_fame = deap.tools.ParetoFront(similar=similarity_equation)
ezLogging.debug("Established Hall of Fame")


def update_hall_of_fame(self):
Expand All @@ -127,10 +133,17 @@ def update_hall_of_fame(self):
alive_population = []
for indiv in self.population:
if not indiv.dead:
alive_population.append(indiv)
if indiv.id not in self.hof_ids:
alive_population.append(indiv)

self.hall_of_fame.update(alive_population)
ezLogging.debug("Updated Hall of Fame to size %i" % (len(self.hall_of_fame.items)))

# keep a running list of HOF id's just to keep handy...won't be too much extra space
self.hof_ids = []
for indiv in self.hall_of_fame.items:
self.hof_ids.append(indiv.id)


def get_pareto_front(self, use_hall_of_fame=False, first_front_only=False):
'''
Expand Down
26 changes: 25 additions & 1 deletion codes/utilities/simgan_feature_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
import torch
from scipy.stats import ks_2samp, wasserstein_distance, ttest_ind


### sys relative to root dir
import sys
from os.path import dirname, realpath
sys.path.append(dirname(dirname(dirname(realpath(__file__)))))

### absolute imports wrt root
from codes.utilities.custom_logging import ezLogging


def calc_feature_distances(refiners, validation_data, device):
'''
TODO...get the source from the room
Expand Down Expand Up @@ -726,26 +736,38 @@ def __init__(self, use_phdif=True, use_phratio=True, use_pdist=True, use_thdiff=
self.use_changes = use_changes
self.use_roughness = use_roughness
self.use_th = use_th
self.feature_names = []

self.num_features = 0
if use_phdif:
self.num_features += 1
self.feature_names.append("phdif")
if use_phratio:
self.num_features += 1
self.feature_names.append("phratio")
if use_pdist:
self.num_features += 1
self.feature_names.append("pdist")
if use_thdiff:
self.num_features += 2
self.feature_names.append("thdiff0")
self.feature_names.append("thdiff1")
if use_thdist:
self.num_features += 2
self.feature_names.append("thdist0")
self.feature_names.append("thdist1")
if use_area:
self.num_features += 1
self.feature_names.append("area")
if use_changes:
self.num_features += 1
self.feature_names.append("changes")
if use_roughness:
self.num_features += 1
self.feature_names.append("roughness")
if use_th:
self.num_features += 1
self.feature_names.append("th")

def get_features(self, dataset):
'''
Expand All @@ -755,6 +777,8 @@ def get_features(self, dataset):
Returns:
features (ndarray): MxN' numpy array of N' features for M signals
(Rodd Added) - LIES! it's actually N'xM so we have to transpose whatever gets returned
...could be fixed if np.expand_dims axis is changed from 0 to 1 I think
'''
features = []
if self.use_phdif:
Expand Down Expand Up @@ -823,7 +847,7 @@ def get_peak_distance(data, lrs=10, lre=40, rrs=40, rre=80):
distance (ndarray): numpy array of the distances between the left and right peaks
'''
left_peak_inds = get_peaks(data, lrs, lre)
right_peak_inds = get_peaks(data, rrs, rre)
right_peak_inds = get_peaks(data, rrs, rre)
distances = right_peak_inds - left_peak_inds
return distances

Expand Down

0 comments on commit 3290b2d

Please sign in to comment.