Skip to content

Commit

Permalink
Delinting and fixing last index
Browse files Browse the repository at this point in the history
  • Loading branch information
armenbeck committed Aug 11, 2022
1 parent 6dd7124 commit 18f4aff
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 101 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ paddy.egg-info

.cache/

paddy_test_2.py
23 changes: 11 additions & 12 deletions paddy/Default_Numerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,20 @@ def trig_inter(x_list, seed):
s_x = []
while counter < len(x_list):
###this evaluates over x
n = 1
number = 1
xn_out = []
while n <= ((len(seed)-1)/2.0):
while number <= ((len(seed)-1)/2.0):
###evaluates sums of cos and sin
alpha = ((n*2)-1)
beta = (n*2)
alpha = ((number*2)-1)
beta = (number*2)
temp_y = ((
seed[alpha][0])*(math.cos((n*x_list[counter]))))+(
(seed[beta][0])*(math.sin((n*x_list[counter]))))
seed[alpha][0])*(math.cos((number*x_list[counter]))))+(
(seed[beta][0])*(math.sin((number*x_list[counter]))))
xn_out.append(temp_y)
n = n+1
number += 1
xn_out.append(seed[0][0])
s_x.append(sum(xn_out))
counter = counter + 1
counter += 1
return s_x


Expand Down Expand Up @@ -259,18 +259,16 @@ def __init__(self, error_func=mse_func,
self.error_func = error_func
self.t_func = t_func
self.f_func = f_func
self.x, self.answer = self.t_func()
self.x_vals, self.answer = self.t_func()

def eval(self, seed):
"""Method of `eval_numeric`.
"""
seed = seed
y_val = self.f_func(self.x, seed)
y_val = self.f_func(self.x_vals, seed)
#made negative for maximization problem
error = -self.error_func(self.answer, y_val)
return error


class Polynomial(object):
"""Generate paddy space that is apt for polynomial fitting.
Expand All @@ -296,3 +294,4 @@ def __init__(self, length, scope, gausian_type, normalization=True,
normalization=normalization)
)
counter += 1

16 changes: 7 additions & 9 deletions paddy/Paddy_Parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# Authors: Armen Beck & Jonathan Fine
# License: BSD

import math
import random
import numpy as np

Expand Down Expand Up @@ -116,7 +115,7 @@ def __init__(self, param_range, param_type, limits,
self.normalization = normalization
if param_range[0] > param_range[1]:
raise PaddyParamError(PARAM_RANGE_ORDER_ERROR)
if limits != None:
if limits is not None:
if limits[0] > limits[1] or limits[1] < limits[0]:
raise PaddyParamError(LIMIT_ORDER_ERROR)
if limits[0] == limits[1]:
Expand All @@ -129,10 +128,10 @@ def __init__(self, param_range, param_type, limits,
float('inf') in limits or -float('inf') in limits):
if normalization:
raise PaddyParamError(INF_NORM_ERROR)
if gaussian != 'default' and gaussian != 'scaled':
if gaussian not in ('default', 'scaled'):
error = GAUSSIAN_TYPE_ERROR.format(gaussian)
raise PaddyParamError(error)
if param_type != 'integer' and param_type != 'continuous':
if param_type not in ('integer', 'continuous'):
error = PARAM_TYPE_ERROR.format(param_type)
raise PaddyParamError(error)

Expand Down Expand Up @@ -213,10 +212,10 @@ def new_seed_init(self, values):
if not self.normalization:
b[0] = np.random.normal(
loc=values[0], scale=0.2 **(10**values[1]))
if self.limits == None:
if self.limits is None:
if self.gaussian == 'scaled':
b[1] = np.random.normal(loc=values[1], scale=0.2)
elif self.limits != None:
elif self.limits is not None:
b[0] = np.clip(b[0], self.limits[0], self.limits[1])
if self.gaussian == 'scaled':
b[1] = np.random.normal(loc=values[1], scale=0.2)
Expand Down Expand Up @@ -254,6 +253,5 @@ def get_ecludian_values(self, p_vals):
if not self.normalization:
e_val = p_vals[0]
return e_val
else:
e_val = self.norm_p(p_vals[0])
return e_val
e_val = self.norm_p(p_vals[0])
return e_val
76 changes: 34 additions & 42 deletions paddy/Paddy_Runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ class PFARunner(object):
would raise an exception when calling methods of `PFARunner`.
"""

def __init__(self, space, eval_func, rand_seed_number,
yt, Qmax, paddy_type, r, iterations,
error_handling=True):
Expand All @@ -256,20 +255,17 @@ def __init__(self, space, eval_func, rand_seed_number,
if len(_int_params[i]) > 1:
error = CONTAINER_ERROR.format(ipn[i])
raise PaddyParamError(error)
else:
_int_params[i] = _int_params[i][0]
_int_params[i] = _int_params[i][0]
if isinstance(_int_params[i], str):
if not _int_params[i].isdigit():
error = INT_PARAM_ERROR.format(ipn[i], _int_params[i])
raise PaddyParamError(error)
else:
_int_params[i] = float(_int_params[i])
_int_params[i] = float(_int_params[i])
if isinstance(_int_params[i], float):
if not _int_params[i] % 1 == 0:
error = INT_PARAM_ERROR.format(ipn[i], _int_params[i])
raise PaddyParamError(error)
else:
_int_params[i] = int(_int_params[i])
_int_params[i] = int(_int_params[i])
if isinstance(_int_params[i], int):
if _int_params[i] < 1:
error = INT_PARAM_ERROR.format(ipn[i], _int_params[i])
Expand All @@ -278,8 +274,7 @@ def __init__(self, space, eval_func, rand_seed_number,
if len(r) > 1:
error = CONTAINER_ERROR.format('r')
raise PaddyParamError(error)
else:
r = r[0]
r = r[0]
if isinstance(r, str):
try:
r = float(r)
Expand All @@ -293,9 +288,8 @@ def __init__(self, space, eval_func, rand_seed_number,
if len(paddy_type) > 1:
error = CONTAINER_ERROR.format('paddy_type')
raise PaddyParamError(error)
else:
paddy_type = paddy_type[0]
if paddy_type != 'generational' and paddy_type != 'population':
paddy_type = paddy_type[0]
if paddy_type not in ('generational','population'):
error = PADDY_TYPE_ERROR.format(paddy_type)
raise PaddyParamError(error)
(rand_seed_number, yt, Qmax, iterations) = (_int_params[0],
Expand All @@ -305,7 +299,6 @@ def __init__(self, space, eval_func, rand_seed_number,

if rand_seed_number < 4:
raise PaddyParamError(RANDOM_SEED_ERROR)

self.Qmax = Qmax
self.yt = yt
self.yt_prime = yt
Expand All @@ -323,6 +316,7 @@ def __init__(self, space, eval_func, rand_seed_number,
self.paddy_type = paddy_type
self.paddy_counter = 0
self.s = np.array([])
self.S = np.array([])
self.Un = np.array([])
self.iterations = iterations
self.generation_data = {}
Expand All @@ -346,21 +340,21 @@ def random_step(self):
:meth:`run_paddy` : `PFARunner` method that excecutes the paddy field
algorithm.
"""
sc, rs = random_propogation(rand_seed_number=self.rand_seed_number,
self.seed_counter, rand_seeds = random_propogation(
rand_seed_number=self.rand_seed_number,
p_space=self.space,
seed_counter=self.seed_counter)
self.seed_counter = sc
c = 1
for i in rs:
for i in rand_seeds:
self.seed_params.append(i)
self.seed_fitness.append(self.eval_func(i))
if self.verbose != None:
if self.verbose is not None:
if 'status' in self.verbose:
print(str(c)+" of "+str(len(rs))+" seeds", " complete\r",)
print(str(c)+" of "+str(len(rand_seeds))+" seeds", " complete\r",)
c += 1
self.generation_data['0'] = [0, int(self.rand_seed_number)-1]
############Top Gen Seed###########
if self.verbose != None:
if self.verbose is not None:
if 'top_gen' in self.verbose:
best_rand_seed = self.get_top_seed()
print("Best seed(s) during random initiation was:\
Expand Down Expand Up @@ -399,7 +393,7 @@ def sowing_function(self):
for i in range(seed_key_numbers[0], seed_key_numbers[1]+1):
temp_gen_fit.append(self.seed_fitness[i])
self.generation_fitness[str(self.paddy_counter)] = temp_gen_fit
if self.verbose != None:
if self.verbose is not None:
###########Top Gen Seed###########
if 'top_gen' in self.verbose:
print('Top seed in generation:')
Expand Down Expand Up @@ -456,7 +450,7 @@ def sowing_function(self):
(self.Qmax * ((gen_clone[-counter][1] - yt_val)/
float(y_max - yt_val)))])
counter += 1
self.s = np.array(self.s)
self.s = np.array(self.s,dtype='object')
if self.paddy_type == 'population':
pop_clone = []
pop_clone.append([])
Expand All @@ -469,17 +463,17 @@ def sowing_function(self):
self.top_values[str(self.paddy_counter)]['parameters'] = self.seed_params[
np.argmax(self.seed_fitness)]
self.top_values[str(self.paddy_counter)]['seed'] = np.argsort(
np.array(self.seed_fitness))[-1]
np.array(self.seed_fitness,dtype='object'))[-1]
yt_val = population_fitness_keys[-self.yt]
counter = 1
self.s = []
while counter != self.yt + 1:
if yt_val != y_max:
self.s.append([np.argsort(np.array(self.seed_fitness))[-counter],
self.s.append([np.argsort(np.array(self.seed_fitness,dtype='object'))[-counter],
(self.Qmax * ((population_fitness_keys[-counter] -
yt_val) / float(y_max - yt_val)))])
counter += 1
self.s = np.array(self.s)
self.s = np.array(self.s,dtype='object')


def neighbor_counter(self):
Expand Down Expand Up @@ -551,7 +545,7 @@ def neighbor_counter(self):
if distance.euclidean(i[1:], j[1:])-self.r < 0:
n_count += 1
neighbors.append([i[0], n_count])
neighbors = np.array(neighbors)
neighbors = np.array(neighbors,dtype='object')
quantile_value = 0.75
#this will let the paddy run even if there are no neighbors
while all(x < 1 for x in neighbors[:, 1]):
Expand All @@ -569,20 +563,20 @@ def neighbor_counter(self):
d_list, quantile_value) < 0):
n_count += 1
neighbors.append([i[0], n_count])
neighbors = np.array(neighbors)
neighbors = np.array(neighbors,dtype='object')
quantile_value -= 0.05
n_max = max(neighbors[:, 1])
self.Un = []
for i in neighbors:
self.Un.append([i[0], math.exp((i[1]/float(n_max))-1)])
self.Un = np.array(self.Un)
self.Un = np.array(self.Un,dtype='object')
self.S = []
c = 0
while c < len(neighbors):
self.S.append([neighbors[c, 0],
np.round(self.Un[c, 1]*self.s[c, 1])])
c += 1
self.S = np.array(self.S)
self.S = np.array(self.S,dtype='object')

def new_propogation(self):
"""Generate new seeds and evaluate.
Expand All @@ -603,7 +597,7 @@ def new_propogation(self):
iteration_seed_limits = [self.seed_counter, self.seed_counter+S_len-1]
S_len_counter = 1
#Status#
if self.verbose != None:
if self.verbose is not None:
if 'status' in self.verbose:
print("Starting Iteration " +str(self.paddy_counter)+" of "
+str(self.iterations))
Expand All @@ -623,7 +617,7 @@ def new_propogation(self):
self.seed_fitness.append(self.eval_func(
self.seed_params[self.seed_counter]))
#Status#
if self.verbose != None:
if self.verbose is not None:
if 'status' in self.verbose:
print(str(S_len_counter)+" of "+str(S_len)
+" seeds", " complete\r",)
Expand Down Expand Up @@ -725,12 +719,12 @@ def run_paddy(self, file_name=None, verbose=None):
if self.verbose is None:
self.verbose = {}
#needs error handling for file_name
if file_name != None:
if file_name is not None:
if not isinstance(file_name, str):
raise PaddyRunnerError(PADDY_FILE_ERROR)
if not self.recover:
self.random_step()
if self.file_name != None:
if self.file_name is not None:
self.save_paddy()
while self.paddy_counter < self.iterations:
self.sowing_function()
Expand All @@ -742,7 +736,7 @@ def run_paddy(self, file_name=None, verbose=None):
self.neighbor_counter()
self.paddy_counter += 1
self.new_propogation()
if self.file_name != None:
if self.file_name is not None:
self.save_paddy()
self.top_values[str(self.paddy_counter)] = {}
seed_key_numbers = self.generation_data[str(self.paddy_counter)]
Expand All @@ -754,7 +748,7 @@ def run_paddy(self, file_name=None, verbose=None):
gen_clone = []
for i in range(seed_key_numbers[0], seed_key_numbers[1]+1):
gen_clone.append([i, self.seed_fitness[i]])
gen_clone = np.array(gen_clone)
gen_clone = np.array(gen_clone,dtype='object')
gen_clone = gen_clone[gen_clone[:, 1].argsort()]
y_max = gen_clone[-1][1]
self.top_values[str(self.paddy_counter)] = {}
Expand All @@ -770,7 +764,7 @@ def run_paddy(self, file_name=None, verbose=None):
self.top_values[str(self.paddy_counter)]['parameters'] = (
self.seed_params[(np.argmax(self.seed_fitness))])
self.top_values[str(self.paddy_counter)]['seed'] = (
np.argsort(np.array(self.seed_fitness))[-1])
np.argsort(np.array(self.seed_fitness,dtype='object'))[-1])
###########Top Gen Seed###########
#this prints the seed(s) id's and their fitness followed by parameters
if 'top_gen' in self.verbose:
Expand Down Expand Up @@ -800,7 +794,7 @@ def run_paddy(self, file_name=None, verbose=None):
single_param_print(self.seed_params, int((i).split('_')[1]))
##################################
print("paddy is done!")
if self.file_name != None:
if self.file_name is not None:
self.save_paddy()

def paddy_plot_and_print(self, verbose=None, figure_name=None):
Expand Down Expand Up @@ -872,7 +866,6 @@ def paddy_plot_and_print(self, verbose=None, figure_name=None):
for comand in verbose:
if paddy_plot_and_write_comands.issuperset({comand}):
is_running = True
pass
else:
print(str(comand)+' is not a valid input for plot and print')
return
Expand Down Expand Up @@ -931,7 +924,6 @@ def save_paddy(self, new_file_name=None):
PaddyRunnerError(error)
else:
self.file_name = new_file_name

with open('{0}.pickle'.format(self.file_name), 'wb') as handle:
pickle.dump(self, handle, protocol=pickle.HIGHEST_PROTOCOL)
handle.close()
Expand Down Expand Up @@ -1024,7 +1016,7 @@ def extend_paddy(self, new_iterations,
if new_iterations < 1 or new_iterations % 1 != 0:
raise PaddyRunnerError(EXTENSION_ERROR)
self.recover = True
if new_verbose != None:
if new_verbose is not None:
self.verbose = new_verbose
if isinstance(new_file_name, (str, int)):
self.file_name = new_file_name
Expand Down Expand Up @@ -1119,10 +1111,10 @@ def get_generation_top_seed(self, counter=None, verbose=False):
print('multiple seeds of maximum fitness value:'+str(max_fit))
temp_names = []
if counter is None:
tg = self.generation_data[str(self.paddy_counter)]
gen_seeds = self.generation_data[str(self.paddy_counter)]
else:
tg = self.generation_data[str(counter)]
for i in np.arange(tg[0], tg[1]):
gen_seeds = self.generation_data[str(counter)]
for i in np.arange(gen_seeds[0], gen_seeds[1]+1):
if self.seed_fitness[i] == max_fit:
if verbose:
print('seed_{0}'.format(i))
Expand Down
Loading

0 comments on commit 18f4aff

Please sign in to comment.