Skip to content

Commit

Permalink
major changes: refactored params and converted model_opt to TPE (GA d…
Browse files Browse the repository at this point in the history
…eprecated)
  • Loading branch information
tkchafin committed Jun 4, 2024
1 parent 7f47643 commit 3cf7676
Show file tree
Hide file tree
Showing 7 changed files with 1,163 additions and 961 deletions.
2 changes: 2 additions & 0 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ requirements:
- pip
- perl
- pytest
- hyperopt
- numpy
- pandas>2
- matplotlib
- scikit-learn
- lightgbm
- seaborn
- geopandas
- sortedcontainers
Expand Down
4 changes: 2 additions & 2 deletions scripts/ensembleResistnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import glob
import pandas as pd
from resistnet.resistance_network import ResistanceNetwork
from resistnet.model_optimisation import ModelRunner
from resistnet.model_optimisation import ModelRunnerGA
from resistnet.hall_of_fame import HallOfFame

# Set environment variable
Expand Down Expand Up @@ -95,7 +95,7 @@ def main():
)

# Step 3: Set up ModelRunner
runner = ModelRunner(
runner = ModelRunnerGA(
resistance_network=network,
seed=params.seed,
verbose=True
Expand Down
68 changes: 49 additions & 19 deletions scripts/runResistnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from resistnet.params import parseArgs
from resistnet.resistance_network import ResistanceNetwork
from resistnet.model_optimisation import ModelRunner
from resistnet.model_optimisation import ModelRunnerTPE, ModelRunnerGA


def main():
Expand All @@ -27,7 +27,6 @@ def main():
coords=params.coords,
variables=params.variables,
agg_opts=params.agg_opts,
pop_agg=params.pop_agg,
inmat=params.inmat,
reachid_col=params.reachid_col,
length_col=params.length_col,
Expand All @@ -39,8 +38,7 @@ def main():
if params.shapefile:
network.write_geodataframe(params.out, params.output_driver)

# Step 2: Initialise ModelRunner
runner = ModelRunner(
runner = ModelRunnerTPE(
resistance_network=network,
seed=params.seed,
verbose=True
Expand All @@ -50,41 +48,73 @@ def main():
if params.gridSearch:
runner.optimise_univariate(
fitmetric=params.fitmetric,
threads=params.GA_procs,
threads=params.procs,
max_shape=params.max_shape,
out=params.out,
plot=True,
verbose=True
)

# Step 3: Run GA optimisation
runner.run_ga(
maxgens=params.maxGens,
# Step 3: Run optimisation
runner.run_tpe(
max_evals=params.max_iter,
fitmetric=params.fitmetric,
threads=params.GA_procs,
burnin=params.burnin,
deltaB=params.deltaB,
deltaB_perc=params.deltaB_perc,
indpb=params.indpb,
mutpb=params.mutpb,
cxpb=params.cxpb,
threads=params.procs,
nFail=params.nfail,
popsize=params.popsize,
maxpopsize=params.maxpopsize,
fixWeight=params.fixWeight,
fixShape=params.fixShape,
min_weight=params.min_weight,
max_shape=params.max_shape,
max_hof_size=params.max_hof_size,
awsum=params.awsum,
only_keep=params.only_keep,
only_keep=True,
use_full=params.use_full,
fixed_params=params.transFile,
out=params.out,
plot=True,
verbose=True
verbose=True,
reps=params.reps,
n_startup=params.nstart,
n_candidates=params.ncand,
gamma=params.gamma
)

# # Step 2: Initialise ModelRunner
# runner = ModelRunnerGA(
# resistance_network=network,
# seed=params.seed,
# verbose=True
# )


# # Step 3: Run GA optimisation
# runner.run_ga(
# maxgens=params.maxGens,
# fitmetric=params.fitmetric,
# threads=params.GA_procs,
# burnin=params.burnin,
# deltaB=params.deltaB,
# deltaB_perc=params.deltaB_perc,
# indpb=params.indpb,
# mutpb=params.mutpb,
# cxpb=params.cxpb,
# nFail=params.nfail,
# popsize=params.popsize,
# maxpopsize=params.maxpopsize,
# fixWeight=params.fixWeight,
# fixShape=params.fixShape,
# min_weight=params.min_weight,
# max_shape=params.max_shape,
# max_hof_size=params.max_hof_size,
# awsum=params.awsum,
# only_keep=params.only_keep,
# use_full=params.use_full,
# fixed_params=params.transFile,
# out=params.out,
# plot=True,
# verbose=True
# )


# Call main function
if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions src/resistnet/hall_of_fame.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, variables, max_size, use_full=False, init_pop=None):
cols = ["fitness"]
for v in variables:
cols.extend([
str(v), f"{v}_weight", f"{v}_trans", f"{v}_shape", f"{v}_asym"
str(v), f"{v}_weight", f"{v}_trans", f"{v}_shape"
])
cols.extend(["loglik", "r2m", "aic", "delta_aic_null"])
self.data = pd.DataFrame(columns=cols)
Expand Down Expand Up @@ -350,7 +350,6 @@ def get_best_model(self):
"Weight": best_model[f"{var}_weight"],
"Shape": best_model[f"{var}_shape"],
"Transformation": best_model[f"{var}_trans"],
"Asymmetry": best_model[f"{var}_asym"],
}
for var in self.variables
]
Expand Down Expand Up @@ -433,7 +432,7 @@ def cleanHOF(self):

for v in self.variables:
mask = ret[v] == 0
for attr in ["_weight", "_trans", "_shape", "_asym"]:
for attr in ["_weight", "_trans", "_shape"]:
ret.loc[mask, f"{v}{attr}"] = np.nan

if ret.iloc[0]["fitness"] == (ret.iloc[0]["aic"] * -1):
Expand Down Expand Up @@ -732,7 +731,7 @@ def from_dataframe(cls, df, max_size=None):
the DataFrame.
"""
# Extract variable names from the DataFrame column names
variable_prefixes = ["_weight", "_trans", "_shape", "_asym"]
variable_prefixes = ["_weight", "_trans", "_shape"]
variables = set()
for col in df.columns:
for prefix in variable_prefixes:
Expand Down Expand Up @@ -760,5 +759,6 @@ def from_dataframe(cls, df, max_size=None):
instance.data = df.copy()
instance.data = instance.drop_active_duplicates(instance.data,
instance.variables)
instance._update_data_frame()

return instance
Loading

0 comments on commit 3cf7676

Please sign in to comment.